HTML——通过创建小测验学习无障碍

**无障碍环境正在使你的网页便于所有人使用——甚至是残疾人。
在这个课程中,你将建立一个测验网页。你将学习诸如键盘快捷键、ARIA 属性和设计最佳实践等无障碍工具。**

步骤 1
欢迎来到无障碍测试题的第一部分。 你正在成为一个经验丰富的 HTML 和 CSS 开发者,我们从基本的模板带你开始学习这些知识。
通过为你的 html 元素添加一个 lang 属性来开始这段无障碍之旅。 这将帮助屏幕阅读器识别页面的语言。

<html lang="en">
  <head>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
  </body>
</html>

步骤 2
你可能已经熟悉 meta 元素;它用于指定有关页面的信息,例如标题、描述、关键词和作者。

给你的页面一个 meta 元素,然后给它的属性 charset 一个合适的值。

charset 属性指定了页面的字符编码,UTF-8 是现在大多数浏览器支持的唯一编码。

<head>
    <link rel="stylesheet" href="styles.css" />
    <meta charset="UTF-8">
  </head>

步骤 3
继续学习 meta 元素,viewport 定义告诉浏览器如何呈现页面。 包括改善移动设备上的视觉可访问性,并改进 SEO(搜索引擎优化)。

添加带有 content 属性的 viewport 定义,详细说明页面的 width 和 initial-scale。

 <head>
    <meta charset="UTF-8" />
    <link rel="stylesheet" href="styles.css" />
     <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  </head>

步骤 4
另一个关于可访问性和 SEO 的重要 meta 元素是 description 定义。 content 属性的值被搜索引擎用来提供你的页面的描述。

添加一个 meta 元素,并将 name 属性设置为 description,并为其添加一个有用的 content 属性。

 <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="styles.css" />
     <meta name="description" content="freeCodeCamp Accessibility Quiz practice project" />
  </head>

步骤 5
最后,在 head 元素中,title 元素对于屏幕阅读器理解页面内容很有用。 此外,它是 SEO 的重要组成部分。

给你的页面一个描述性的简洁的 title。

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="freeCodeCamp Accessibility Quiz practice project" />
    <link rel="stylesheet" href="styles.css" />
    <title>Accessibility Quiz</title>
  </head>

步骤 6
导航是无障碍的核心部分,屏幕阅读器依靠你提供你的页面结构。 这是通过语义 HTML 元素完成的。

将 header 元素和 main 元素添加到你的页面。

header 元素将用于介绍页面,以及提供导航菜单。

main 元素将包含页面的核心内容。

 <body>
    <header>
      </header>
      <main>
        </main>
  </body>

步骤 7
在 header 中,通过嵌套一个 img 元素、h1 元素和 nav 元素来提供有关页面的上下文。

img 元素应该指向 https://cdn.freecodecamp.org/platform/universal/fcc_primary.svg,并且有一个 id 为 logo。

h1 元素应该包含文本 HTML/CSS Quiz。

   <header>
<img id="logo" src="https://cdn.freecodecamp.org/platform/universal/fcc_primary.svg"> 
<h1>HTML/CSS Quiz</h1>
<nav></nav>
    </header>

步骤 8
SVG(可缩放矢量图形)的一个有用之处是它包含一个 path 属性,该属性允许在不影响图像分辨率的情况下缩放图像。

img 当前是默认尺寸,这个尺寸太大。 CSS has a max function which returns the largest of a set of comma-separated values. For example:

img {
width: max(250px, 25vw);
}
In this example, img elements will have a minimum width of 250px. And as the viewport grows, the image will grow accordingly to be 25 percent of the viewport width.

Scale the image using its id as a selector, and setting the width to be the maximum of 100px or 18vw.

 #logo{
  width: max(100px, 18vw);
}

步骤 9
如 freeCodeCamp 样式指南中所述,logo 应保持 35 / 4 的长宽比,并且文本周围有 padding。

首先,将 background-color 设置为 #0a0a23,以便你能看见 logo。 然后,使用 aspect-ratio 属性将所需的长宽比设置为 35 / 4。 最后,在周围添加 0.4rem 的 padding。

