Sub topic 4
focus, focus-visible, and focus-within
This chapter assumes you know the CSS :focus, :focus-visible, and :focus-within pseudo-classes, and that Tailwind is already set up in your project. It covers three focus variants, when each one applies, and the mistakes people make with them.
What "focus" means
An element has focus when it is the target of keyboard input. Links, buttons, form controls, and anything with a tabindex can receive focus. Tailwind gives you one variant for each of the three related CSS pseudo-classes.
| Variant | CSS pseudo-class | Applies when |
|---|---|---|
| `focus:` | `:focus` | The element has focus, however it got it |
| `focus-visible:` | `:focus-visible` | The browser decides a focus indicator should show |
| `focus-within:` | `:focus-within` | The element or any descendant has focus |
focus: any focus
focus: matches whenever the element has focus. That includes a mouse click on a button, a tap on an input, and tabbing with the keyboard.
<input
type="text"
class="rounded border border-slate-300 px-3 py-2 focus:border-indigo-600 focus:ring-2 focus:ring-indigo-200"
/>For text inputs this is usually what you want. The user needs to see which field will receive typing, no matter how they got there.
focus-visible: keyboard-style focus
For buttons and links, focus: has a drawback. Clicking a button with a mouse gives it focus, so a focus: ring appears after every click. Many designers find that distracting.
focus-visible: solves this. Browsers apply :focus-visible when a focus indicator is helpful. That is typically keyboard navigation, and always for text inputs. A mouse click on a button usually does not trigger it.
<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>Press Tab to reach this button and the outline appears. Click it with a mouse and, in most browsers, no outline shows.
Keeping keyboard focus visible
Keyboard users depend on the focus indicator to know where they are. Never remove it without adding a replacement.
<!-- Bad: no indicator left -->
<button class="outline-none">Save</button>
<!-- Good: replaces the default with a visible style -->
<button class="outline-none focus-visible:ring-2 focus-visible:ring-indigo-600 focus-visible:ring-offset-2">Save</button>A practical rule: use focus-visible: for buttons, links, and custom controls. Use focus: for text inputs, where showing the state on click is expected.
focus-within: a parent reacts to a child
focus-within: applies to an element when it or any of its descendants has focus. This lets you highlight a whole container, such as a search bar, while the user types in the input inside it.
<div class="flex items-center gap-2 rounded border border-slate-300 px-3 py-2 focus-within:border-indigo-600 focus-within:ring-2 focus-within:ring-indigo-200">
<span aria-hidden="true">🔍</span>
<input type="search" class="flex-1 outline-none" placeholder="Search" />
</div>The input hides its own outline here, which is safe because the container shows a visible ring through focus-within:. The indicator still exists. It has moved to the parent.
Common errors
- Removing the outline and adding nothing. Keyboard users lose their position on the page.
- Using
focus:on buttons and links. A ring flashes after every mouse click. Switch tofocus-visible:. - Putting
focus-within:on the input. An input has no descendants that take focus, so the variant adds nothing there. Put it on the wrapper. - Expecting
focus-visible:on a non-focusable element. A plaindivnever gets focus. Addtabindex="0"only if it is truly interactive. Prefer a realbuttonoraelement.
Exercise
A search field is built as a div containing an icon and an input. You want the whole div to show an indigo border while the user types, and you hide the input's own outline. Which variant goes on which element, and why?
Show answer
Put focus-within:border-indigo-600 on the wrapper div. The input is the element that receives focus, and focus-within: matches a parent when any descendant has focus. The visible border on the div replaces the input's removed outline, so the focus indicator remains.
Exercise
Your team's buttons show a blue ring after every mouse click, but designers want the ring only for keyboard users. Which variant should replace focus:ring-2?
Show answer
Use focus-visible:ring-2. The browser applies :focus-visible after keyboard navigation but usually not after a mouse click on a button.