Complete guide to building accessible web applications that meet WCAG 2.2 standards.
WCAG 2.2 (Web Content Accessibility Guidelines) has three conformance levels:
Target: WCAG 2.2 Level AA compliance for all projects.
Four Principles (POUR):
Text Contrast:
UI Components:
Tools:
Examples:
/* ✅ Good: 7:1 ratio */
color: #000000;
background: #ffffff;
/* ✅ Good: 4.5:1 ratio */
color: #595959;
background: #ffffff;
/* ❌ Bad: 2.5:1 ratio (fails AA) */
color: #999999;
background: #ffffff;
Every interactive element must be keyboard-accessible:
Logical tab order matches visual layout:
// ✅ Good: Natural DOM order
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
</nav>
// ❌ Bad: Using tabIndex to override
<nav>
<a href="/" tabIndex={3}>Home</a>
<a href="/about" tabIndex={1}>About</a>
<a href="/contact" tabIndex={2}>Contact</a>
</nav>
Only use tabIndex in three cases:
tabIndex={0}: Add to tab order (for custom interactive elements)tabIndex={-1}: Remove from tab order (programmatic focus only)tabIndex={1+}: Override order (avoid unless absolutely necessary)All interactive elements must have visible focus:
/* ✅ Good: Visible focus outline */
button:focus-visible {
outline: 3px solid #3b82f6;
outline-offset: 2px;
}
/* ❌ Bad: Removing focus without replacement */
button:focus {
outline: none;
}
/* ✅ Good: Custom focus style */
button:focus-visible {
outline: none;
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.5);
}
Focus indicators must:
Provide skip links for keyboard users:
<a
href="#main-content"
className="sr-only focus:not-sr-only focus:absolute focus:top-4 focus:left-4 focus:z-50 focus:px-4 focus:py-2 focus:bg-blue-600 focus:text-white"
>
Skip to main content
</a>
<main id="main-content">
{/* Page content */}
</main>
Always use semantic elements:
// ✅ Good: Semantic HTML
<nav>
<ul>
<li><a href="/">Home</a></li>
</ul>
</nav>
<main>
<article>
<h1>Title</h1>
<p>Content</p>
</article>
</main>
// ❌ Bad: Div soup
<div class="nav">
<div class="nav-item">
<div class="link" onclick="navigate()">Home</div>
</div>
</div>
<div class="main">
<div class="article">
<div class="title">Title</div>
<div class="content">Content</div>
</div>
</div>
Use proper heading levels (h1-h6):
// ✅ Good: Logical hierarchy
<h1>Page Title</h1>
<h2>Section 1</h2>
<h3>Subsection 1.1</h3>
<h2>Section 2</h2>
<h3>Subsection 2.1</h3>
// ❌ Bad: Skipping levels
<h1>Page Title</h1>
<h4>Section 1</h4>
One h1 per page (page title).
Every image must have alt text:
// ✅ Good: Descriptive alt text
<img src="logo.png" alt="Acme Corporation logo" />
<img src="chart.png" alt="Bar chart showing 50% increase in sales from 2024 to 2025" />
// ❌ Bad: Generic or missing alt
<img src="logo.png" alt="image" />
<img src="chart.png" />
// ✅ Good: Decorative images (empty alt)
<img src="decorative-line.png" alt="" />
Alt text rules:
alt="") for decorative imagesTrap focus inside modals:
"use client"
import { useEffect, useRef } from "react"
import { Dialog, DialogContent } from "@/components/ui/dialog"
export function AccessibleDialog({ isOpen, onClose, children }) {
const closeButtonRef = useRef(null)
useEffect(() => {
if (isOpen && closeButtonRef.current) {
closeButtonRef.current.focus()
}
}, [isOpen])
return (
<Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent onEscapeKeyDown={onClose}>
<button
ref={closeButtonRef}
onClick={onClose}
aria-label="Close dialog"
>
×
</button>
{children}
</DialogContent>
</Dialog>
)
}
Announce dynamic changes:
// Live region for status updates
<div role="status" aria-live="polite" aria-atomic="true">
{statusMessage}
</div>
// Alert for errors
<div role="alert" aria-live="assertive">
{errorMessage}
</div>
aria-live values:
polite: Announce when screen reader is idleassertive: Interrupt immediatelyoff: Don't announceLabels:
// aria-label: Provides label when no visible text
<button aria-label="Close menu">
<XIcon />
</button>
// aria-labelledby: References visible label
<h2 id="dialog-title">Delete Account</h2>
<div role="dialog" aria-labelledby="dialog-title">
...
</div>
// aria-describedby: Additional description
<input
id="password"
aria-describedby="password-requirements"
/>
<p id="password-requirements">
Password must be at least 8 characters
</p>
States:
// aria-expanded: Dropdown/accordion state
<button aria-expanded={isOpen}>
Menu
</button>
// aria-selected: Selected item
<div role="tab" aria-selected={isActive}>
Tab 1
</div>
// aria-checked: Checkbox/radio state
<div role="checkbox" aria-checked={isChecked}>
Accept terms
</div>
// aria-disabled: Disabled state
<button aria-disabled="true">
Submit
</button>
Relationships:
// aria-controls: Element controls another
<button aria-controls="menu-panel" aria-expanded={isOpen}>
Open Menu
</button>
<div id="menu-panel">
Menu content
</div>
// aria-owns: Parent-child relationship
<div role="listbox" aria-owns="option1 option2 option3">
<div id="option1" role="option">Option 1</div>
<div id="option2" role="option">Option 2</div>
<div id="option3" role="option">Option 3</div>
</div>
Only use ARIA roles when semantic HTML isn't available:
// ✅ Good: Semantic HTML (no ARIA needed)
<button>Click Me</button>
<nav>...</nav>
// ⚠️ Acceptable: When no semantic option exists
<div role="button" tabIndex={0} onClick={handleClick} onKeyDown={handleKeyDown}>
Custom Button
</div>
// ❌ Bad: Redundant ARIA
<button role="button">Click Me</button>
Every input must have a label:
// ✅ Good: Explicit label association
<label htmlFor="email">Email Address</label>
<input id="email" type="email" />
// ✅ Good: Wrapping label
<label>
Email Address
<input type="email" />
</label>
// ❌ Bad: Placeholder as label
<input type="email" placeholder="Email Address" />
Associate errors with inputs:
<div>
<label htmlFor="email">Email</label>
<input
id="email"
type="email"
aria-invalid={hasError}
aria-describedby={hasError ? "email-error" : undefined}
/>
{hasError && (
<p id="email-error" role="alert" className="text-red-600">
Please enter a valid email address
</p>
)}
</div>
Indicate required fields:
<label htmlFor="name">
Name <span aria-label="required">*</span>
</label>
<input id="name" required aria-required="true" />
Use buttons for actions, links for navigation:
// ✅ Good: Button for action
<button onClick={handleSubmit}>Submit Form</button>
// ✅ Good: Link for navigation
<a href="/about">About Us</a>
// ❌ Bad: Link styled as button for action
<a href="#" onClick={handleSubmit}>Submit Form</a>
// ❌ Bad: Button for navigation
<button onClick={() => navigate('/about')}>About Us</button>
Make divs keyboard-accessible:
// ❌ Bad: Div as button (not keyboard accessible)
<div onClick={handleClick}>Click Me</div>
// ✅ Good: Proper keyboard support
<div
role="button"
tabIndex={0}
onClick={handleClick}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault()
handleClick()
}
}}
>
Click Me
</div>
// ✅ Better: Just use a button
<button onClick={handleClick}>Click Me</button>
axe DevTools (Chrome Extension)
Lighthouse (Chrome DevTools)
WAVE (WebAIM)
Keyboard Navigation
Screen Reader
Zoom Test
Color Blindness Simulator
Before Launch:
Common Gotchas:
Remember: Accessibility is not optional. It's a legal requirement (ADA, Section 508) and ethical responsibility. 1 in 4 adults in the US has a disability.
Test early, test often. Don't wait until launch to check accessibility.