添加空alt=""有什么好处?以及应该使用哪个 alt 这个 alt=""或 alt=” ”?

发布于 2024-08-19 15:01:17 字数 162 浏览 8 评论 0 原文

添加 null alt="" 有什么好处?只是为了通过验证还是有更多原因

以及应该如何编写?

像这样,没有空格

alt=""

或这个有一个空格

alt=" "

What is the benefit to 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=" "

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一百个冬季 2024-08-26 15:01:17

验证您的 XHTML。 alt 是图像的必需属性。

然而,将其添加为空是程序员懒惰的表现(尽管我承认我也对那些对站点导航不重要的图像(如小装饰元素等)执行此操作)。

PS 如果您有阴影组件等装饰元素,某些装饰品您可以不将它们与图像一起添加,而是将其作为 CSS 背景添加,从而避免需要编写替代文本并保持标记中不含非内容内容。

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.

临风闻羌笛 2024-08-26 15:01:17

其他答案已经指出了标准中的要求。这是一个实际示例:

给定空白替代文本,lynx 将呈现:

如果缺少 alt 属性,lynx 将呈现:

文件名.jpg

您不希望内容中散布着不相关的文件名。

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.

初心 2024-08-26 15:01:17

对于没有合适的替代文本的图像(即不携带任何语义的图片,例如装饰元素),alt 属性应为空。空的意思是空的,没有一个空间(这是一种惯例和建议,但很好)。

必须为 IMGAREA 元素指定 alt 属性。对于 INPUTAPPLET 元素来说,它是可选的。

虽然替代文本可能非常有帮助,但必须小心处理。作者应遵守以下准则:

  • 在包含旨在格式化页面的图像时,请勿指定不相关的替代文本,例如,alt="red ball" 对于添加红球来装饰标题或段落。在这种情况下,替代文本应该是空字符串 ("")。无论如何,建议作者避免使用图像来格式化页面;应使用样式表。
  • 不要指定无意义的替代文本(例如“虚拟文本”)。这不仅会让用户感到沮丧,还会减慢必须将文本转换为语音或盲文输出的用户代理的速度。

实施者应查阅有关可访问性的部分,以获取有关如何处理省略替代文本情况的信息。

HTML 4 规范。第13.8节如何指定替代文本

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

恏ㄋ傷疤忘ㄋ疼 2024-08-26 15:01:17

我也会将其添加为答案(最初是对另一个答案的评论),因为这样做是有意义的。

用于设计页面样式的图像(因此没有真正的“alt”用法)应通过 CSS 和背景图像及其相关项插入,而不是通过标记插入。这样你就可以同时做两件好事。您将设计保留在样式表中,并将无语义的代码保留在页面之外。

尽管我确实认为“语义就是上帝”运动未能看到 div 和 span 的失败,以及它们产生的固有歧义,但我仍然认为带有背景图像的 div 比 img 标签的样式更好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文