#logo {
  width: max(100px, 18vw);
  background-color: #0a0a23;
  aspect-ratio: 35 / 4;
  padding: 0.4rem;
}

步骤 10
使 header 占用其父容器的全宽,设置它的 height 为 50px,并将属性 background-color 设置为 #1b1b32。 然后设置 display 属性时使用 Flexbox。

header {
  width: 100%;
  height: 50px;
  background-color: #1b1b32;
  display: flex;  
}

步骤 11
将 h1 字体颜色更改为 #f1be32,并将字体大小设置为 min(5vw, 1.2em)。

h1 {
  color: #f1be32;
  font-size: min(5vw, 1.2em);
}

步骤 12
要在页面上启用导航,请添加一个包含以下三个列表项的无序列表:

INFO
HTML
CSS
列表项文本应包含在锚标记中。

<nav>
 <ul>
          <li><a >INFO</a></li>
          <li><a>HTML</a></li>
          <li><a>CSS</a></li>
        </ul>
      </nav>

步骤 14
由于这是一个测试题,你需要给用户提供一个表单以便他们提交答案。 你可以使用 section 元素在语义上分隔表单中的内容。

在 main 元素中,创建一个包含三个嵌套 section 元素的表单。 然后,使用正确的方法将表单提交到 https://freecodecamp.org/practice-project/accessibility-quiz。

  <main>
        <form method="post" action="https://freecodecamp.org/practice-project/accessibility-quiz">
<section>
    </section>
    <section>
  </section>
  <section>
</section>
    </main>

步骤 15
为了增加页面的可访问性,role 属性可用于向辅助技术指示页面上元素背后的用途。 role 属性是 Web Accessibility Initiative (WAI,Web 可访问性倡议)的一部分,并接受预设值。

为每个 section 元素设置 role 为 region。

 <form method="post" action="https://freecodecamp.org/practice-project/accessibility-quiz">
        <section role="region"></section>
        <section role="region"></section>
        <section role="region"></section>
      </form>

步骤 16
每个 region role 都需要一个标签,帮助屏幕阅读者理解 region 的目的。 一个添加标签的方法是在 region 内添加一个 heading 元素,然后使用 aria-labelledby 属性来引用它。

给 section 元素添加以下 aria-labelledby 属性:

student-info
html-questions
css-questions
然后,在每个 section 元素中,嵌套一个 h2 元素,其 id 与相应的 aria-labelledby 属性匹配。 给每个 h2 添加适当的文本内容。

 <form method="post" action="https://freecodecamp.org/practice-project/accessibility-quiz">
        <section role="region" aria-labelledby="student-info">
          <h2 id="student-info">Student Info</h2>
          </section>
        <section role="region" aria-labelledby="html-questions">
          <h2 id="html-questions">HTML</h2>
                   </section>
         <section role="region" aria-labelledby="css-questions">
          <h2 id="css-questions">CSS</h2>
                   </section>
      </form>

步骤 17
Typeface 在页面的可访问性方面发挥着重要作用。 有些字体比其他字体更容易阅读,在低分辨率屏幕上尤其如此。

将 h1 和 h2 元素的字体更改为 Verdana,并使用 sans-serif 系列中的另一种网络安全字体作为后备。

此外,给 h2 元素 添加 4px solid #dfdfe2 的 border-bottom,以使各部分区分开来。

h1,
h2 {
  font-family: Verdana, Tahoma;
}

h2 {
  border-bottom: 4px solid #dfdfe2;
}

步骤 18
为了能够在页面内导航,给每个锚元素一个与 h2 元素的 id 相对应的 href。

<li><a href="#student-info">INFO</a></li>
          <li><a href="#html-questions">HTML</a></li>
          <li><a href="#css-questions">CSS</a></li>

步骤 19
填写测验内容,在 #student-info下添加三个 class 为 info 的 div 元素

然后,在每个 div 内嵌套一个 label 元素和一个 input 元素。

      <section role="region" aria-labelledby="student-info">
          <h2 id="student-info">Student Info</h2>
