添加空alt=""有什么好处?以及应该使用哪个 alt 这个 alt=""或 alt=” ”?
添加 null alt="" 有什么好处?只是为了通过验证还是有更多原因
以及应该如何编写?
像这样,没有空格
alt=""
或这个有一个空格
alt=" "
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
验证您的 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.
其他答案已经指出了标准中的要求。这是一个实际示例:
给定空白替代文本,lynx 将呈现:
如果缺少 alt 属性,lynx 将呈现:
您不希望内容中散布着不相关的文件名。
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:
You don't want your content to have irrelevant filenames scattered throughout.
对于没有合适的替代文本的图像(即不携带任何语义的图片,例如装饰元素),
alt
属性应为空。空的意思是空的,没有一个空间(这是一种惯例和建议,但很好)。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).我也会将其添加为答案(最初是对另一个答案的评论),因为这样做是有意义的。
用于设计页面样式的图像(因此没有真正的“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.