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;
Example 2
Change the alternative text of the image:
document.getElementById("myImg").alt = "Beijing Scenery";
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:
|
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