admin管理员组

文章数量:1593971

from:

https://learn.freecodecamp.one/

 

应用无障碍:为视觉受损的用户添加替代图像的文本

在其他挑战里你应该已经见到过img标签的alt属性了。alt属性中的文本作为备用文字描述了图片的内容,这可以帮助用户在图片加载失败或者不可见的情况下理解图片内容,也有助于搜索引擎理解图片内容,并将其加入到搜索结果中。例如:

<img src="importantLogo.jpeg" alt="Company logo">

那些无法通过视觉获取信息的用户,只能通过屏幕阅读器将网页内容转换为音频的方式获取信息,而屏幕阅读器通过识别alt属性,并朗读其中的内容,来告知用户图片包含的关键信息。

良好的alt文本可以简明扼要地描述图片信息,所以你应该为图片添加alt属性。另外,HTML5 标准也在考虑强制要求对图片添加alt属性。

 


碰巧,Camper Cat 是忍者中写代码最厉害的,他正在建立一个可以分享忍者知识的网站。在这个网站中,他想使用一张图片来展示他的忍者技能。为了可以让所有的访问者都能获取图片中的信息,Camper Cat 给img标签添加了一个alt属性(当图片的src属性没有指向任何图片时,显示器上才可看到alt属性中的文本)。

<img src="doingKarateWow.jpeg" alt="doingKarateWow">

应用无障碍:知道 Alt 文本何时应该留空

在上一个挑战中,我们了解到img标签必须有一个alt属性。在图片已经有了文字说明,或者仅仅为了美化页面的情况下,alt属性似乎有些多余。

即便如此,我们仍然需要为img标签添加alt属性,这时可以把它设为空,例如:

<img src="visualDecoration.jpeg" alt="">

对于背景图片,它们通常起装饰作用,而且含有 CSS 类,所以背景图片不会被屏幕阅读器处理。

注意:
对于有标题的图片,我们依然需要添加alt属性,因为这样有助于搜索引擎记录图片内容。

 


Camper Cat 已经大体写好了博客页面。他打算使用忍者刀图片作为两篇文章之间的分割线,并为图片添加一个空的alt属性(注意:这里img标签的src属性提供的链接是无效的,因此页面上可能不会显示图片,不用担心)。

<h1>Deep Thoughts with Master Camper Cat</h1>
<article>
  <h2>Defeating your Foe: the Red Dot is Ours!</h2>
  <p>To Come...</p>
</article>

<img src="samuraiSwords.jpeg" alt="">

<article>
  <h2>Is Chuck Norris a Cat Person?</h2>
  <p>To Come...</p>
</article>

应用无障碍:使用标题显示内容的层次关系

标题标签(包括h1h6)有很高的使用率,它们用于描述内容的主题。在屏幕阅读器中,用户为更快地了解页面内容,可以设置让阅读器只朗读页面标题。这意味着标题标签之间以及标签本身都应语义化,不应仅仅为了获得不同字号而使用不同级别的标题标签。

语义化:标签名能准确地表达它所含内容的信息类型。

对于一篇含有引言、正文、结论的论文,把结论作为引言的一部分没有任何意义,因为结论应该是独立的章节。类似地,页面中的标题标签也应该是有序的,并且能表明内容的层次关系。

在使用中,相同级别(或者更高级别)的标题标签用于开启新的章节,低一级别的标题标签用于开启上一级标题标签的子小节。

举个例子:一个h2标签后紧跟若干h4标签的页面,会让使用屏幕阅读器的用户感到困惑。尽管在页面中,使用这 6 个标题标签可以控制内容的的视觉样式,但我们应该使用 CSS 来调整。

最后一点,每个页面应该只有一个h1标签,用来说明页面主要内容。h1标签和其他的标题标签可以让搜索引擎获取页面的大纲。

 


Camper Cat 希望他的网站有一个介绍如何成为忍者页面。请帮助他修改标题标签,使它们语义化且顺序正确。你需要将所有的h5标题标签调整为恰当的级别,使它们是h2标题标签的子级。

<h1>How to Become a Ninja</h1>
<main>
  <h2>Learn the Art of Moving Stealthily</h2>
  <h3>How to Hide in Plain Sight</h3>
  <h3>How to Climb a Wall</h3>

  <h2>Learn the Art of Battle</h2>
  <h3>How to Strengthen your Body</h3>
  <h3>How to Fight like a Ninja</h3>

  <h2>Learn the Art of Living with Honor</h2>
  <h3>How to Breathe Properly</h3>
  <h3>How to Simplify your Life</h3>
</main>

 

应用无障碍:使用 main 元素包裹主题内容

HTML5 添加了诸如mainheaderfooternavarticlesection等大量新标签,这些新标签为开发人员提供更多的选择和辅助特性。

