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.

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.

When to use each focus selector
SelectorWhat it selectsWhen to use it
:focusThe element that currently has focusHighlight the input or button itself
:focus-visibleA focused element when the browser decides a focus indicator is neededMake the current control clear during keyboard navigation and other relevant interactions
:focus-withinAn element that has focus itself or contains a focused elementHighlight 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.

:focus-visible

Draw an outline when the browser decides a focus indicator is needed.

:focus-within

Outline the group while its input or button has focus.

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.

Style a control, move focus, or run code
What you want to doWhat to use
Style a control that has focusCSS :focus
Choose an initial control, such as when a page loadsThe HTML autofocus attribute
Move focus after an action, such as pressing a buttonJavaScript focus()
Run code when an element receives focusThe 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.

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

  1. Does the control actually receive focus? Try reaching it with Tab. Links need href. Check for disabled, inert, or hidden elements. Use a for navigation and button for actions so that basic keyboard interaction is built in.
  2. Does the CSS select the right control? Check the colon in selectors such as input:focus, as well as class names and IDs. A plain div or li is not normally part of the Tab order.
  3. Is another CSS rule overriding the style? Inspect outline and related styles in your browser's developer tools. Check whether another declaration takes precedence or a CSS reset removes the outline.
  4. 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

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.

Back to top