开发者

What is the benefit to add empty alt=""? and which alt should be used this alt="" or alt=" "?

开发者 https://www.devze.com 2022-12-18 04:04 出处:网络
What is the benefit t开发者_StackOverflow中文版o add null alt=\"\"? is it only to pass validation or it has more reason

What is the benefit t开发者_StackOverflow中文版o add null alt=""? is it only to pass validation or it has more reason

and how it should be write?

like this, no space

alt=""

or this with one blank space

alt=" "


To get your XHTML validated. The alt is a required attribute on images.

Adding it empty is however a sign of laziness from programmers (although I admit I also do it for images that are not key to site navigation like little decorative elements and so on).

P.S. If you have decorative elements like shadow components, certain ornaments you can add them not with images but as a CSS background, thus avoiding the need to write an alternative text and keeping your markup clean of non-content stuff.


Other answers have pointed out the requirements in the standard. Here is a practical example:

Given blank alt text, lynx will render:

Given a missing alt attribute, lynx will render:

filename.jpg

You don't want your content to have irrelevant filenames scattered throughout.


For images that have no suitable alternate text (i. e. pictures that don't carry any semantics, such as decorative elements), the alt attribute should be empty. Empty meaning empty, not a single space (which is a convention and recommendation but a good one).

The alt attribute must be specified for the IMG and AREA elements. It is optional for the INPUT and APPLET elements.

While alternate text may be very helpful, it must be handled with care. Authors should observe the following guidelines:

  • Do not specify irrelevant alternate text when including images intended to format a page, for instance, alt="red ball" would be inappropriate for an image that adds a red ball for decorating a heading or paragraph. In such cases, the alternate text should be the empty string (""). Authors are in any case advised to avoid using images to format pages; style sheets should be used instead.
  • Do not specify meaningless alternate text (e.g., "dummy text"). Not only will this frustrate users, it will slow down user agents that must convert text to speech or braille output.

Implementors should consult the section on accessibility for information about how to handle cases of omitted alternate text.

—HTML 4 specification. Section 13.8 How to specify alternate text


I'll add this as an answer as well (originally a comment on another answer), since it kind of makes sense to do so.

Images used for styling the page (and therefore has no real "alt" usage) should be inserted through CSS and background-image and its relatives, not through markup. That way you do two good things at once. You keep your design in your stylesheets, and you keep unsemantic code out of your page.

Although I do think the "semantics is god"-movement has failed to see the fail that is div and span, and the inherent ambiguity they produce, I still think a div with background-image is better than an img tag for styling.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号