HTML - alt Attribute



HTML alt attribute is used within an img element to provide a textual description of the image, which is displayed if the image fails to load.

When the alt attribute is used correctly, everyone can understand the information and context of images on web sites, including individuals with disabilities or slow internet connections. It's crucial for search engine optimization, inclusive design, and web accessibility.

Syntax

<tag alt = "value"></tag>

Applies On

Below listed elements allow using of the HTML alt attribute.

Element Description
<img> HTML <img> tag is used to append images in document
<area> HTML <area> element in HTML is used to define a clickable area within an image map.
<input> HTML <input> element in HTML is used to input text.

Examples of HTML alt attribute

The following codes describe various usage ways of alt tag.

Alt attribute with img Element

The below code show how alt attribute can be used with img tag. In first case image is displayed without any error, but in second case alternate text is displayed as image is not found in specified location.

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'alt' attribute</title>
   <style>
      img {
         width: 300px;
         height: 100px;
      }
   </style>
</head>

<body>
   <!--HTML 'alt' attribute-->
   <p>Example of the HTML 'alt' attribute</p>
   <p>Image without any error</p>
   <img src="https://www.tutorialspoint.com/images/logo.png?v3" 
      alt="Tutorialpoint logo">
   <br>
   <p>Images with error</p>
   <img src="Asset-2PP_.png" 
      alt="Tutorialpoint logo">
</body>

</html>

Alt attribute with input Element

Considering the another scenario, where we are going to use the alt attribute with the input type=image.

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'alt' attribute</title>
   <style>
      img {
         width: 300px;
         height: 50px;
      }
   </style>
</head>

<body>
   <!--HTML 'alt' attribute-->
   <p>Example of the HTML 'alt' attribute</p>
   <p>Image with an error</p>
   <input type="image" 
      src="download1.png" 
      alt="Tutorialspoint old logo">
   <p>Image without any error</p>
   <input type="image" 
      src="https://www.tutorialspoint.com/images/logo.png?v3" 
      alt="Tutorialspoint logo">
</body>

</html>

Alt attribute with area Element

Let's look at the following example, where we are going to use the alt attribute with the area tag. The below code will generate an output consisting of the images displayed on the webpage. When the user clicks on the image it displays its alt text.

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'alt' attribute</title>
</head>

<body>
   <!--HTML 'alt' attribute-->
   <p>Example of the HTML 'alt' attribute</p>
   <!--<img src="images (1).png"
    usemap="#sep" 
    width = "400" 
    height="379">-->
   <img src="images (1).png" 
      alt="HTML CSS JS logo" 
      usemap="#sep" 
      width="200" 
      height="200">
   <map name="sep">
      <area shape="circle" 
      coords="30,44,270,350" 
      alt="HTML logo" 
      href="html logo.html">
   </map>
</body>

</html>

HTML logo.html file Code

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML</title>
</head>
<body>
   <img src="html log.jpg" alt="HTML logo">
</body>
</html>

Supported Browsers

Attribute Chrome Edge Firefox Safari Opera
alt Yes Yes Yes Yes Yes
html_attributes_reference.htm
Advertisements