Accessible CSS
Focus styles, contrast, reduced motion, and screen reader support.
Overview
Accessible CSS ensures content is usable by everyone.
Key Concepts
- Focus Styles — Visible focus indicators
- Color Contrast — Readable text
- Reduced Motion — Respect user preferences
- Screen Reader — Visually hidden content
- Skip Links — Navigation shortcuts
Code Examples
/* Focus styles */
:focus {
outline: 2px solid var(--primary);
outline-offset: 2px;
}
:focus:not(:focus-visible) {
outline: none;
}
:focus-visible {
outline: 2px solid var(--primary);
outline-offset: 2px;
}
/* Skip link */
.skip-link {
position: absolute;
top: -100%;
left: 0;
background: var(--primary);
color: white;
padding: 0.5rem 1rem;
z-index: 1000;
}
.skip-link:focus {
top: 0;
}
/* Visually hidden */
.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;
}
/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
/* High contrast */
@media (prefers-contrast: high) {
:root {
--text: #000000;
--bg: #ffffff;
--primary: #0000ff;
}
.border {
border: 2px solid #000000;
}
}
/* Color contrast */
.text-contrast {
color: #333333; /* 12.6:1 contrast on white */
background: #ffffff;
}
/* Focus within */
.form-group:focus-within {
border-color: var(--primary);
}
/* Touch targets */
.button {
min-height: 44px;
min-width: 44px;
padding: 0.5rem 1rem;
}
/* Readable line lengths */
.prose {
max-width: 65ch;
line-height: 1.6;
}
/* Print styles */
@media print {
.no-print {
display: none !important;
}
body {
font-size: 12pt;
color: #000;
background: #fff;
}
a {
color: #000;
text-decoration: underline;
}
}
/* Dark mode accessibility */
@media (prefers-color-scheme: dark) {
:root {
--text: #e0e0e0;
--bg: #121212;
}
.contrast-warning {
color: #ffff00; /* High contrast yellow */
}
}
Practice
Audit a website for accessibility and fix CSS issues.