Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS Accessibility – Focus St | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 35 of 50

·css

CSS Accessibility – Focus Styles & Reduced Motion

9 mins

Course progress70%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • `:focus-visible` only shows a focus ring when the element is keyboard‑focused.
  • `prefers-reduced-motion` should disable non‑essential animations.
  • Ensure contrast ratios meet WCAG 2.1 AA (≥ 4.5:1 for normal text).

Accessibility isn’t an after‑thought – it’s a core part of a usable design system.

Focus Visible – The Keyboard Indicator

focus-visible matches elements that receive focus via keyboard, not by mouse click.

:focus-visible {
  outline:2px solid #ff6600;
  outline-offset:2px;
}

This prevents the unsightly outline that appears when clicking with a mouse, while still keeping a visible cue for keyboard navigation.


Reducing Motion

Many users set prefers‑reduced‑motion to minimise animations that can cause discomfort.

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

The rule essentially turns off all non‑essential motion for those users.


Contrast – WCAG 2.1 AA

  • Minimum 4.5:1 contrast for normal text, 3:1 for large text.
  • Use tools like Lighthouse, axe, or Stark (plugin) to test.
  • When using blend modes or semi‑transparent overlays, ensure the final contrast still meets the threshold.

ARIA & Semantic Markup

  • Use native HTML elements (<button>, <nav>, <header>) whenever possible – they come with built‑in keyboard support.
  • Add role only when native semantics are insufficient.
  • Provide clear focus order – the DOM order should match visual order.

Example – Accessible Modal

.modal {
  display:none; /* hidden by default */
  position:fixed; inset:0; background:rgba(0,0,0,.5);
  align-items:center; justify-content:center; /* Flex centering */
}
.modal[open] { display:flex; }
.modal:focus-visible { outline:none; }
<div class="modal" id="myModal" role="dialog" aria-modal="true" aria-labelledby="modalTitle">
  <div class="dialog" tabindex="-1">
    <h2 id="modalTitle">Dialog title</h2>
    <p>Content …</p>
    <button data-close>Close</button>
  </div>
</div>

Testing Checklist

  • Verify keyboard navigation (Tab, Shift+Tab, Enter, Escape) works.
  • Check focus rings on all interactive elements.
  • Run an automated audit (axe core or Chrome DevTools Lighthouse > Accessibility).
  • Confirm the site respects both prefers-reduced-motion and prefers-contrast.
Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonCSS Counters – Automatic NumberingContinue to lesson 36Print StylesheetsBack to CSS Basics to Advanced curriculum