<div class="info">
  <label> </label>
  <input>
    </div>
<div class="info">
  <label> </label>
  <input>
    </div>
<div class="info">
 <label> </label>
  <input>
    </div>
        </section>

步骤 20
将每个 input 链接到相应的 label 元素很重要。 这为辅助技术用户提供了对输入的视觉参考。

这是通过给 label 一个 for 属性来实现的,该属性包含 input 的 id。

这个部分将填写学生的名字、电子邮件地址和出生日期。 为 label 元素提供适当的 for 属性以及文本内容。 然后,将 input 元素链接到相应的 label 元素。

<section role="region" aria-labelledby="student-info">
          <h2 id="student-info">Student Info</h2>
          <div class="info">
            <label for="student-name">Name:</label>
            <input id="student-name" />
          </div>
          <div class="info">
            <label for="student-email">Email:</label>
            <input id="student-email" />
          </div>
          <div class="info">
            <label for="birth-date">D.O.B.:</label>
            <input id="birth-date" />
          </div>
        </section>
   <section role="region" aria-labelledby="student-info">
          <h2 id="student-info">Student Info</h2>
          <div class="info">
            <label for="student-name">Name:</label>
            <input type="text" name="student-name" id="student-name" placeholder="student-name"/>
          </div>
          <div class="info">
            <label for="student-email">Email:</label>
            <input type="email" name="student-email" id="student-email" />
          </div>
          <div class="info">
            <label for="birth-date">D.O.B.:</label>
            <input type="date" name="birth-date" id="birth-date" />
          </div>
        </section>

步骤 23
可以说,D.O.B. 的描述性不够。 对于视力受损的用户来说尤其如此。 解决此类问题的一种方法是添加只有屏幕阅读器可以阅读的文本,而无需向标签添加可见文本。

将具有 sr-only 类的 span 元素添加到第三个 label 元素的当前文本内容。

       <div class="info">
            <label for="birth-date">D.O.B.<span class="sr-only"></span></label>
            <input type="date" name="birth-date" id="birth-date" />
          </div>

步骤 25
.sr-only 文本仍然可见。 有一种常见的模式可以在视觉上隐藏文本以供屏幕阅读器阅读。

.sr-only{
  position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}

步骤 26
在第二个 section 元素中,添加两个具有 question-block 类的 div 元素。

然后,在每个 div.question-block 元素中,添加一个带有递增数字文本、从 1 开始的 p 元素,以及一个具有 question 类的 fieldset 元素。

  <section role="region" aria-labelledby="html-questions">
          <h2 id="html-questions">HTML</h2>
<div class="question-block"> <p>1</p>
            <fieldset class="question" name="html-question-one">
            </div>
<div class="question-block"> <p>2</p>
            <fieldset class="question" name="html-question-one">
            </div>
        </section>

步骤 27
每个 fieldset 将包含一个真/假问题。

在每个 fieldset 中,嵌套一个 legend 元素和一个带有两个选项的 ul 元素。

 <section role="region" aria-labelledby="html-questions">
          <h2 id="html-questions">HTML</h2>
          <div class="question-block">
            <p>1</p>
            <fieldset class="question">
              <legend></legend>
              <ul>
                <li></li>
                <li></li>
              </ul>
            </fieldset>
          </div>
          <div class="question-block">
            <p>2</p>
            <fieldset class="question">
              <legend></legend>
              <ul>
                <li></li>
                <li></li>
              </ul>
            </fieldset>
          </div>
        </section>

步骤 28-33
给每个 fieldset 一个适当的 name 属性。 然后,给两个无序列表添加 class 为 answers-list。

