CSS Best Practices

Accessibility in CSS

Create inclusive designs that work for everyone through accessible CSS practices and WCAG compliance.

How does CSS affect accessibility?

CSS can make or break accessibility. Keep a visible focus indicator (:focus-visible), keep text contrast at 4.5:1 or more, size text in rem so zoom works, respect prefers-reduced-motion, and never convey meaning by colour alone. Hide content visually with a .sr-only class rather than display: none when screen readers should still hear it.

:focus-visible { outline: 3px solid #2563eb; outline-offset: 2px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition: none !important; }
}

.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;
}

Accessible CSS in Practice

Code Editor

<a class="skip" href="#main">Skip to content</a>
<nav><a href="#">Home</a> <a href="#">Courses</a></nav>
<main id="main">
  <button>Press Tab to see the focus ring</button>
  <p class="sr-only">This text is only for screen readers.</p>
  <p class="low">Low contrast text is hard to read.</p>
  <p class="good">Good contrast text is easy to read.</p>
</main>

Preview

Visual Accessibility

Color Contrast Requirements

/* WCAG Color Contrast Guidelines */
:root {
  /* WCAG AA: 4.5:1 ratio for normal text */
  --color-text: #111827; /* 15.8:1 on white */
  --color-primary: #2563eb; /* 4.62:1 on white */
  --color-secondary: #64748b; /* 4.54:1 on white */

  /* WCAG AA Large Text: 3:1 ratio */
  --color-text-light: #6b7280; /* 4.5:1 on white */

  /* WCAG AAA: 7:1 ratio (enhanced) */
  --color-text-aaa: #1f2937; /* 12.6:1 on white */
}

/* Test contrast with tools: */
/* - WebAIM Contrast Checker */
/* - Chrome DevTools */
/* - Colour Contrast Analyser */

/* Don't rely on color alone */
.error {
  color: #dc2626; /* Red */
  border-left: 4px solid #dc2626; /* Visual indicator */
  background: #fef2f2; /* Background context */
}

.error::before {
  content: "⚠️ "; /* Icon for context */
}

Always ensure sufficient contrast ratios and don't rely solely on color to convey information.

Typography Accessibility

/* Readable typography */
body {
  font-size: 1rem; /* Minimum 16px */
  line-height: 1.6; /* Minimum 1.5 */
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
}

/* Optimal reading width */
p {
  max-width: 65ch; /* 45-75 characters */
}

/* Clear hierarchy */
h1 { font-size: 1.875rem; }
h2 { font-size: 1.5rem; }
h3 { font-size: 1.25rem; }

/* Avoid justified text */
.text-content {
  text-align: left; /* Not justify */
}

Use readable fonts, appropriate sizes, and optimal line spacing

Focus Indicators

/* Visible focus styles */
*:focus {
  outline: 2px solid #3b82f6;
  outline-offset: 2px;
}

/* Enhanced focus for interaction */
button:focus,
a:focus,
input:focus {
  outline: 2px solid #3b82f6;
  outline-offset: 2px;
  box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.3);
}

/* Modern focus-visible */
@supports selector(:focus-visible) {
  *:focus { outline: none; }
  *:focus-visible {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
  }
}

Always provide clear, visible focus indicators for keyboard users

Motor & Cognitive Accessibility

Touch Target Guidelines

/* Minimum touch target size: 44px x 44px */
:root {
  --touch-target: 2.75rem; /* 44px */
}

.btn {
  min-height: var(--touch-target);
  min-width: var(--touch-target);
  padding: 0.5rem 1rem;
  /* Ensure spacing between targets */
  margin: 0.25rem;
}

.nav__link {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: var(--touch-target);
  padding: 0.5rem 1rem;
}

/* Adequate spacing between interactive elements */
.button-group > * {
  margin: 0.5rem;
}

/* Easy click/tap areas */
.card-link {
  display: block;
  padding: 1rem; /* Expand clickable area */
}

Ensure interactive elements are large enough and well-spaced for easy interaction.

Reduced Motion

/* Respect motion preferences */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Safe animations for vestibular disorders */
.safe-animation {
  /* Fade instead of slide */
  opacity: 0;
  transition: opacity 0.3s ease;
}

.safe-animation.visible {
  opacity: 1;
}

/* Avoid: */
/* - Continuous motion */
/* - Large movements */
/* - Parallax scrolling */
/* - Spinning animations */

Provide minimal motion alternatives for users with vestibular disorders

User Preferences

/* High contrast support */
@media (prefers-contrast: high) {
  :root {
    --color-primary: #000080;
    --color-text: #000000;
  }

  .btn {
    border-width: 3px;
  }
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #111827;
    --color-text: #f9fafb;
    --color-primary: #60a5fa; /* Higher contrast */
  }
}

/* Respect system preferences */
@media (prefers-reduced-transparency: reduce) {
  .modal {
    backdrop-filter: none;
    background: rgba(0, 0, 0, 0.9);
  }
}

Respect user system preferences for optimal personalized experience

Testing & Tools

Accessibility Testing Tools

  • • axe DevTools: Automated accessibility testing
  • • WAVE: Web accessibility evaluation
  • • Lighthouse: Built-in Chrome accessibility audit
  • • Pa11y: Command-line accessibility testing
  • • Colour Contrast Analyser: WCAG contrast checking
  • • Screen readers: NVDA, JAWS, VoiceOver testing
  • • Keyboard navigation: Tab through all elements
  • • Focus management: Verify focus indicators

CSS Accessibility Best Practices

  • • Use semantic HTML with proper CSS styling
  • • Maintain logical heading hierarchy (h1 → h6)
  • • Provide sufficient color contrast ratios
  • • Make focus indicators clearly visible
  • • Ensure minimum touch target sizes (44px)
  • • Respect user motion preferences
  • • Support high contrast and dark modes
  • • Test with actual assistive technologies

WCAG 2.1 CSS Guidelines Checklist

Perceivable

  • □ 4.5:1 contrast ratio for normal text
  • □ 3:1 contrast ratio for large text
  • □ Non-text contrast 3:1 (UI elements)
  • □ Text can resize to 200% without loss
  • □ Content reflows at 320px width
  • □ Don't rely on color alone for meaning

Operable

  • □ All functionality keyboard accessible
  • □ Visible focus indicators present
  • □ Minimum 44px touch targets
  • □ No content flashes more than 3 times/sec
  • □ Respect prefers-reduced-motion
  • □ Adequate spacing between targets

CSS Accessibility — step by step

1 / 4
1

Maintain sufficient color contrast

Text and background colors must have a contrast ratio of at least 4.5:1 for normal text. Use tools like the WebAIM Contrast Checker to verify.

1 of 4
🔍Focus Visible Strategy
Use :focus-visible instead of :focus to style keyboard focus rings. This shows the outline only when navigating with a keyboard, preventing the unsightly ring that appears when clicking buttons with a mouse.

🎯 Quick Check

What is the minimum color contrast ratio for normal body text per WCAG AA?

Accessible CSS: summary

Key points

  • outline: none without a replacement makes your site unusable by keyboard.
  • Touch targets should be at least 24×24 CSS pixels (44×44 is better).
  • Visual order changed with order or Grid placement doesn't change reading or tab order.
  • Check text against Windows High Contrast with the forced-colors media query.

Frequently asked questions

›How do I hide content but keep it for screen readers?

Use a visually-hidden (.sr-only) class that shrinks the element to 1px and clips it. display: none and visibility: hidden hide content from screen readers too.

›What color contrast ratio do I need?

WCAG AA requires 4.5:1 for normal text and 3:1 for large text (about 24px, or 19px bold) and for UI components.