How to Create Accessible Focus States for Keyboard and Screen Reader Users

Table of Contents

Web accessibility is not just about compliance—it is about inclusion. Users who rely on keyboard navigation and screen readers need clear, visible focus indicators to move through a website efficiently.

Unfortunately, many websites neglect focus states, making navigation frustrating or impossible for users who cannot use a mouse.

Let’s explore why focus states matter, what makes them effective, and how to implement them with CSS and JavaScript.


Why Focus States Matter in Web Accessibility

A focus state highlights the currently selected element when users navigate via keyboard (Tab key) or assistive technologies. Without proper focus styles:

  • Users lose track of where they are, making interaction difficult.
  • Form fields, buttons, and links become inaccessible to non-mouse users.
  • Websites fail WCAG (Web Content Accessibility Guidelines) compliance, limiting usability.

A well-designed focus state guides users visually and ensures smooth navigation—without sacrificing aesthetics.


Best Practices for Accessible Focus Styles

1. Use the :focus-visible Pseudo-Class for Better UX

By default, browsers apply a focus outline to elements, but many developers remove it entirely with:

				
					*:focus {
    outline: none;
}

				
			

This destroys accessibility for keyboard users. Instead, use :focus-visible, which only shows focus indicators when needed (like when using the Tab key, but not when clicking with a mouse).

				
					button:focus-visible, 
a:focus-visible, 
input:focus-visible {
    outline: 3px solid #005fcc;
    outline-offset: 2px;
    border-radius: 4px;
}

				
			

Why This Works:

  • The :focus-visible selector only applies styles when necessary, avoiding unnecessary outlines for mouse users.
  • The outline property ensures high contrast, improving visibility.
  • outline-offset prevents overlap with content.

2. Ensure Sufficient Contrast for Visibility

Focus states must meet WCAG contrast requirements. A faint or low-contrast outline can be difficult to see, especially for users with visual impairments.

Good Example:

				
					a:focus-visible {
    outline: 3px dashed #ff9900; /* High contrast */
}

				
			

Bad Example:

				
					a:focus-visible {
    outline: 1px solid lightgray; /* Low contrast, hard to see */
}

				
			

For the best experience, use bold colors that contrast well against your background.


3. Add a Keyboard-Only Focus Indicator with JavaScript

If you want to show focus only when using the keyboard, you can add a class dynamically.

JavaScript Solution:

				
					document.body.addEventListener("keydown", function(e) {
    if (e.key === "Tab") {
        document.body.classList.add("user-is-tabbing");
    }
});

				
			

Then in CSS:

				
					.user-is-tabbing button:focus,
.user-is-tabbing a:focus {
    outline: 3px solid #00aaff;
}

				
			

Why This Works:

  • Prevents unnecessary focus outlines for mouse users.
  • Improves keyboard navigation without disrupting design.

4. Ensure Focus Order is Logical

Screen reader and keyboard users navigate sequentially through a webpage. If focus jumps around unpredictably, users get lost.

How to Fix Focus Order Issues:

  • Use a logical HTML structure—screen readers follow the document flow.
  • Avoid tabindex values greater than 0, as they can disrupt expected navigation order.
  • Ensure modal dialogs, dropdowns, and overlays trap focus (users should not be able to tab out of an open modal).

Example of trapping focus in a modal:

				
					document.addEventListener("keydown", function(e) {
    if (e.key === "Tab" && modalIsOpen) {
        keepFocusInsideModal(e);
    }
});

				
			

5. Test with a Keyboard and Screen Reader

After implementing focus styles, test your website without a mouse to ensure smooth navigation.

Keyboard Testing Checklist:

✔ Press Tab—does the focus move logically?
✔ Can you see the focus outline clearly on every interactive element?
✔ Does pressing Enter activate buttons and links as expected?
✔ Can you navigate out of and back into modals or dropdowns without issues?

Screen Reader Testing:

  • Use NVDA (Windows) or VoiceOver (Mac) to check how focus is announced.
  • Ensure focus states are visually and audibly recognizable.

Final Thoughts: Focus is Not an Afterthought

A well-implemented focus state ensures seamless navigation for all users, making your website more inclusive.

By using :focus-visible, maintaining strong contrast, and testing with assistive technologies, you create an experience that is both functional and accessible.

Not sure if your site meets accessibility standards? Try navigating without a mouse—and see what your users experience.

Explore more posts

Journal Entry

There are people who change the temperature of a room the second they enter. Not because they’re loud or relentlessly cheerful, but because they carry a steady kind of “we’ll figure it out.” Spend enough time with them and your shoulders drop, your thoughts unclench, and the next step stops...

Article
Over-editing feels like care, but it often slows learning and sands off the ideas people remember. This post shows a simple three-pass workflow, concrete examples, and fast metrics you can track in 48 hours to ship with more clarity and better results....
Article
Marketing shifted: search answers in-SERP, creators shape trust, marketplaces close buys, and AI speeds cycles. Here’s how to turn those shifts into pipeline and revenue—with clear roles and measurement....
Article
Always-on culture is taxing your team’s focus. Learn a simple system—response classes, focus blocks, and office hours—to protect deep work without hurting collaboration....
Article
Anti-design is everywhere right now—gritty fonts, clashing colors, broken grids. But the best versions aren't messy by accident. They're intentionally designed to feel raw, real, and human. Here's how to embrace the movement without throwing your principles out the window....
Article
AI tools promise speed, efficiency, and enhanced creativity—but a recent MIT study reveals a surprising downside: decreased brain activity, weaker memory, and lower curiosity. This post explores what’s really at stake when we let AI do the heavy lifting—and how to keep our thinking sharp in an age of convenience....