Accessibility in CSS (a11y)
Building accessible websites isn't just empathy—it's good engineering and law (WCAG 2.2). Master the superpower of :focus-visible, calculate mathematical color contrast ratios, and master the bulletproof .sr-only pattern.
1. The Keyboard Warrior: :focus vs. :focus-visible
The Old IssueThe :focus Pseudo-Class
Whenever a user clicks a button with a mouse, :focus applies. Designers hated the blue halo on mouse clicks and rashly wrote outline: none;, making the site unusable for blind/motor-impaired users navigating with keyboard Tab keys!
The Modern StandardThe :focus-visible Pseudo-Class
:focus-visible only activates when the browser detects keyboard navigation (pressing Tab or Arrow keys)! Mouse clicks remain clean, while keyboard users get bright, glowing navigation rings.
outline: 3px solid #3b82f6;
outline-offset: 4px;
}
2. The Bulletproof .sr-only Pattern (Screen Reader Only)
Never use display: none or visibility: hidden to hide visual icons. Screen readers completely drop elements with display: none from the accessibility accessibility tree! Use this standard 7-line utility instead:
3. Interactive Studio: Live Focus Ring & Contrast Analyzer
Accessibility Verification
Legibility allows users in bright sunlight, on low-end screens, or with visual impairments to consume your interface without squinting.
First-Year Student Traps: Accessibility
- 1.Relying Exclusively on Color for Error States: If an invalid input field only turns red without an error text icon or message, the 8% of male users with red-green colorblindness will have no idea why their form won't submit! Always pair color with an icon and clear message.
- 2.Ignoring Reduced Motion Preferences: Excessive parallax scrolling or bouncing hero animations can trigger genuine vertigo and nausea in users with vestibular disorders. Always wrap intense animations in
@media (prefers-reduced-motion: no-preference)!
Print Stylesheets & Clean PDF Generation
Format invoices and receipts for physical paper with @media print, page breaks, and @page margins.