HTML Accessibility
制作 web pages accessible 使用 ARIA, alt 文本, keyboard nav, and 语义 markup.
你将学到
- 什么 accessibility (a11y) 是 and 为什么它 是必要
- 如何 use alt 文本, labels, and 语义标签 for assistive tech
- 如何 ARIA attributes fill gaps 当 HTML alone 是不 enough
- 如何 测试 keyboard 导航 and focus management
概念讲解
HTML Accessibility
Accessibility (often abbreviated a11y — 11 letters between "a" and "y") means building web pages that everyone can use, including people with visual, motor, cognitive, or auditory disabilities. An accessible page works with screen readers, keyboard-only navigation, voice control, and high-contrast modes. Accessibility is not an optional enhancement — it is a legal requirement in many countries and a moral obligation everywhere.
The Basics: Alt, Labels, and Semantics
The foundation of accessibility is correct HTML. Provide alt text on every image so screen readers can describe it. Connect <label> to every form input. Use semantic tags (<nav>, <主要>, <article>) so assistive technology can navigate by landmarks. These three habits alone make a page dramatically more accessible than the average.
ARIA Attributes
ARIA (Accessible Rich Internet Applications) attributes add accessibility information that HTML cannot express on its own. Use role to declare an element's purpose, aria-label for a text alternative, aria-hidden to hide decorative elements from screen readers, and aria-live to announce dynamic updates. But ARIA is a last resort — the first rule of ARIA is "no ARIA is better than bad ARIA." If a native HTML element does the job, use it instead.
Keyboard Navigation
Many users cannot use a mouse. Every interactive element must be reachable with the Tab key and operable with Enter or Space. Links and buttons do this automatically. Custom widgets (like a custom dropdown) need tabindex and key event handlers. Never remove the focus outline (outline: 无) without providing a visible alternative — invisible focus states make keyboard navigation impossible.
Testing Accessibility
Test with the keyboard first: unplug your mouse and try to use your page. Then run an automated checker like Lighthouse or axe. Finally, test with a real screen reader (NVDA on Windows, VoiceOver on Mac). Automated tools catch about 30% of issues; the rest need human judgment. Accessibility is a process, not a checkbox.
示例代码
<nav aria-label="Main navigation">
<a href="/">Home</a>
<a href="/products">Products</a>
</nav>
<main>
<h1>Welcome to Our Store</h1>
<img src="banner.jpg" alt="Summer sale — up to 50% off" />
<button aria-label="Close dialog" onclick="closeDialog()">
×
</button>
<div role="alert" aria-live="assertive">
<!-- Screen readers announce changes here automatically -->
<p id="status">Item added to cart.</p>
</div>
</main>
aria-label names nav for screen readers. alt 文本描述 banner 镜像. 关闭 button 有文本 label 即使它 仅 shows ×. role='alert' 使用 aria-live announces 动态更新到 assistive technology.
动手试一试
- HTML ↔ Markdown
检查那个你的 accessible HTML converts cleanly 到 Markdown and 后面.
- Markdown Previewer
Preview 内容到 验证 heading 结构 and 读取顺序 for screen readers.
常见错误
错误写法
Using alt="" on meaningful 镜像 or omitting alt 完全.
正确做法
每个 <img> 需要 alt. Use descriptive 文本 for meaningful 镜像. Use alt="" (空) 仅 for purely decorative 镜像那个 screen readers 应该 skip.
错误写法
移除 focus outline 使用 outline: 无 and 否 replacement.
正确做法
Keyboard 用户需要可见 focus indicator 到 know 哪里它们是. 如果你 移除默认 outline, 添加自定义 one (e.g., box-shadow) 那个是 清晰地可见.
相关资源
相关速查表
- HTML Cheatsheet
引用 for 语义标签 and ARIA attributes.
练习 HTML Accessibility
2 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。