TIL: CSS "content" accepts alternative text

栏目: IT技术 · 发布时间: 4年前

内容简介:Developers often useUnfortunately, you have to consider usingLet's have a look at an example:

Developers often use before and after pseudo-elements ( generated content ) for the styling of elements in websites. With a few lines of extra CSS, it is possible to include icons, images, or even add text without adjusting the HTML.

Unfortunately, you have to consider using content in pseudo-elements very carefully because it can affect accessibility. Just because your generated content is not defined in the HTML, it doesn't mean that it is not picked up by assistive technology like screen readers.

Let's have a look at an example:

.new-item::before {
  content: "★";
}

The above CSS code prepends the symbol (black star) to the inner content of elements with the class new-item . And this might be all great from a visual perspective, but for screen readers, it has unexpected side effects.

<a href="new-things">go to new things</a>

This anchor link is now presented with the visual symbol of a star. On the other hand, screen readers will now read out "Black star go to new things". This experience is not great!

Today I learned that the content property supports a way to define alternative text for generated content .

.new-item::before {
  /* "black star" and element content is read out */
  content: "★";

  /* "Highlighted item" and element content is read out */
  content: "★" / "Highlighted item";

  /* Generated content is ignored and only element content is read out */
  content: "★" / "";
}

That's pretty cool because we can now provide alternative texts right in CSS! Unfortunately, at the time of writing, the overall browser support is not given yet (Firefox and Safari are missing).

Today, you should still use generated content, and this way of handling its alternative text very carefully. Going with a separate element in combination with aria-hidden=true is probably still a better approach these days.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

UNIX系统编程: 通信、并发与线程

UNIX系统编程: 通信、并发与线程

【美】Kay Robbins、Steve Robbins / 师蓉 / 电子工业出版社 / 2018-5 / 198

《UNIX系统编程: 通信、并发与线程》是一本基于最新UNIX标准的完备的参考书,对UNIX编程的要点进行了清晰易懂的介绍,从一些用于说明如何使用系统调用的短小代码段开始,逐渐过渡到能帮助读者扩展自己技能水平的实际项目中。《UNIX系统编程: 通信、并发与线程》中对通信、并发和线程问题进行了深入探讨,对复杂的概念(如信号和并发)进行了全面且清晰的解释,还覆盖了与文件、信号、信号量、POSIX线程和......一起来看看 《UNIX系统编程: 通信、并发与线程》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具