默认情况下,浏览器呈现这些新标签的方式与div相似。然而,合理地使用它们,可以使你的标签更加的语义化。辅助技术(如:屏幕阅读器)可以通过这些标签为用户提供更加准确的、易于理解的页面信息。

main标签用于呈现网页的主体内容,且每个页面只能有一个。这意味着它只应包含与页面中心主题相关的信息,而不应包含如导航连接、网页横幅等可以在多个页面中重复出现的内容。

main标签的语义化特性可以使辅助技术快速定位到页面的主体。有些页面中有 “跳转到主要内容” 的链接,使用main标签可以使辅助设备自动获得这个功能。

 


Camper Cat 对他的忍者武器页面有一些新的想法,请帮助他在header标签和footer标签(在接下来的挑战中会详细介绍)之间添加完整main标签来使页面语义化。在这个挑战中,你可以先让main为空。

<header>
  <h1>Weapons of the Ninja</h1>
</header>
<main>
  
</main>


<footer></footer>

应用无障碍:使用 article 元素包裹文章内容

article是另一个具有语义化特性的 HTML5 新标签。article是一个分段标签,用于呈现独立及完整的内容。这个标签适用于博客入口、论坛帖子或者新闻文章。

有些技巧可以用来判断内容是否独立,像是如果内容脱离了上下文,是否仍然有意义?类似地,对于 RSS 提要中的文本,它是否有意义?

请牢记,使用辅助设备的用户依赖组织良好的、语义化的标签来获取页面中的信息。

请注意sectiondiv的区别:
section也是一个 HTML5 新标签,与article标签的语义含义略有不同。article用于独立的、完整的内容,而section用于对与主题相关的内容进行分组。它们可以根据需要嵌套着使用。举个例子:如果一本书是一个article的话,那么每个章节就是section。当内容组之间没有联系时,可以使用div

<div> - 内容组
<section> - 有联系的内容组
<article> - 独立完整的内容

 


Camper Cat 打算使用article标签来呈现他的博客页面里的帖子,但是他忘记在顶部的帖子上使用它。请使用article标签来代替div标签。

<h1>Deep Thoughts with Master Camper Cat</h1>
<main>
  <article>
    <h2>The Garfield Files: Lasagna as Training Fuel?</h2>
    <p>The internet is littered with varying opinions on nutritional paradigms, from catnip paleo to hairball cleanses. But let's turn our attention to an often overlooked fitness fuel, and examine the protein-carb-NOM trifecta that is lasagna...</p>
  </article>

  <img src="samuraiSwords.jpeg" alt="">

  <article>
    <h2>Defeating your Foe: the Red Dot is Ours!</h2>
    <p>Felines the world over have been waging war on the most persistent of foes. This red nemesis combines both cunning stealth and lightening speed. But chin up, fellow fighters, our time for victory may soon be near...</p>
  </article>

  <img src="samuraiSwords.jpeg" alt="">

  <article>
    <h2>Is Chuck Norris a Cat Person?</h2>
    <p>Chuck Norris is widely regarded as the premier martial artist on the planet, and it's a complete coincidence anyone who disagrees with this fact mysteriously disappears soon after. But the real question is, is he a cat person?...</p>
  </article>
</main>

应用无障碍:使用 header 元素使屏幕阅读器更容易导航

header也是一个具有语义化的、提升页面可访问性的 HTML5 标签。它可以为父级标签呈现简介信息或者导航链接,适用于那些在多个页面顶部重复出现的内容。

main类似,header的语义化特性也可以使辅助技术快速定位到它的内容。

注意:
header用在 HTML 文档的body标签中。这点与包含页面标题、元信息的head标签不同。

 


Camper Cat 正在写一些训练忍者的精彩文章,并为它们建立一个新的页面。请使用header替换页面顶端包含h1div标签。

<body>

  <header>
    <h1>Training with Camper Cat</h1>
  </header>


  <main>
    <section id="stealth">
      <h2>Stealth &amp; 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>
  </main>
</body>

应用无障碍:使用 nav 元素使屏幕阅读器更容易导航

nav也是一个具有语义化特性的 HTML5 标签,用于呈现页面中的主导航链接。它可以使屏幕阅读器快速识别页面中的导航信息。

对于页面底部辅助性质的站点链接,不需要使用nav,用footer(在下个挑战中介绍)会更好。

 


Camper Cat 在他的忍者训练页面顶端使用了很多导航链接,但把它们写在了div中。请将div更改为nav标签,以提升页面的可访问性。

<body>
  <header>
    <h1>Training with Camper Cat</h1>

    <nav>
      <ul>
        <li><a href="#stealth">Stealth &amp; Agility</a></li>
        <li><a href="#combat">Combat</a></li>
        <li><a href="#weapons">Weapons</a></li>
      </ul>
    </nav>

  </header>
  <main>
    <section id="stealth">
      <h2>Stealth &amp; 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>
  </main>
