2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 587d778d367417b2b2512aaa
|
2020-12-16 07:37:30 +00:00
|
|
|
|
title: 使用自定义 CSS 让元素仅对屏幕阅读器可见
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 0
|
2020-02-11 09:06:41 +00:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cJ8QGkhJ'
|
|
|
|
|
forumTopicId: 301020
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: make-elements-only-visible-to-a-screen-reader-by-using-custom-css
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-03-12 14:30:30 +00:00
|
|
|
|
到目前为止,所有关于可访问性的挑战都没有使用 CSS。 这是为了在介绍视觉设计方面之前强调使用逻辑结构和有语义意义的标签的重要性。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
但如果我们想在页面中添加一些只对屏幕阅读器可见的内容,可以用 CSS 来实现。 当信息为视觉格式(例如图表)时,但屏幕阅读器用户需要备用文稿(例如表格)来访问数据,在这种情况下, 使用 CSS 将屏幕的只读元素放到浏览器窗口可视区域之外。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2020-02-11 09:06:41 +00:00
|
|
|
|
举个例子:
|
|
|
|
|
|
|
|
|
|
```css
|
|
|
|
|
.sr-only {
|
|
|
|
|
position: absolute;
|
|
|
|
|
left: -10000px;
|
|
|
|
|
width: 1px;
|
|
|
|
|
height: 1px;
|
|
|
|
|
top: auto;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2021-08-25 16:12:11 +00:00
|
|
|
|
**注意:** 以下的 CSS 解决方案与上面的结果不同:
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2020-02-11 09:06:41 +00:00
|
|
|
|
<ul>
|
2021-01-08 19:20:48 +00:00
|
|
|
|
<li><code>display: none;</code> 或 <code>visibility: hidden;</code> 会把内容彻底隐藏起来,屏幕阅读器也无法获取这些内容。</li>
|
|
|
|
|
<li>如果把值设置为 0px,如 <code>width: 0px; height: 0px;</code>,意味着让元素脱离文档流,这样做同样也会让屏幕阅读器忽略此元素。</li>
|
2020-02-11 09:06:41 +00:00
|
|
|
|
</ul>
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
Camper Cat 为他的训练页面创建了一个十分酷炫的条形图。 考虑到可访问性,他还将数据放入到了一个表格中,供屏幕阅读器用户使用。 表格已有一个 `sr-only` class,但是还没有添加 CSS 规则。 设置 `position` 的值为 `absolute`,`left` 的值为 `-10000px`,`width` 和 `height` 的值均为 `1px`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
设置 `sr-only` class 的 `position` 属性值为 `absolute`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert($('.sr-only').css('position') == 'absolute');
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
设置 `sr-only` class 的 `left` 属性值为`-10000px`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert($('.sr-only').css('left') == '-10000px');
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-16 22:21:30 +00:00
|
|
|
|
设置 `sr-only` class 的 `width` 属性值为 `1` 像素。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/width:\s*?1px/gi));
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-16 22:21:30 +00:00
|
|
|
|
设置 `sr-only` class 的 `height` 属性值为 `1` 像素。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/height:\s*?1px/gi));
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-02-11 09:06:41 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<head>
|
|
|
|
|
<style>
|
|
|
|
|
.sr-only {
|
|
|
|
|
position: ;
|
|
|
|
|
left: ;
|
|
|
|
|
width: ;
|
|
|
|
|
height: ;
|
|
|
|
|
top: auto;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<header>
|
|
|
|
|
<h1>Training</h1>
|
|
|
|
|
<nav>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="#stealth">Stealth & Agility</a></li>
|
|
|
|
|
<li><a href="#combat">Combat</a></li>
|
|
|
|
|
<li><a href="#weapons">Weapons</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</nav>
|
|
|
|
|
</header>
|
|
|
|
|
<section>
|
|
|
|
|
<h2>Master Camper Cat's Beginner Three Week Training Program</h2>
|
|
|
|
|
<figure>
|
|
|
|
|
<!-- Stacked bar chart of weekly training -->
|
|
|
|
|
<p>[Stacked bar chart]</p>
|
|
|
|
|
<br />
|
|
|
|
|
<figcaption>Breakdown per week of time to spend training in stealth, combat, and weapons.</figcaption>
|
|
|
|
|
</figure>
|
|
|
|
|
<table class="sr-only">
|
|
|
|
|
<caption>Hours of Weekly Training in Stealth, Combat, and Weapons</caption>
|
|
|
|
|
<thead>
|
|
|
|
|
<tr>
|
|
|
|
|
<th></th>
|
|
|
|
|
<th scope="col">Stealth & Agility</th>
|
|
|
|
|
<th scope="col">Combat</th>
|
|
|
|
|
<th scope="col">Weapons</th>
|
|
|
|
|
<th scope="col">Total</th>
|
|
|
|
|
</tr>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody>
|
|
|
|
|
<tr>
|
|
|
|
|
<th scope="row">Week One</th>
|
|
|
|
|
<td>3</td>
|
|
|
|
|
<td>5</td>
|
|
|
|
|
<td>2</td>
|
|
|
|
|
<td>10</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<th scope="row">Week Two</th>
|
|
|
|
|
<td>4</td>
|
|
|
|
|
<td>5</td>
|
|
|
|
|
<td>3</td>
|
|
|
|
|
<td>12</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<th scope="row">Week Three</th>
|
|
|
|
|
<td>4</td>
|
|
|
|
|
<td>6</td>
|
|
|
|
|
<td>3</td>
|
|
|
|
|
<td>13</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
</section>
|
|
|
|
|
<section id="stealth">
|
|
|
|
|
<h2>Stealth & Agility Training</h2>
|
|
|
|
|
<article><h3>Climb foliage quickly using a minimum spanning tree approach</h3></article>
|
|
|
|
|
<article><h3>No training is NP-complete without parkour</h3></article>
|
|
|
|
|
</section>
|
|
|
|
|
<section id="combat">
|
|
|
|
|
<h2>Combat Training</h2>
|
|
|
|
|
<article><h3>Dispatch multiple enemies with multithreaded tactics</h3></article>
|
|
|
|
|
<article><h3>Goodbye, world: 5 proven ways to knock out an opponent</h3></article>
|
|
|
|
|
</section>
|
|
|
|
|
<section id="weapons">
|
|
|
|
|
<h2>Weapons Training</h2>
|
|
|
|
|
<article><h3>Swords: the best tool to literally divide and conquer</h3></article>
|
|
|
|
|
<article><h3>Breadth-first or depth-first in multi-weapon training?</h3></article>
|
|
|
|
|
</section>
|
|
|
|
|
<footer>© 2018 Camper Cat</footer>
|
|
|
|
|
</body>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```html
|
|
|
|
|
<head>
|
|
|
|
|
<style>
|
|
|
|
|
.sr-only {
|
|
|
|
|
position: absolute;
|
|
|
|
|
left: -10000px;
|
|
|
|
|
width: 1px;
|
|
|
|
|
height: 1px;
|
|
|
|
|
top: auto;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<header>
|
|
|
|
|
<h1>Training</h1>
|
|
|
|
|
<nav>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="#stealth">Stealth & Agility</a></li>
|
|
|
|
|
<li><a href="#combat">Combat</a></li>
|
|
|
|
|
<li><a href="#weapons">Weapons</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</nav>
|
|
|
|
|
</header>
|
|
|
|
|
<section>
|
|
|
|
|
<h2>Master Camper Cat's Beginner Three Week Training Program</h2>
|
|
|
|
|
<figure>
|
|
|
|
|
<!-- Stacked bar chart of weekly training -->
|
|
|
|
|
<p>[Stacked bar chart]</p>
|
|
|
|
|
<br />
|
|
|
|
|
<figcaption>Breakdown per week of time to spend training in stealth, combat, and weapons.</figcaption>
|
|
|
|
|
</figure>
|
|
|
|
|
<table class="sr-only">
|
|
|
|
|
<caption>Hours of Weekly Training in Stealth, Combat, and Weapons</caption>
|
|
|
|
|
<thead>
|
|
|
|
|
<tr>
|
|
|
|
|
<th></th>
|
|
|
|
|
<th scope="col">Stealth & Agility</th>
|
|
|
|
|
<th scope="col">Combat</th>
|
|
|
|
|
<th scope="col">Weapons</th>
|
|
|
|
|
<th scope="col">Total</th>
|
|
|
|
|
</tr>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody>
|
|
|
|
|
<tr>
|
|
|
|
|
<th scope="row">Week One</th>
|
|
|
|
|
<td>3</td>
|
|
|
|
|
<td>5</td>
|
|
|
|
|
<td>2</td>
|
|
|
|
|
<td>10</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<th scope="row">Week Two</th>
|
|
|
|
|
<td>4</td>
|
|
|
|
|
<td>5</td>
|
|
|
|
|
<td>3</td>
|
|
|
|
|
<td>12</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<th scope="row">Week Three</th>
|
|
|
|
|
<td>4</td>
|
|
|
|
|
<td>6</td>
|
|
|
|
|
<td>3</td>
|
|
|
|
|
<td>13</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
</section>
|
|
|
|
|
<section id="stealth">
|
|
|
|
|
<h2>Stealth & Agility Training</h2>
|
|
|
|
|
<article><h3>Climb foliage quickly using a minimum spanning tree approach</h3></article>
|
|
|
|
|
<article><h3>No training is NP-complete without parkour</h3></article>
|
|
|
|
|
</section>
|
|
|
|
|
<section id="combat">
|
|
|
|
|
<h2>Combat Training</h2>
|
|
|
|
|
<article><h3>Dispatch multiple enemies with multithreaded tactics</h3></article>
|
|
|
|
|
<article><h3>Goodbye, world: 5 proven ways to knock out an opponent</h3></article>
|
|
|
|
|
</section>
|
|
|
|
|
<section id="weapons">
|
|
|
|
|
<h2>Weapons Training</h2>
|
|
|
|
|
<article><h3>Swords: the best tool to literally divide and conquer</h3></article>
|
|
|
|
|
<article><h3>Breadth-first or depth-first in multi-weapon training?</h3></article>
|
|
|
|
|
</section>
|
|
|
|
|
<footer>© 2018 Camper Cat</footer>
|
|
|
|
|
</body>
|
|
|
|
|
```
|