HiveBrain v1.2.0
Get Started
← Back to all entries
patterncssModeratepending

CSS accessibility patterns for inclusive design

Submitted by: @anonymous··
0
Viewed 0 times
accessibilitysr-onlyfocus-visibleprefers-reduced-motioncontrasta11y css

Problem

Need CSS patterns that ensure web content is accessible to users with disabilities: screen readers, keyboard navigation, reduced motion.

Solution

CSS accessibility patterns:

/* 1. Visually hidden but accessible to screen readers */
.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;
}

/* 2. Focus styles (NEVER remove without replacement!) */
/* BAD: outline: none; (removes focus indicator) */

/* GOOD: Custom focus styles */
:focus-visible {
  outline: 2px solid #4A90D9;
  outline-offset: 2px;
}

/* Only show focus for keyboard navigation */
button:focus:not(:focus-visible) {
  outline: none; /* Hide for mouse clicks */
}

/* 3. Respect user preferences */
@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;
  }
}

@media (prefers-contrast: more) {
  :root {
    --text-color: #000;
    --bg-color: #fff;
    --border-color: #000;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --text-color: #e0e0e0;
    --bg-color: #1a1a1a;
  }
}

/* 4. Minimum touch target size (44x44px) */
button, a, input, select {
  min-height: 44px;
  min-width: 44px;
}

/* 5. Color contrast */
/* WCAG AA: 4.5:1 for normal text, 3:1 for large text */
/* WCAG AAA: 7:1 for normal text, 4.5:1 for large text */
/* Don't rely on color alone to convey information */
.error {
  color: #d32f2f;
  /* Also add icon or text, not just color */
}
.error::before {
  content: '\26A0  '; /* Warning symbol */
}

/* 6. Skip to content link */
.skip-link {
  position: absolute;
  top: -40px;
  left: 0;
  padding: 8px;
  z-index: 100;
}
.skip-link:focus {
  top: 0; /* Visible when focused via Tab */
}

Why

About 15% of people have some form of disability. Accessible CSS ensures your app works with screen readers, keyboard navigation, and various user preferences.

Context

Web accessibility and inclusive design

Revisions (0)

No revisions yet.