Sub topic 4
focus, focus-visible, and focus-within
This chapter assumes you know basic HTML and CSS, including the :hover and :focus pseudo-classes. It also assumes you know common Tailwind utilities for colors, padding, and rounded corners, and that you work in a project where Tailwind CSS is already set up. You should also know how the hover: prefix works from the previous chapters.
By the end, you can choose between focus:, focus-visible:, and focus-within:. You can also predict when each one fires and avoid the common mistakes.
What focus means
An element has focus when it is the target of keyboard input. Only some elements can receive focus by default: links with an href, buttons, form controls, and elements with a tabindex attribute. Pressing Tab moves focus from one such element to the next.
Focus matters for two groups of users. Keyboard users and screen reader users rely on a visible focus indicator to know where they are on the page. Mouse users also focus elements, for example by clicking into a text input.
Browsers draw a default focus ring. Tailwind's variants let you replace it with a style that matches your design. Each variant maps to a CSS pseudo-class:
| Variant | CSS pseudo-class | Applies when |
|---|---|---|
| `focus:` | `:focus` | The element itself has focus, by any method |
| `focus-visible:` | `:focus-visible` | The element has focus and the browser decides a focus indicator should show |
| `focus-within:` | `:focus-within` | The element, or any descendant, has focus |
The focus variant
focus: applies a utility whenever the element has focus, no matter how it got there. Clicking, tapping, and pressing Tab all trigger it.
This is the right choice for text inputs. A user who clicks into a text field expects to see that it is active.
<input
type="text"
class="rounded border border-slate-300 px-3 py-2 focus:border-indigo-600 focus:outline-none focus:ring-2 focus:ring-indigo-600"
placeholder="Your name"
/>Here the border changes color and a ring appears when the input has focus. Tailwind's focus:outline-none removes the browser default outline, and focus:ring-2 adds a replacement. This pairing is the safe pattern: you remove the default only when you supply a visible substitute in the same state.
On a button, focus: has a side effect. In most browsers, a mouse click on a button also gives it focus, so focus:ring-2 shows a ring after every click. Many designers find that distracting. That problem is what focus-visible: solves.
The focus-visible variant
focus-visible: applies only when the browser decides the focus indicator should be shown. The browser uses heuristics, and the details vary slightly, but the general behavior is consistent:
- Keyboard focus, such as pressing Tab, matches
:focus-visible. - A mouse click on a button or link usually does not match.
- A click or tap into a text input usually does match, because typing requires a visible caret and indicator.
That makes focus-visible: the best default for buttons and links. The ring appears for keyboard users and stays hidden for mouse clicks.
<button
class="rounded bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
Save
</button>The button uses an outline instead of a ring. focus-visible:outline-2 sets the outline width, focus-visible:outline-offset-2 leaves a gap between the button and the outline, and focus-visible:outline-indigo-600 sets the color. Because the default outline is not removed, there is no state in which a keyboard user loses the indicator.
The following diagram shows how the variants react to different ways of focusing a button.
Choosing between focus and focus-visible
Use these rules as a starting point:
- For text inputs, textareas, and selects, use
focus:. The user always benefits from seeing which field is active. - For buttons, links, and custom controls, use
focus-visible:. The indicator shows for keyboard users without flashing on every click. - If you are unsure,
focus-visible:is safe, because it matches whenever the browser thinks an indicator is needed.
A callout on one risk: if you use focus:outline-none on a button and give it no replacement, keyboard users lose all feedback.
The focus-within variant
focus-within: applies to a container when the container itself or any element inside it has focus. It is useful when the visual feedback belongs to a wrapper rather than to the control.
A common case is a search box that has an icon and an input inside a styled border. The input holds focus, but you want the whole box to highlight.
<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-600">
<svg class="h-4 w-4 text-slate-500" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path d="M8 2a6 6 0 104.5 10l4 4 1.5-1.5-4-4A6 6 0 008 2zm0 2a4 4 0 110 8 4 4 0 010-8z" />
</svg>
<input type="search" class="w-full bg-transparent outline-none" placeholder="Search" />
</div>The outline-none on the input is acceptable here, because the wrapper supplies the visible indicator through focus-within:. The indicator is still tied to the same state. If the wrapper did not highlight, removing the input's outline would hide focus completely.
focus-within: also works for menus and cards that contain several focusable elements. The container can reveal extra controls whenever any child has focus.
How focus-within differs from focus
focus: on a wrapper div does nothing in most cases, because a plain div is not focusable. The pseudo-class :focus only matches the element that itself has focus. :focus-within matches the element and all of its ancestors up the tree.
This also means more than one element can match :focus-within at once. If an input sits inside a form inside a card, all three match while the input has focus.
Combining the three
You can use all three on one component. A card that contains a link might highlight when any child is focused, while the link shows its own keyboard indicator.
<div class="rounded-lg border border-slate-200 p-4 focus-within:border-indigo-600">
<a
href="#"
class="font-semibold text-slate-900 hover:underline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
Read the guide
</a>
</div>The card border changes whenever the link has focus, including after a mouse click, because :focus-within does not use the keyboard-only heuristic. The link outline appears only for keyboard focus. If you want the card to react to keyboard focus only, there is a combined pseudo-class, :has(:focus-visible), which you can write in Tailwind as has-[:focus-visible]:border-indigo-600 in versions that support the has-* variant.
Common errors
- Using
focus:for button rings. Clicks leave a ring behind. Switch tofocus-visible:. - Removing the outline with no replacement. Keyboard users lose the indicator. Pair
outline-nonewith a ring or border in the same state. - Putting
focus:on a non-focusable element. Adivwithouttabindexnever receives focus, so the variant never matches. Usefocus-within:on the wrapper, or addtabindex="0"if the element should be a stop in the Tab order. - Relying on color alone. A subtle border color change can be hard to see. Use a ring or outline with enough contrast against its surroundings.
- Forgetting that ring and outline are different utilities.
ring-*is drawn with a box shadow, andoutline-*uses the CSSoutlineproperty. Both work, but outlines followoutline-offsetand are preserved in forced-colors mode, while shadows may be removed there. For this reason, an outline is often the more robust choice for keyboard focus. - Testing only with a mouse. Press Tab through your page after any focus change to check the indicator at each stop.
Testing your focus styles
- Load the page and click once in an empty area of the page.
- Press Tab and watch where the indicator moves.
- Confirm every interactive element shows a visible change when it has focus.
- Click each button with the mouse and confirm that
focus-visible:styles do not appear. - Click into each text input and confirm that the
focus:style appears.
If a control receives focus with no visible change, add a focus-visible: or focus: style to it.
Exercise
A button uses focus:ring-2 focus:ring-indigo-600. Designers complain that a ring stays on the button after a mouse click. Which variant should replace focus:, and why does the ring still appear for keyboard users after the change?
Show answer
Replace focus: with focus-visible:. A mouse click on a button gives it focus, so :focus matches and the ring shows. :focus-visible matches only when the browser decides an indicator is needed, which it does for keyboard focus such as pressing Tab. The ring therefore still appears for keyboard users and usually stays hidden for mouse clicks.
Exercise
You have a div containing an icon and an input. You want the whole div to show an indigo border when the input has focus. Which variant goes on the div, and why does focus: not work there?
Show answer
Put focus-within:border-indigo-600 on the div. A plain div cannot receive focus, so :focus never matches it. :focus-within matches an element when it or any descendant has focus, so it matches the div while the input is focused.