HTML - alt Attribute



The HTML alt attribute is essential for supplying alternative text for images, facilitating accessibility, and improving user experience. When the image cannot be rendered or by screen readers for users who has vision issues, it is used to describe the image's content, function, or purpose.

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

Following is the syntax of alt attribute −

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

Where value can any alternate information about the current image.

Example

In the following example, we are going to use the alt attribute with the imag tag.

<!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>

When we run the above code, it will generate an output consisting of the image along with the alt text displayed on the webpage.

Example

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 wihtout any error</p>
   <input type="image" src="https://www.tutorialspoint.com/images/logo.png?v3" alt="Tutorialspoint logo">
</body>
</html>

On running the above code, the output window will pop up displaying the alte text along with a image on the webpage.

Example

Let's look at the following example, where we are going to use the alt attribute with the area tag.

<!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>

When we run the above code, it will generate an output consisting of the images displayed on the webpage. when the user clicks on the image it displays its alt text.

html_attributes_reference.htm
Advertisements