</body>

应用无障碍:使用 footer 元素使屏幕阅读器更容易导航

headernav类似,footer也具有语义化特性,可以使辅助设备快速定位到它。它位于页面底部,用于呈现版权信息或者相关文档链接。

 


Camper Cat 的忍者训练页面进展顺利。请将他在页面底部呈现版权信息的div标签更改为footer标签。

<body>
  <header>
    <h1>Training</h1>
    <nav>
      <ul>
        <li><a href="#stealth">Stealth &amp; Agility</a></li>
        <li><a href="#combat">Combat</a></li>
        <li><a href="#weapons">Weapons</a></li>
      </ul>
    </nav>
  </header>
  <main>
    <section id="stealth">
      <h2>Stealth &amp; 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>
  </main>


  <footer>&copy; 2016 Camper Cat</footer>


</body>

应用无障碍:使用 audio 元素提高音频内容的可访问性

HTML5 的audio标签用于呈现音频内容,它也具有语义化特性。可以在audio上下文中为音频内容添加文字说明或者字幕链接,使听觉障碍用户也能获取音频中的信息。

audio支持controls属性,可以使浏览器为音频提供具有开始、暂停等功能的播放控件。controls属性是一个布尔属性,只要这个属性出现在audio标签中,浏览器就会开启播放控件。

举个例子:

<audio id="meowClip" controls>
  <source src="audio/meow.mp3" type="audio/mpeg" />
  <source src="audio/meow.ogg" type="audio/ogg" />
</audio>

注意:
多媒体内容通常同时包含音频与视频部分,它需要同步音频与字幕,以使视觉或听觉障碍用户可以获取它的内容。一般情况下,网页开发者不需要创建音频与字幕,但是需要将它们添加到多媒体中。

 


是时候让 Camper Cat 休息一下,并与朋友 camper Zersiax (@zersiax) 会面。Zersiax 是一位屏幕阅读器用户,同时也是无障碍设计的高手。为了体验 Zersiax 的屏幕阅读器的朗读效果,请在p标签之后添加一个具有controls属性的audio标签。然后在audio标签内添加一个source标签,并且设置src属性为"https://s3.amazonaws/freecodecamp/screen-reader.mp3",并且设置type属性为"audio/mpeg".

注意:
音频片段的播放速度可能会快到另我们难以理解,但是对于屏幕阅读器用户来说这是正常速度。

<body>
  <header>
    <h1>Real Coding Ninjas</h1>
  </header>
  <main>
    <p>A sound clip of Zersiax's screen reader in action.</p>
    <audio controls>
      <source src="https://s3.amazonaws/freecodecamp/screen-reader.mp3" type="audio/mpeg">
    </audio>
    
  </main>
</body>

应用无障碍:使用 figure 元素提高图表的可访问性

HTML5 引入了figure标签以及与之相关的figcaption标签。它们一起用于展示可视化信息(如:图片、图表)及其标题。这样通过语义化对内容进行分组并配以用于解释figure的文字,可以极大的提升内容的可访问性。

对于图表之类的可视化数据,标题可以为屏幕阅读器用户提供简要的说明。但是这里有一个难点,如何处理那些超出屏幕可视范围(使用 CSS )的表格版本的图表数据,以使屏幕阅读器用户也可以获取信息。

举个例子——注意figcaption包含在figure标签中,并且可以与其他标签组合使用:

<figure>
  <img src="roundhouseDestruction.jpeg" alt="Photo of Camper Cat executing a roundhouse kick">
  <br>
  <figcaption>
    Master Camper Cat demonstrates proper form of a roundhouse kick.
  </figcaption>
</figure>

 


Camper Cat 正在努力创建一张条形图,用来显示每周用于隐形、战斗、武器训练的时间。请帮助完善他的页面,将他的用于呈现图表的div标签修改为figure标签,用于呈现图表标题的p标签改为figcaption标签。

<body>
  <header>
    <h1>Training</h1>
    <nav>
      <ul>
        <li><a href="#stealth">Stealth &amp; Agility</a></li>
        <li><a href="#combat">Combat</a></li>
        <li><a href="#weapons">Weapons</a></li>
      </ul>
    </nav>
  </header>
  <main>
    <section>
      
      <!-- Add your code below this line -->
      <div>
        <!-- Stacked bar chart will go here -->
        <br>
        <p>Breakdown per week of time to spend training in stealth, combat, and weapons.</p>
      </div>
      <!-- Add your code above this line -->
      
    </section>
    <section id="stealth">
      <h2>Stealth &amp; 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>
  </main>
  <footer>&copy; 2016 Camper Cat</footer>
</body>

应用无障碍:使用 label 元素提高表单的可访问性

