Sub topic 7
Keeping keyboard focus visible
This chapter assumes you know the focus: and focus-visible: variants from the previous chapter. It also assumes you can read basic Tailwind utilities. The examples use Tailwind CSS v4.
Why the focus indicator matters
People who navigate with the Tab key need to see which element is active. Without a visible indicator, they can't tell where they are on the page. WCAG success criterion 2.4.7 (Focus Visible) requires that indicator for keyboard users.
Browsers already draw a default outline. The accessibility problem usually starts when a developer removes it.
The common mistake
<button class="rounded bg-indigo-600 px-4 py-2 text-white outline-none">
Save
</button>In v4, outline-none sets outline-style: none. The button now shows no focus indicator at all, and keyboard users can't see when it is focused. The same problem appears with focus:outline-none when nothing replaces the outline.
Replace the outline, don't remove it
Style the focus state with focus-visible:. This variant matches only when the browser decides a focus indicator is useful. That means keyboard focus, but usually not a mouse click on a button.
<button class="rounded bg-indigo-600 px-4 py-2 text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">
Save
</button>Each utility has a job:
| Utility | Effect |
|---|---|
| `focus-visible:outline-2` | Draws a 2px solid outline |
| `focus-visible:outline-offset-2` | Leaves a 2px gap between the element and the outline |
| `focus-visible:outline-indigo-600` | Sets the outline color |
Tailwind v3 differs here. outline-2 sets only the width, so you must also add focus-visible:outline to turn the outline on.
If you must hide the default outline
Sometimes you want to replace the browser's default with your own style. Use outline-hidden in v4 instead of outline-none. It hides the outline visually but leaves a transparent one in place. Windows forced colors mode (high contrast) still shows that outline.
<input
type="text"
class="rounded border border-slate-300 px-3 py-2 outline-hidden focus:border-indigo-600 focus:ring-2 focus:ring-indigo-600/40"
/>Here focus: is the right choice. Browsers show focus on text inputs for mouse clicks too, so the indicator should appear every time.
Choosing the variant
Check your result
Do not rely on the code alone. Test it:
- Load the page and press Tab repeatedly.
- Confirm that every interactive element shows an indicator when focused.
- Check that the indicator contrasts with both the element and the background. WCAG 2.2 asks for at least a 3:1 contrast ratio.
- Click a button with the mouse. With
focus-visible:, the outline normally stays hidden.
Exercise
Exercise
A teammate styles a link with class="text-indigo-600 focus:outline-none" because the outline "looks ugly". What is wrong, and how would you fix it?
Show answer
Removing the outline leaves keyboard users with no focus indicator, which fails WCAG 2.4.7. Replace it with a visible style, for example focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600. This shows the indicator for keyboard focus and usually hides it for mouse clicks, which addresses the "ugly" complaint.