无障碍

让图标对更多人可用

图标能传递方向、状态和操作。写的时候要让辅助技术也能拿到同样的信息。

图标字体和无障碍

读屏软件会读 CSS 生成的内容,也会读某些 Unicode。图标如果只写成默认的 ,可能出现下面两种情况。

  • 读屏软件找不到可以念的内容
  • 读屏软件念出 Unicode,和这个图标在页面上的意思对不上

用 CDN 自动处理

Font Awesome CDN 可以自动补上无障碍属性。打开对应设置,按 他们的写法,下面这些手动步骤会自动加上。

或

自己写无障碍属性

不用 CDN 时,自己告诉辅助技术:这个图标该忽略,还是该念出文字。

只作装饰

图标只是装饰,或旁边已经有同样的文字,就不要再念一遍。在图标上加 aria-hidden="true"。

<i class="fa fa-fighter-jet" aria-hidden="true"></i>
纯装饰
<h1 class="logo">
  <i class="fa fa-pied-piper" aria-hidden="true"></i>
  Pied Piper, A Middle-Out Compression Solution Making Data Storage Problems Smaller
</h1>
当作标志
<a href="https://github.com/FortAwesome/Font-Awesome"><i class="fa fa-github" aria-hidden="true"></i> View this project's code on Github</a>
放在链接文字前面

图标本身表达意思

图标用来表达意思时,辅助技术也要拿到同样的文字。按钮、表单、开关都一样。可以这样写:

图标不能被聚焦时

图标加 aria-hidden="true",再用一个 <span class="sr-only"> 放下要念的文字。视力正常的人如果需要提示,可以再加 title。

<dl>
  <dt>
    <i class="fa fa-car" aria-hidden="true" title="Time to destination by car"></i>
    <span class="sr-only">Time to destination by car:</span>
  </dt>
  <dd>4 minutes</dd>
  <dt>
    <i class="fa fa-bicycle" aria-hidden="true" title="Time to destination by bike"></i>
    <span class="sr-only">Time to destination by bike:</span>
  </dt>
  <dd>12 minutes</dd>
</dl>
用图标表示出行方式
<i class="fa fa-hourglass" aria-hidden="true" title="60 minutes remain in your exam"></i>
<span class="sr-only">60 minutes remain in your exam</span>
<i class="fa fa-hourglass-half" aria-hidden="true" title="30 minutes remain in your exam"></i>
<span class="sr-only">30 minutes remain in your exam</span>
<i class="fa fa-hourglass-end" aria-hidden="true" title="0 minutes remain in your exam"></i>
<span class="sr-only">0 minutes remain in your exam</span>
用图标表示剩余时间

图标是可以操作的控件

控件本身可以聚焦时,不用隐藏的 <span>,直接在控件上加 aria-label。需要鼠标提示时,再加 title,或用 自定义提示。

<a href="path/to/shopping/cart" aria-label="查看购物车里的 3 件商品">
  <i class="fa fa-shopping-cart" aria-hidden="true"></i>
</a>
用图标表示购物车
<a href="#navigation-main" aria-label="跳到主导航">
  <i class="fa fa-bars" aria-hidden="true"></i>
</a>
用图标链到导航
<a class="btn btn-danger" href="path/to/settings" aria-label="删除">
  <i class="fa fa-trash-o" aria-hidden="true" title="Delete this item?"></i>
</a>
删除按钮,并用 title 做鼠标提示

上面这些写法能避开常见问题,但覆盖不了所有情况,比如低视力用户需要更高的对比度。下面几篇可以继续看。

遇到问题可以 去反馈。