合理地使用语义化的 HTML 标签和属性可以提升页面可访问性。在接下来的挑战中,你将会看到使用表单属性的重要场景。

label标签用于呈现特定表单控件的文本,通常是选项的名称。这些文本代表了选项的含义,使表单具有更好的可读性。label标签的for属性指定了与label绑定的表单控件,并且屏幕阅读器也会读取for属性。

在 HTML 基础章节的课程中,我们已经了解了单选按钮标签。在那节课程中,我们为了可以点击单选按钮的名称,将input标签放在label标签中。在本节课程中,我们介绍了另外一种实现这个功能的方法,那就是使用for属性。

for属性的值必须与表达控件的id属性的值相同。举个例子:

<form>
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">
</form>

 


Camper Cat 希望他的博客文章能有很多订阅,他想添加一个电子邮件注册表单。请为表示电子邮件的label标签添加for属性,并设置属性值与input标签的id属性值相同。

<body>
  <header>
    <h1>Deep Thoughts with Master Camper Cat</h1>
  </header>
  <section>
    <form>
      <p>Sign up to receive Camper Cat's blog posts by email here!</p>
      
      
      <label for="email">Email:</label>
      <input type="text" id="email" name="email">
      
      
      <input type="submit" name="submit" value="Submit">
    </form>
  </section>
  <article>
    <h2>The Garfield Files: Lasagna as Training Fuel?</h2>
    <p>The internet is littered with varying opinions on nutritional paradigms, from catnip paleo to hairball cleanses. But let's turn our attention to an often overlooked fitness fuel, and examine the protein-carb-NOM trifecta that is lasagna...</p>
  </article>
  <img src="samuraiSwords.jpeg" alt="">
  <article>
    <h2>Defeating your Foe: the Red Dot is Ours!</h2>
    <p>Felines the world over have been waging war on the most persistent of foes. This red nemesis combines both cunning stealth and lightening speed. But chin up, fellow fighters, our time for victory may soon be near...</p>
  </article>
  <img src="samuraiSwords.jpeg" alt="">
  <article>
    <h2>Is Chuck Norris a Cat Person?</h2>
    <p>Chuck Norris is widely regarded as the premier martial artist on the planet, and it's a complete coincidence anyone who disagrees with this fact mysteriously disappears soon after. But the real question is, is he a cat person?...</p>
  </article>
  <footer>&copy; 2016 Camper Cat</footer>
</body>

应用无障碍:将单选按钮包裹在 fieldset 元素中以获得更好的可访问性

下一个表单主题与单选按钮的可访问性有关。在上一个挑战中,单选按钮含有一个拥有for属性的label标签,for属性指向相关选项的id。然而单选按钮通常成组出现,用户必须其中选择一项。本次挑战介绍一种可以语义化呈现单选按钮组的方法。

使用fieldset标签将单选按钮组包含在里面就可以实现这个目的,通常还会使用legend标签来为单选按钮组提供文字说明。屏幕阅读器也可以朗读这些文字。

当选项的含义很明确时,如:性别选择,fieldset标签与legend标签就可以省略。这时,使用含有for属性的label标签就足够了。

举个例子:

<form>
  <fieldset>
    <legend>Choose one of these three items:</legend>
    <input id="one" type="radio" name="items" value="one">
    <label for="one">Choice One</label><br>
    <input id="two" type="radio" name="items" value="two">
    <label for="two">Choice Two</label><br>
    <input id="three" type="radio" name="items" value="three">
    <label for="three">Choice Three</label>
  </fieldset>
</form>

 


当用户使用邮件注册时,Camper Cat 想知道他们的忍者等级。通过上一个挑战的学习,Camper Cat 创建了一组单选按钮,并为每个选项的label标签添加了for属性,但是 Camper Cat 的代码依然需要你的帮助。请将包含单选按钮组的div标签替换为fieldset标签;将p标签替换为legend标签。

<body>
  <header>
    <h1>Deep Thoughts with Master Camper Cat</h1>
  </header>
  <section>
    <form>
      <p>Sign up to receive Camper Cat's blog posts by email here!</p>
      <label for="email">Email:</label>
      <input type="text" id="email" name="email">
      
      
      <!-- Add your code below this line -->
      <fieldset>
        <legend>What level ninja are you?</legend>
        <input id="newbie" type="radio" name="levels" value="newbie">
        <label for="newbie">Newbie Kitten</label><br>
        <input id="intermediate" type="radio" name="levels" value="intermediate">
        <label for="intermediate">Developing Student</label><br>
        <input id="master" type="radio" name="levels" value="master">
        <label for="master">Master</label>
      </fieldset>
      <!-- Add your code above this line -->
      
      
      <input type="submit" name="submit" value="Submit">
    </form>
  </section>
  <article>
    <h2>The Garfield Files: Lasagna as Training Fuel?</h2>
    <p>The internet is littered with varying opinions on nutritional paradigms, from catnip paleo to hairball cleanses. But let's turn our attention to an often overlooked fitness fuel, and examine the protein-carb-NOM trifecta that is lasagna...</p>
  </article>
  <img src="samuraiSwords.jpeg" alt="">
  <article>
    <h2>Defeating your Foe: the Red Dot is Ours!</h2>
    <p>Felines the world over have been waging war on the most persistent of foes. This red nemesis combines both cunning stealth and lightening speed. But chin up, fellow fighters, our time for victory may soon be near...</p>
  </article>
  <img src="samuraiSwords.jpeg" alt="">
  <article>
    <h2>Is Chuck Norris a Cat Person?</h2>
    <p>Chuck Norris is widely regarded as the premier martial artist on the planet, and it's a complete coincidence anyone who disagrees with this fact mysteriously disappears soon after. But the real question is, is he a cat person?...</p>
  </article>
  <footer>&copy; 2016 Camper Cat</footer>
