The alt attribute is a required attribute of the element, which gives the alternative text of the image for use when the image cannot be displayed.
The alt attribute provides alternative information when the user cannot view the image for some reason (such as: slow opening speed, error in the src attribute, or user using a screen reader).
Note: Internet Explorer’s processing of the alt attribute is to display the alt attribute value as a tooltip when the mouse cursor stays on the image. But according to the HTML specification, this is not the correct way of doing things. Other browsers are standard-compliant, displaying the alt attribute value when the image cannot be loaded.
Tip: If you want to create a tooltip for an image, you should use the title attribute.
Extended information
The correct use of the alt and title attributes in the img tag:
The img tag has two attributes respectively For alt and title, many beginners are still confused about the correct use of these two attributes. Of course, part of the reason is also caused by the IE browser. The correct use of these two attributes can not only improve the search ability of pictures, but also help in user experience.
alt
The real function of this attribute is to replace the text when the picture cannot be displayed correctly, but it also plays the role of title under IE6 The function (the text prompt after the mouse is placed), the implementation method of IE is actually wrong.
If you want to display a prompt when the mouse is over, you should use the title attribute. Due to wrong guidance, many beginners only add the alt attribute to the img tag.
title
The text prompt displayed when the mouse slides over is very important in user experience. Of course, it is not necessary to add this attribute to all img tags. For example, the logo is more important or the image content that users will experience is recommended to add this attribute.
Recommended tutorial: “HTML Tutorial”
The above is the detailed content of the alt function of the img tag. For more information, please pay attention to other related articles on 1024programmer.com!