CSS :focus:
style focused inputs and buttons
Focus identifies the element that receives keyboard input. Clicking a text field so you can type in it is one example. CSS :focus lets you change how that element looks.
Try an input and a button
Click or tap the input below. If you have a keyboard, press Tab to move forward or Shift + Tab to move back. Watch the outline move to the control that receives focus.
Current focus: Outside this example
This button is for trying the focus style. Nothing you type here is submitted. When you Tab out of the example, its custom outline disappears.
HTML
<div class="example">
<label for="name">Name</label>
<input id="name" type="text">
<button type="button">Practice button</button>
</div>
CSS
.example input:focus,
.example button:focus {
outline: 3px solid #005fcc;
outline-offset: 3px;
}
input:focus selects an input that has focus. A selector that matches an element in a particular state is called a pseudo-class. The colon : is part of the syntax. This is different from a class named .focus.
outline draws a line around the control. outline-offset sets the gap between the control and that line. An outline does not add to the width or height used for layout, so nearby controls do not move when it appears.
The example color is intended for a light background. On a dark background, use a lighter color and check that the outline is easy to see. The live examples on this page change their text, background, and outline colors with your selected theme.
Compare :focus, :focus-visible, and :focus-within
These selectors are related, but they select different things for different purposes.
You can scroll the table horizontally.
| Selector | What it selects | When to use it |
|---|---|---|
:focus | The element that currently has focus | Highlight the input or button itself |
:focus-visible | A focused element when the browser decides a focus indicator is needed | Make the current control clear during keyboard navigation and other relevant interactions |
:focus-within | An element that has focus itself or contains a focused element | Highlight a whole group containing an input |
:focus-visible is not limited to keyboard navigation. A text input can match it after a click or tap too. The decision depends on the control, the interaction, and browser or user settings.
In each example, select the input and Tab to the button. Compare this with clicking the button using a mouse. The buttons are for comparing focus styles. A browser's default focus indicator may also appear.
:focus
Draw an outline around the focused control.
Current focus: Outside this example
:focus-visible
Draw an outline when the browser decides a focus indicator is needed.
Current focus: Outside this example
:focus-within
Outline the group while its input or button has focus.
Current focus: Outside this example
CSS
/* Select the control itself */
.example input:focus-visible {
outline: 3px solid #005fcc;
outline-offset: 3px;
}
/* Select the group containing the input */
.example:focus-within {
outline: 3px solid #005fcc;
outline-offset: 3px;
}
:focus-within does not give focus to every control in the group. Also, :hover means a pointer is over an element, and :active matches while it is being activated, such as during a press. Neither state is the same as focus.
Start by keeping the browser's default focus indicator. Using only outline: none to remove it can make keyboard users lose track of their position.
Can CSS move focus to an input?
CSS :focus changes the appearance of an element that already has focus. Use HTML or JavaScript when you need to move focus.
You can scroll the table horizontally.
| What you want to do | What to use |
|---|---|
| Style a control that has focus | CSS :focus |
| Choose an initial control, such as when a page loads | The HTML autofocus attribute |
| Move focus after an action, such as pressing a button | JavaScript focus() |
| Run code when an element receives focus | The JavaScript focus event. onfocus is one way to assign its handler. |
Press the button below to move focus to the input. Then try typing into it.
Current focus: Outside this example
HTML
<button id="move-button" type="button">Move to the input</button>
<label for="move-target">Target input</label>
<input id="move-target" type="text">
JavaScript (run after the HTML above)
const button = document.querySelector("#move-button");
const input = document.querySelector("#move-target");
button.addEventListener("click", () => {
input.focus();
});
The target must be able to receive focus. This page does not move focus automatically when it opens. Unnecessary focus changes can interrupt a screen reader user's reading position or unexpectedly open a mobile keyboard.
See also how to use autofocus carefully.
What to check when :focus does not work
- Does the control actually receive focus? Try reaching it with Tab. Links need
href. Check fordisabled,inert, or hidden elements. Useafor navigation andbuttonfor actions so that basic keyboard interaction is built in. - Does the CSS select the right control? Check the colon in selectors such as
input:focus, as well as class names and IDs. A plaindivorliis not normally part of the Tab order. - Is another CSS rule overriding the style? Inspect
outlineand related styles in your browser's developer tools. Check whether another declaration takes precedence or a CSS reset removes the outline. - Is the outline hard to see or cut off? Check for a color similar to the background, a very thin line, or clipping caused by a parent's
overflow. Test light and dark themes and leave room around the outline.
Should you add tabindex?
tabindex="0" adds an element to the normal Tab order. tabindex="-1" leaves it out of that order but lets code such as focus() move focus to it. Positive values make the navigation order harder to understand and are usually best avoided.
Adding tabindex does not give a div all the behavior of a button. Start with the HTML element that fits the task.
Focus and form submission are different
Pressing Enter in a form field can submit the form under certain conditions. It does not mean “submit only the focused input.” The result depends on the form's structure and the types of buttons it contains.
Keep the control easy to use
- Choose text and outline colors that stand out against the background. Use a clear outline as well as any color change.
- Give inputs a
labeland buttons a name that explains their action. - Check that a keyboard user can move forward, move back, and leave the example.
- Keep the focus indicator visible when nothing is moving. Do not rely on a temporary animation alone.
Using focus in modals, iframes, and Shadow DOM
A modal keeps interaction inside it until it closes. CSS :focus-within alone cannot stop focus from moving outside. When using dialog, consider how it opens with showModal() and where focus returns after closing.
Inside an iframe or a component that uses Shadow DOM, check the inner and outer focus contexts separately. document.activeElement may identify the iframe or component host while an element inside it has focus.
Read the HTML Standard's sections on showModal() and focus.
Check your understanding
What would you use to style a focused input, move focus after a button press, and highlight a group containing a focused input?
Check the answers
Use CSS :focus to style the input, JavaScript focus() to move focus, and CSS :focus-within to highlight the containing group.