</body>

应用无障碍:添加可访问的日期选择器

表单中经常出现input标签,它可以用来创建多种表单控件。它的type属性指定了所要创建的input标签类型。

在以前的挑战中,你可能已经见过textsubmit类型的input标签,HTML5 引入了date类型来创建时间选择器。依赖于浏览器的支持,当点击input标签时,时间选择器会显示出来,这可以让用户填写表单更加容易。

对于旧版本的浏览器,type属性的默认值是text。这种情况下,可以利用label标签或者占位文本来提示用户input标签的输入类型为日期。

举个例子:

<label for="input1">Enter a date:</label>
<input type="date" id="input1" name="input1">

 


Camper Cat 想举办一场比武大会,他想收集参赛者的最佳参赛时间。请为 Camper Cat 的页面添加一个type属性且值为 date,id属性为 pickdate,name属性为 date 的input标签。

<body>
  <header>
    <h1>Tournaments</h1>
  </header>
  <main>
    <section>
      <h2>Mortal Kombat Tournament Survey</h2>
      <form>
        <p>Tell us the best date for the competition</p>
        <label for="pickdate">Preferred Date:</label>
        
        <!-- Add your code below this line -->
        <input type="date" id="pickdate" name="date">
        
        
        <!-- Add your code above this line -->
        
        <input type="submit" name="submit" value="Submit">
      </form>
    </section>
  </main>
  <footer>&copy; 2016 Camper Cat</footer>
</body>

 

应用无障碍:使用 HTML5 的 datatime 属性标准化时间

继续日期主题。HTML5 还引入了time标签与datetime属性来标准化时间。time是一个行内标签,用于在页面中呈现日期或时间,而datetime属性保存了日期的有效格式,辅助设备可以访问这个值。通过标准化时间格式,即使时间在文本中是以非正式的或口语化的形式编写,辅助设备依然可以获取准确的时间和日期。

举个例子:

<p>Master Camper Cat officiated the cage match between Goro and Scorpion <time datetime="2013-02-13">last Wednesday</time>, which ended in a draw.</p>

 


Camper Cat 的比武大会的时间确定了!请使用time标签包含文本 "Thursday, September 15<sup>th</sup>",并将datetime属性设置为 "2016-09-15"。

<body>
  <header>
    <h1>Tournaments</h1>
  </header>
  <article>
    <h2>Mortal Kombat Tournament Survey Results</h2>
    
    <!-- Add your code below this line -->
    
    <p>Thank you to everyone for responding to Master Camper Cat's survey. The best day to host the vaunted Mortal Kombat tournament is <time datetime="2016-09-15">Thursday, September 15<sup>th</sup></time>. May the best ninja win!</p>
    
    <!-- Add your code above this line -->
    
    <section>
      <h3>Comments:</h3>
      <article>
        <p>Posted by: Sub-Zero on <time datetime="2016-08-13T20:01Z">August 13<sup>th</sup></time></p>
        <p>Johnny Cage better be there, I'll finish him!</p>
      </article>
      <article>
        <p>Posted by: Doge on <time datetime="2016-08-15T08:12Z">August 15<sup>th</sup></time></p>
        <p>Wow, much combat, so mortal.</p>
      </article>
      <article>
        <p>Posted by: The Grim Reaper on <time datetime="2016-08-16T00:00Z">August 16<sup>th</sup></time></p>
        <p>Looks like I'll be busy that day.</p>
      </article>
    </section>
  </article>
  <footer>&copy; 2016 Camper Cat</footer>
</body>

应用无障碍:使用自定义 CSS 让元素仅对屏幕阅读器可见

到目前为止,所有关于可访问性的挑战都没有使用 CSS。这是为了让你在关注外观样式之前,先把页面的逻辑结构写清,以及明确语义化标签的重要性。

