之所以合理,是因为 <button> 标签不仅提供了按钮的样式,还提供了键盘的无障碍访问。例如,使用 Tab 更换按钮,使用 Enter 点击按钮。
<button> 默认提供的样式大多数情况下需要重新编写
文本内容
文本内容放在良好的结构中,避免使用表现性 HTML 和换行符。
✅Good
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<h1>My heading</h1> <p>This is the first section of my document.</p> <p>I'll add another paragraph here too.</p> <ol> <li>Here is</li> <li>a list for</li> <li>you to read</li> </ol> <h2>My subheading</h2> <p> This is the first subsection of my document. I'd love people to be able to find this content! </p> <h2>My 2nd subheading</h2> <p> This is the second subsection of my content. I think is more interesting than the last one. </p>
<fontsize="7">My heading</font><br /><br /> This is the first section of my document. <br /><br /> I'll add another paragraph here too. <br /><br /> 1. Here is <br /><br /> 2. a list for <br /><br /> 3. you to read <br /><br /> <fontsize="5">My subheading</font> <br /><br /> This is the first subsection of my document. I'd love people to be able to find this content! <br /><br /> <fontsize="5">My 2nd subheading</font> <br /><br /> This is the second subsection of my content. I think is more interesting than the last one.
<header> <h1>Header</h1> </header> <nav> <!-- main navigation in here --> </nav> <!-- Here is our page's main content --> <main> <!-- It contains an article --> <article> <h2>Article heading</h2> <!-- article content in here --> </article> <aside> <h2>Related</h2> <!-- aside content in here --> </aside> </main> <!-- And here is our main footer that is used across all the pages of our website --> <footer> <!-- footer content in here --> </footer>
UI 控制
有意义的文本。
确保按钮和链接的文本是可以理解和独特的。
✅Good
1 2 3 4
<p> Whales are really awesome creatures. <ahref="whales.html">Find out more about whales</a>. </p>
❌Bad
1 2 3 4
<p> Whales are really awesome creatures. To find more out about whales, <ahref="whales.html">click here</a>. </p>
表单
✅Good
1 2 3 4 5 6 7 8
<div> <labelfor="name">Fill in your name:</label> <input type="text" id="name" name="name" /> </div>
❌Bad
1 2 3 4 5 6
Fill in your name: <input type="text" id="name" name="name" />
<img src="dinosaur.png" alt="A red Tyrannosaurus Rex: A two legged dinosaur standing upright like a human, with small arms, and a large head with lots of sharp teeth." />
<img src="dinosaur.png" alt="A red Tyrannosaurus Rex: A two legged dinosaur standing upright like a human, with small arms, and a large head with lots of sharp teeth." title="The Mozilla red dinosaur" />
<pid="dino-label"> The Mozilla red Tyrannosaurus Rex: A two legged dinosaur standing upright like a human, with small arms, and a large head with lots of sharp teeth. </p>
第一张图片没有提供文字描述
第二张图片的 alt 属性会被屏幕阅读器读到
第三张图片提供了额外的 title 属性
第四张图片没有提供 alt 属性,但是通过 aria-labelledby 属性将页面中已经存在的文本作为图片描述