Sub topic 7
Keeping keyboard focus visible
This chapter assumes you know HTML and CSS, including the :hover and :focus pseudo-classes. It also assumes you can use basic Tailwind utilities for color, padding, and borders, and that a Tailwind project is already set up. You have already seen how focus: and focus-visible: work as variants. Here the goal is narrower: making sure people who use a keyboard can always see where they are on the page.
Why focus indicators matter
Focus is the state of the one element that currently receives keyboard input. When you press Tab, focus moves to the next link, button, or form field. The browser draws an outline around the focused element so you can tell where you are.
People who cannot use a mouse depend on that outline. This includes people with motor disabilities, people who use switch devices, and many power users who prefer the keyboard. If the outline disappears, they cannot tell which control a press of Enter or Space will activate.
The Web Content Accessibility Guidelines (WCAG) treat this as a requirement. Success Criterion 2.4.7, "Focus Visible", says that any keyboard-operable interface must have a visible focus indicator. WCAG 2.2 adds criteria for focus appearance, which ask for enough size and contrast. Treat the indicator as part of the component, not as decoration.
The common mistake: outline-none on its own
Many Tailwind users add focus:outline-none to remove the browser's default ring because it looks plain or clashes with the design. That removes the only focus cue and gives nothing in its place.
<!-- Bad: keyboard users get no indicator at all -->
<button class="rounded bg-indigo-600 px-4 py-2 text-white focus:outline-none">
Save
</button>Tab to this button and nothing changes. The page looks as if focus has vanished.
Removing the default outline is fine only if you add another indicator at the same time. The rule is simple: every outline-none needs a replacement in the same element's class list.
A note on what outline-none does
In Tailwind v4, outline-none sets outline-style: none. The older v3 utility set a transparent 2px outline, which kept a visible outline in Windows High Contrast (forced colors) mode. In v4 the utility that matches that older behavior is outline-hidden. If you target forced-colors users and want to hide the default outline while keeping a fallback, use outline-hidden and add your own indicator.
Check which major version your project uses. The utility names differ, and the behavior in forced-colors mode differs with them.
Prefer focus-visible over focus
The focus: variant matches :focus. That pseudo-class matches whenever an element has focus, including when you click it with a mouse. A focus: ring on a button therefore appears after every click, which many designers find distracting.
The focus-visible: variant matches :focus-visible. The browser decides when to show the indicator, using heuristics roughly like these:
- Keyboard navigation with Tab always matches.
- Clicking a button or link with a mouse usually does not match.
- Clicking into a text input or textarea does match, because the user needs to see where typing will go.
For buttons and links, focus-visible: gives you a ring for keyboard users and a clean look for mouse users. For text fields, the browser shows the indicator on click as well, so either variant behaves similarly there.
The following example replaces the outline with a visible ring on keyboard focus only.
<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>Three utilities work together here:
outline-2sets the outline width to 2px.outline-offset-2leaves a 2px gap between the element and the outline. The gap helps the outline stay visible against the element's own background.outline-indigo-600sets the outline color.
In Tailwind v4, outline-2 also sets the outline style to solid, so you do not need a separate outline utility. In v3, you would write focus-visible:outline focus-visible:outline-2 so the style is set.
Ring utilities as an alternative
Tailwind also provides ring utilities, which draw a box-shadow-based ring around an element.
<input
type="text"
class="rounded border border-slate-300 px-3 py-2
focus:border-indigo-600 focus:outline-hidden focus:ring-2 focus:ring-indigo-600/40"
/>Here the field loses the default outline but gains a colored border and a soft ring. That is a valid replacement, because there is still a clear change on focus.
Outline and ring differ in a few ways that matter for accessibility:
| Property | outline | ring (box-shadow) |
|---|---|---|
| Follows `border-radius` | Yes in current browsers | Yes |
| Takes up layout space | No | No |
| Shown in forced-colors mode | Yes, the browser keeps it | No, box-shadow is removed |
| Clipped by `overflow: hidden` on a parent | Can be clipped | Can be clipped |
The forced-colors row is the main reason to prefer outlines for indicators. In Windows High Contrast mode, the browser removes box-shadows, so a ring-only indicator disappears for those users. If you use a ring, keep an outline fallback with outline-hidden, or keep a visible border change.
Make the indicator easy to see
An indicator that exists but cannot be seen does not help. Check these points.
- Contrast. WCAG 2.2 asks for a contrast ratio of at least 3:1 between the indicator and the colors next to it, both the element and the page background. A pale
indigo-200ring on a white page fails this test. - Thickness. Use at least 2px. A 1px line is easy to miss.
- Offset. When the element has a colored background, an outline drawn directly against it can blend in. A positive offset places the outline on the page background instead.
- Dark backgrounds. A ring color that works on white may be invisible on a dark surface. Use
dark:to change it.
Variants stack, so you can adjust the color per theme:
<a
href="/docs"
class="rounded px-1 text-indigo-700 underline
focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600
dark:text-indigo-300 dark:focus-visible:outline-indigo-300"
>
Read the docs
</a>The class dark:focus-visible:outline-indigo-300 applies only in dark mode while the link has keyboard focus.
focus-within for composite controls
Sometimes the focused element is a small part of a larger control. A search box with an icon and an input is one example: the user focuses the input, but the visible control is the whole bar.
The focus-within: variant matches :focus-within, which is true for an element when it or any descendant has focus. Put it on the wrapper and remove the outline from the input, so the wrapper shows the indicator.
<label
class="flex items-center gap-2 rounded border border-slate-300 px-3 py-2
focus-within:outline-2 focus-within:outline-offset-2 focus-within:outline-indigo-600"
>
<span aria-hidden="true">🔍</span>
<input
type="search"
placeholder="Search"
class="w-full bg-transparent outline-hidden"
/>
</label>This is acceptable because the indicator moves to the wrapper and is still shown whenever the input has focus. Test it: if you remove the outline from the input and the wrapper does not change, you have created the same problem as the bad example above.
Check your work with the keyboard
You cannot judge focus styling by looking at the design in a mouse session. Use this routine on every interactive component.
- Reload the page and press Tab repeatedly, without touching the mouse.
- At each stop, confirm that you can see which element has focus.
- Press Shift+Tab to go backward and check again.
- Activate controls with Enter or Space.
- Open the page on a dark background or in dark mode and repeat.
- Optionally, emulate forced colors in your browser's developer tools and repeat.
If any step leaves you unsure where focus is, the indicator is too weak or missing.
Common problems
- Focus is hidden by a parent with
overflow-hidden. Outlines and rings extend outside the element, so a clipping parent can cut them off. Add padding to the parent, or use a negative offset such asfocus-visible:outline-offset-[-2px]so the outline is drawn inside the element. - Focus is hidden behind a neighbor. A later sibling with a background can cover an outline. Raise the focused element with
focus-visible:relative focus-visible:z-10. - Custom elements get no focus at all. A
divwith a click handler is not focusable. Use abuttonora, or addtabindex="0"and the correct role. Styling cannot fix an element that never receives focus. - Only
hover:was styled. Hover does not apply to keyboard users. Whenever you add ahover:style that signals interactivity, pair it with a focus variant.
A reusable pattern
Repeating the same focus classes on every control is tedious. A short, consistent set is easier to keep correct. One approach is to define the same indicator once with @apply in a component class, or to keep the string in a shared component in your framework.
@import "tailwindcss";
@utility focus-ring {
&:focus-visible {
outline-style: solid;
outline-width: 2px;
outline-offset: 2px;
outline-color: var(--color-indigo-600);
}
}You can then write class="focus-ring" on any control. This uses the Tailwind v4 @utility directive. In v3, you would use @layer components with @apply instead. A single definition also makes it easier to change the color or width everywhere at once.
Summary of the decisions
| Situation | Use |
|---|---|
| Button or link | `focus-visible:` with outline, width, offset, and color |
| Text input | `focus:` or `focus-visible:` with a border or ring change, plus an outline fallback |
| Wrapper around an input | `focus-within:` on the wrapper |
| Removing the default outline | Only with a replacement indicator on the same element |
| Dark mode | Add `dark:focus-visible:` color overrides |
Exercise
A teammate writes <button class="bg-teal-600 px-4 py-2 text-white focus:outline-none">Send</button> because the default outline looked ugly. Explain what the problem is, then rewrite the classes so that keyboard users see a 2px indicator separated from the button by a gap, while mouse users who click do not see a ring.
Show answer
The problem is that focus:outline-none removes the only focus indicator and replaces it with nothing, so keyboard users cannot tell the button has focus (this fails WCAG 2.4.7). A fix is: <button class="bg-teal-600 px-4 py-2 text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-teal-600">Send</button>. The focus-visible: variant shows the outline for keyboard focus but not usually for mouse clicks. outline-offset-2 creates the gap, and outline-2 sets the 2px width.
Exercise
You have a search bar made of a label containing an icon and an input. The input has outline-hidden. Which variant do you put on the label to keep a visible focus indicator, and why does a ring-only replacement carry a risk in Windows High Contrast mode?
Show answer
Put focus-within: utilities on the label, for example focus-within:outline-2 focus-within:outline-offset-2 focus-within:outline-indigo-600, because :focus-within matches when any descendant has focus. A ring is drawn with box-shadow, and forced-colors mode removes box-shadows, so a ring-only indicator disappears for those users. An outline is kept by the browser, so use an outline or keep a visible border change.