然而,如果我们需要在页面中添加一些只对屏幕阅读器可见的内容时,CSS 可以提升页面的可访问性。当信息以可视化形式(如:图表)展示,而屏幕阅读器用户需要一种替代方式(如:表格)来获取信息时,就会出现这种情况。CSS 被用来将这些仅供屏幕阅读器使用的信息定位到浏览器可见区域之外。

举个例子:

.sr-only {
  position: absolute;
  left: -10000px;
  width: 1px;
  height: 1px;
  top: auto;
  overflow: hidden;
}

注意:
下面的 CSS 代码与上面的结果不同:

  • display: none;visibility: hidden;会把内容彻底隐藏起来,对于屏幕阅读器也不例外。
  • 如果把值设置为 0px,如width: 0px; height: 0px;,意味着让元素脱离文档流,这样做也会让元素被屏幕阅读器忽略。

Camper Cat 为他的训练页面创建了一个十分酷炫的条形图,并将数据放入表格中,供屏幕阅读器用户使用。表格已经有了一个sr-only类,但是还没有添加 CSS 规则。请设置position的值为 absolute,left的值为 -10000px,widthheight的值为 1px。

<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 &amp; 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 &amp; 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 &amp; 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>&copy; 2016 Camper Cat</footer>
</body>

 

应用无障碍:使用高对比度文本提高可读性

低对比度的前景色与背景色会使文本难以阅读。足够的对比度可以提高内容的可读性,但是怎样的对比度才算是 “足够” 的?

Web 内容无障碍指南(WCAG)建议正常文本的对比度至少为 4.5 : 1。对比度是通过比较两种颜色的相对亮度值来计算的,其范围是从相同颜色的 1 : 1(无对比度)到白色与黑色的最高对比度 21 : 1。网上有很多可以帮助你计算对比度的工具。

 