最后,使用 legend 为 fieldset 的内容添加标题,方法是将真/假问题作为文本内容。

 <section role="region" aria-labelledby="html-questions">
          <h2 id="html-questions">HTML</h2>
          <div class="question-block">
            <p>1</p>
            <fieldset class="question" name="html-question-one">
              <legend>
                The legend element represents a caption for the content of its
                parent fieldset element
              </legend>
              <ul class="answers-list">
                <li>
                  <label for="q1-a1">
                    <input type="radio" id="q1-a1" name="q1" value="true" />
                    True
                  </label>
                </li>
                <li>
                  <label for="q1-a2">
                    <input type="radio" id="q1-a2" name="q1" value="false" />
                    False
                  </label>
                </li>
              </ul>
            </fieldset>
          </div>
          <div class="question-block">
            <p>2</p>
            <fieldset class="question" name="html-question-two">
              <legend>
                A label element nesting an input element is required to have a
                for attribute with the same value as the input's id
              </legend>
              <ul class="answers-list">
                <li>
                  <label for="q2-a1">
                    <input type="radio" id="q2-a1" name="q2" value="true" />
                    True
                  </label>
                </li>
                <li>
                  <label for="q2-a2">
                    <input type="radio" id="q2-a2" name="q2" value="false" />
                    False
                  </label>
                </li>
              </ul>
            </fieldset>
          </div>
        </section>

步骤 34
为了防止不必要的重复,针对 p 元素的 before 伪元素,给它 一个值为 "Question #" 的 content 属性。

p::before {
  content: "Question #";
}

步骤 35
本测验的最后一部分将包含一个下拉列表和一个文本框。

首先将 div 与 formrow 的 class 嵌套,并在其中嵌套四个 div 元素,交替添加它们class 属性为 question-block 和 answer。

     <section role="region" aria-labelledby="css-questions">
          <h2 id="css-questions">CSS</h2>
          <div class="formrow">
            <div class="question-block">
            </div>
            <div class="answer">
            </div>
            <div class="question-block">
            </div>
            <div class="answer">
            </div>
          </div>
        </section>

步骤 36
Within the div.question-block elements, nest one label element, and add a CSS related question to the label text.


        <section role="region" aria-labelledby="css-questions">
          <h2 id="css-questions">CSS</h2>
          <div class="formrow">
            <div class="question-block">
              <label>CSS</label>
            </div>
            <div class="answer">
            </div>
            <div class="question-block">
                  <label>CSS</label>
            </div>
            <div class="answer">
            </div>
          </div>
        </section>

步骤 37
在第一个 div.answer 元素中,嵌套一个必需的 select 元素和三个 option 元素。

给第一个 option 元素一个 value 为 "",以及文本 Select an option。 给第二个 option 元素一个 value 为 yes,以及文本 Yes。 给第三个 option 元素一个 value 为 no,以及文本 No。

 <div class="answer">
 <select name="customer" id="customer" required>
                <option value="">Select an option</option>
                <option value="yes">Yes</option>
                <option value="no">No</option>
            </div>

步骤 39
在第二个 div.answer 元素中嵌套一个 textarea 元素,并设置它的行数和列数。

然后,给 textarea 添加描述示例答案的占位符文本。

            <div class="answer">
 <textarea rows="5" cols="24" placeholder="Who is flexbox..."></textarea>
            </div>

步骤 40
与其他 input 和 label 元素一样,将 textarea 链接到其对应的 label 元素,并给它一个 name 属性。

  <div class="question-block">
              <label for="css-questions">Do you have any questions:</label>
            </div>
            <div class="answer">
               <textarea id="css-questions" name="css-questions" rows="5" cols="24" placeholder="Who is flexbox..."></textarea>
            </div>

步骤 41
不要忘记给你的 form 一个提交按钮,文本为 Send。
<button type="submit">Send</button>
步骤 42
该项目的最后的两个语义 HTML 元素是 footer 和 address 元素。 footer 元素是与页面相关的内容集合的容器,address 元素是页面作者的联系信息的容器。

在 main 元素之后,添加一个 footer 元素,并在其中嵌套一个 address 元素。

 <footer>
      <address>
      
      </address>
    </footer>

步骤 45
继续为页面添加样式。 选择导航栏中的列表元素,并给它们添加下面的样式:


nav > ul > li {
  color: #dfdfe2;
  margin: 0 0.2rem;
  padding: 0.2rem;
  display: block;
}

步骤 47
要使导航按钮看起来更像典型按钮,从锚点标签中移除下划线。

然后,创建一个针对导航列表元素的新选择器,以便当光标悬停在它们上方时, 背景颜色和文字颜色被切换,光标变成指针。


