Image alt attribute

Definition and Usage

alt attribute setting or returning the image's alt attribute value.

If the image cannot be displayed for some reason (due to slow connection speed, incorrect src attribute, or the user using a screen reader), this necessary alt attribute Specify the alternative text for an image.

Note:When the mouse hovers over the <img> element, Internet Explorer displays the value of the alt attribute as a tooltip. According to the HTML specification, this is not the correct behavior. All other browsers follow the specification and only display alternative text when the image cannot be displayed.

Tip:If you want to create a tooltip for an image, use the global title attribute.

Example

Example 1

Return the alternative text of the image:

var x = document.getElementById("myImg").alt;

Try it yourself

Example 2

Change the alternative text of the image:

document.getElementById("myImg").alt = "Beijing Scenery";

Try it yourself

Syntax

Return the alt attribute:

imageObject.alt

Set the alt attribute:

imageObject.alt = text

Attribute value

Value Description
text

Specify the alternative text for an image.

Alternative Text Guide:

  • If the image contains information, the text should describe the image
  • If the image is within an <a> element, the text should indicate the position of the link
  • If the image is only for decoration, please use alt=""

Technical Details

Return Value: A string value that represents the alternative text for the image.

Browser Support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support

Related Pages

HTML Reference Manual:HTML <img> alt Attribute