Camper Cat 为他的博客选择了白色背景,浅灰色文字,对比度为 1.5 : 1,这使博客文章难以阅读。请将文字的color从当前的浅灰色(#D3D3D3)修改为深灰色(#636363),以使对比度提升到 6 : 1。

<head>
  <style>
  body {
    color: #636363;
    background-color: white;
  }
  </style>
</head>
<body>
  <header>
    <h1>Deep Thoughts with Master Camper Cat</h1>
  </header>
  <article>
    <h2>A Word on the Recent Catnip Doping Scandal</h2>
    <p>The influence that catnip has on feline behavior is well-documented, and its use as an herbal supplement in competitive ninja circles remains controversial. Once again, the debate to ban the substance is brought to the public's attention after the high-profile win of Kittytron, a long-time proponent and user of the green stuff, at the Claw of Fury tournament.</p>
    <p>As I've stated in the past, I firmly believe a true ninja's skills must come from within, with no external influences. My own catnip use shall continue as purely recreational.</p>
  </article>
</body>

应用无障碍:通过使用充足的对比度避免色盲问题

颜色是可视化设计的重要组成部分,但是使用颜色也引入了两个可访问性问题。首先,不能仅仅使用颜色作为传达重要信息的唯一方式,因为屏幕阅读器无法获取这些信息。其次,前景色与背景色需要有足够的对比度,这样色盲用户才可以识别它们。

在之前的挑战中,我们用文本备用方案解决了第一个问题。在上一个挑战中,我们使用对比度检测工具解决了第二问题。WCAG 建议为颜色及灰度组合使用 4.5 : 1 的对比度。

色盲用户无法将一些颜色与另一些颜色区分出来——通常是因为色调,也有时候是因为亮度。你可能想起对比度是用前景色与背景色的相对亮度计算的。

实践中,在对比度检测工具的帮助下,我们可以将较暗的颜色变暗、将较淡的颜色变淡的方法来使对比度达到 4.5 : 1。在色轮中,较暗的颜色通常是蓝色、紫色、洋红和红色,而较淡的颜色通常是橙色、黄色、绿色和蓝绿色。

 


Camper Cat 正在尝试为他的博客文本与背景使用颜色,但是他目前使用的组合是绿色的background-color与栗色的color,它们的对比度为 2.5 : 1。Camper Cat 使用了 CSS 的hsl()(hsl为hue, saturation, lightness的缩写)属性来设置颜色,所以通过修改hsl()属性的第三个参数,可以很容易的调整颜色的亮度。请将background-color的亮度从 35% 增加到 55%,color的亮度从 20% 减少到 15%,这样可以使对比度达到 5.9 : 1。

<head>
  <style>
  body {
    color: hsl(0, 55%, 15%);
    background-color: hsl(120, 25%, 55%);
  }
  </style>
</head>
<body>
  <header>
    <h1>Deep Thoughts with Master Camper Cat</h1>
  </header>
  <article>
    <h2>A Word on the Recent Catnip Doping Scandal</h2>
    <p>The influence that catnip has on feline behavior is well-documented, and its use as an herbal supplement in competitive ninja circles remains controversial. Once again, the debate to ban the substance is brought to the public's attention after the high-profile win of Kittytron, a long-time proponent and user of the green stuff, at the Claw of Fury tournament.</p>
    <p>As I've stated in the past, I firmly believe a true ninja's skills must come from within, with no external influences. My own catnip use shall continue as purely recreational.</p>
  </article>
</body>

 

应用无障碍:通过仔细选择传达信息的颜色来避免色盲问题

色盲的形式有很多种,它的表现可以从对特定波长光波的感知度较低,到几乎无法看到颜色。最常见的形式是对绿色的低感知度。

例如:如果内容的前景色与背景色是两种相近的绿色,那么色盲用户可能会无法识别它们。可以将色轮上相邻的颜色认为是相近的,我们不应用这些颜色来表示重要的信息。

注意:
一些在线颜色拾取器有色盲模拟功能,可以模拟颜色在不同形式色盲的视觉中的呈现结果,它们和在线对比度检查器一样,都是很好的工具。

 


Camper Cat 正在用不同样式测试一个重要按钮。在色轮上,黄色(#FFFF33)的background-color和绿色(#33FF33)的文本color是相邻的色调,一些色盲用户几乎无法区分它们,而它们相近的亮度会使对比度测试失败。为了解决这两个问题,请将文本的color修改为深蓝色(#003366)。

<head>
  <style>
  button {
    color: #003366;
    background-color: #FFFF33;
    font-size: 14px;
    padding: 10px;
  }
  </style>
</head>
<body>
  <header>
    <h1>Danger!</h1>
  </header>
  <button>Delete Internet</button>
</body>

 

应用无障碍:使用描述性链接文本赋予链接含义

屏幕阅读器用户可以选择其设备读取的内容的类型,这包括跳转到(或跳过)标志标签,以便跳转到主要内容,或者从标题中获取页面摘要,还可以选择只听取页面中的有效链接。

屏幕阅读器通过阅读链接文本或者锚点标签(a)之间的内容来完成这个操作。拥有 "click here" 或者 "read more" 列表并没有什么用处。相反地,应该在a标签中使用简洁的描述性语言来为用户提供更多的信息。

 


Camper Cat 在链接中使用的文本在脱离上下文的情况下,描述性不是很好。请修改锚点标签(a),使其包含的文本从 "click here" 变为 "information about batteries"。

<body>
  <header>
    <h1>Deep Thoughts with Master Camper Cat</h1>
  </header>
  <article>
    <h2>Defeating your Foe: the Red Dot is Ours!</h2>
    <p>Felines the world over have been waging war on the most persistent of foes. This red nemesis combines both cunning stealth and lightening speed. But chin up, fellow fighters, our time for victory may soon be near. <a href="">information about batteries</a> for information about batteries</p>
  </article>
</body>

应用无障碍:通过给元素添加 accesskey 属性来让用户可以在链接之间快速导航

HTML 提供accesskey属性,用于指定激活标签或者使标签获得焦点的快捷键,这可以使键盘用户的导航更加有效。

HTML5 允许在任何标签上使用这个属性。该属性对于交互类标签(如链接、按钮、表单控件等)十分有用。

举个例子:

<button accesskey="b">Important Button</button>

 


Camper Cat 希望为他的两篇博客的标题的链接设置快捷键,以使用户可以快速导航到文章的全文。请为这两个链接添加accesskey属性,并将第一个设置为 "g"(表示 Garfield),第二个设置为 "c"(表示 Chuck Norris)。

<body>
  <header>
    <h1>Deep Thoughts with Master Camper Cat</h1>
  </header>
  <article>
    
    
    <h2><a id="first" href="" accesskey="g">The Garfield Files: Lasagna as Training Fuel?</a></h2>
    
    
    <p>The internet is littered with varying opinions on nutritional paradigms, from catnip paleo to hairball cleanses. But let's turn our attention to an often overlooked fitness fuel, and examine the protein-carb-NOM trifecta that is lasagna...</p>
  </article>
  <article>
    
    
    <h2><a id="second" href="" accesskey="c">Is Chuck Norris a Cat Person?</a></h2>
    
    
    <p>Chuck Norris is widely regarded as the premier martial artist on the planet, and it's a complete coincidence anyone who disagrees with this fact mysteriously disappears soon after. But the real question is, is he a cat person?...</p>
  </article>
  <footer>&copy; 2016 Camper Cat</footer>
</body>

应用无障碍:使用 tabindex 将键盘焦点添加到元素中

HTML 的tabindex属性有三个不同与标签焦点的功能。当它在标签上时,表示标签可以获得焦点。它的值可以是零、负整数及正整数,并决定了标签的行为。

当用户在页面中使用 tab 键时,有些标签,如:链接、表单控件,可以自动获得焦点。它们获得焦点的顺序与它们出现在文档流中的顺序一致。我们可以通过将tabindex属性值设为 0,来给其他标签赋予相同的功能,如:divspanp等。举个例子:

<div tabindex="0">I need keyboard focus!</div>

注意:
tabindex属性值为负整数(通常为 -1)的标签也是有焦点的,只是不可以通过 tab 键来获得焦点。这种方法通常用于以编程的方式使内容获得焦点(如:激活用于弹出框的div标签),但是它超出了当前挑战的范围。

 


Camper Cat 新建了一个调查,用来收集他的用户的信息。他知道输入框可以自动获得键盘焦点,但他希望确保当键盘用户切换标签时,焦点可以停留在指示文字上。请给p标签添加tabindex属性,并将它的值设置为 0。注意:使用tabindex属性可以使 CSS 伪类:focusp标签上生效。

<head>
  <style>
  p:focus {
    background-color: yellow;
  }
  </style>
</head>
<body>
  <header>
    <h1>Ninja Survey</h1>
  </header>
  <section>
    <form>
      
      
      <p tabindex="0">Instructions: Fill in ALL your information then click <b>Submit</b></p>
      
      
      <label for="username">Username:</label>
      <input type="text" id="username" name="username"><br>
      <fieldset>
        <legend>What level ninja are you?</legend>
        <input id="newbie" type="radio" name="levels" value="newbie">
        <label for="newbie">Newbie Kitten</label><br>
        <input id="intermediate" type="radio" name="levels" value="intermediate">
        <label for="intermediate">Developing Student</label><br>
        <input id="master" type="radio" name="levels" value="master">
        <label for="master">9th Life Master</label>
      </fieldset>
      <br>
      <fieldset>
      <legend>Select your favorite weapons:</legend>
      <input id="stars" type="checkbox" name="weapons" value="stars">
      <label for="stars">Throwing Stars</label><br>
      <input id="nunchucks" type="checkbox" name="weapons" value="nunchucks">
      <label for="nunchucks">Nunchucks</label><br>
      <input id="sai" type="checkbox" name="weapons" value="sai">
      <label for="sai">Sai Set</label><br>
      <input id="sword" type="checkbox" name="weapons" value="sword">
      <label for="sword">Sword</label>
      </fieldset>
      <br>
      <input type="submit" name="submit" value="Submit">
    </form><br>
  </section>
  <footer>&copy; 2016 Camper Cat</footer>
</body>

应用无障碍:使用 tabindex 指定多个元素的键盘焦点顺序

tabindex属性还可以指定标签的 tab 键顺序,将它的值设置为大于或等于 1 就可以实现这个功能。

tabindex属性值为 1 的标签将首先获得键盘焦点,然后焦点将按照指定的tabindex的值(如:2,3 等)的顺序进行移动,直到回到默认的或tabindex值为 0 的标签上,如此循环。

需要注意的是,当按照这种方式设置 tab 键顺序时,将会覆盖默认的顺序(标签在文档流中出现的顺序)。这可能会令那些希望从页面顶部开始导航的用户感到困惑。这个技术在某些情况下可能是必要的,但是对可访问性而言,在应用时要十分小心。

举个例子:

<div tabindex="1">I get keyboard focus, and I get it first!</div>

<div tabindex="2">I get keyboard focus, and I get it second!</div>

 


Camper Cat 在他的励志名言页面中有一个搜索区域,他打算使用 CSS 将这个区域定位在页面的右上角。Camper Cat 希望他的搜索input与提交input表单控件是 tab 键顺序的前两项。请为搜索input添加tabindex属性,其值为 1。为提交input添加tabindex属性,其值为 2。

<body>
  <header>
    <h1>Even Deeper Thoughts with Master Camper Cat</h1>
    <nav>
      <ul>
        <li><a href="">Home</a></li>
        <li><a href="">Blog</a></li>
        <li><a href="">Training</a></li>
      </ul>
    </nav>
  </header>
  <form>
    <label for="search">Search:</label>
    
    
    <input type="search" name="search" id="search" tabindex="1">
    <input type="submit" name="submit" value="Submit" id="submit" tabindex="2">
    
    
  </form>
  <h2>Inspirational Quotes</h2>
  <blockquote>
    <p>&ldquo;There's no Theory of Evolution, just a list of creatures I've allowed to live.&rdquo;<br>
    - Chuck Norris</p>
  </blockquote>
  <blockquote>
    <p>&ldquo;Wise men say forgiveness is divine, but never pay full price for late pizza.&rdquo;<br>
    - TMNT</p>
  </blockquote>
  <footer>&copy; 2016 Camper Cat</footer>
</body>

 

 

 

 

 

 

 

 

 

 

本文标签: 无障碍FreeCodeCamp