nav > ul > li:hover {
  background-color: #dfdfe2;
  color: #1b1b32;
  cursor: pointer;
}

li > a {
  color: inherit;
  text-decoration: none;
}

步骤 48
整理 header,使用 Flexbox 在子元素之间添加空格,并将它们垂直居中。

然后,将 header 固定到视口的顶部。

header {
  width: 100%;
  height: 50px;
  background-color: #1b1b32;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: fixed;
  top: 0;
}

步骤 49
当屏幕宽度较小时,h1 不会以应有的方式包含其文本内容。 将 h1 元素的文本居中对齐。

然后,给 main 设置 padding,以便可以完全看到 Student Info 的标题。

main {
  padding-top: 50px;
}

h1 {
  color: #f1be32;
  font-size: min(5vw, 1.2em);
  text-align: center;
}

步骤 50
在小屏幕上,导航栏中的无序列表会溢出屏幕的右侧。

通过使用 Flexbox 来包含 ul 内容来解决此问题。 然后,设置以下 CSS 属性以正确对齐文本:

nav > ul {
  display: flex;
  justify-content: space-evenly;
  flex-wrap: wrap;
  align-items: center;
  padding-inline-start: 0;
  margin-block: 0;
  height: 100%;
}

步骤 51
将 section 元素的宽度设置为其父容器的 80%。 然后使用 margin 来居中 section 元素,将底部 margin 设置为 10px。

另外,确认 section 元素的宽度不能大于 600px。

section {
  width: 80%;
  margin: 0 auto 10px auto;
  max-width: 600px;
}

section {
width: 80%;
margin: 0 auto 10px auto;
max-width: 600px;
}

h2 {
  border-bottom: 4px solid #dfdfe2;
  margin-top: 0px;
  padding-top: 60px;
}

步骤 54
为 .formrow 元素设置顶部 margin、左侧 padding 和右侧 padding。 其他 padding 的值应为 0。

然后,增加所有 input 元素的字体大小。

.formrow {
  margin-top: 30px;
  padding: 0px 15px;
}

input {
  font-size: 16px;
}

步骤 55
为了使第一部分看起来更像内联,只针对 .info元素中的 input 元素,将其 width 设置为 50%,并将其文本左对齐。

.info input {
  width: 50%;
  text-align: left;
}

骤 56
定位 .info 元素中的所有 label 元素,并将它们的 width 设置为 10%,并使其不小于 55px。

.info label {
  width: 10%;
  min-width: 55px;
  text-align: right;
}

步骤 57
要使输入框相互对齐,在 .info 中为所有 input 和 label 元素创建一个新的规则集合,并设置 display 属性为 inline-block。

另外,将 label 元素的文本向右对齐。

.info label, .info input {
  display: inline-block;
  text-align:right;
}

步骤 61
While ul/li elements are great at providing bullets for list items, your radio buttons don't need them. You can control what the bullets look with the list-style property. For example you can turn your bullets into circles with the following:

ul {
list-style: circle;
}
Remove the default styling for the .answers-list items by setting its style to none, and remove the unordered list padding.


.answers-list {
  list-style: none;
  padding: 0;
}

步骤 66
单击导航链接应将视口跳转到相关部分。 然而,这种跳转可能会让一些用户感到迷惑。

选择所有元素,并将 scroll-behavior 设置为 smooth。


  * {
    scroll-behavior: smooth;
  }

步骤 68
最后,可以通过提供键盘快捷键来改进导航的可访问性。

accesskey 属性接受以空格分隔的访问键列表。 例如:

<button type="submit" accesskey="s">Submit</button>
为每个导航链接提供一个单字母访问键。

注意:并不总是建议使用访问键,但它们可能很有用

很棒, 你已经完成_无障碍测试_练习项目。

 <ul>
          <li><a accesskey="s" href="#student-info">INFO</a></li>
          <li><a accesskey="s" href="#html-questions">HTML</a></li>
          <li><a accesskey="s" href="#css-questions">CSS</a></li>
        </ul>

评论

暂无评论