日本語

How to Highlight the Active Form Field with CSS :focus-within

​​CSS​Accessibility,HTML,Playwright

To highlight the border of the field you are typing in on a sign-up or contact form, wrap the label, input, and help text in one element, and style that element with :focus-within. The border color and background change only while focus is inside one of the inputs, and they return to normal when focus moves to the next field. You do not need JavaScript to track focus.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I opened the form page with Playwright, and after clicks and key presses I read the state with matches(':focus-within') and getComputedStyle(). Forced colors mode was reproduced with Playwright's emulateMedia({ forcedColors: 'active' }); it is not the Windows contrast theme itself. I did not test Firefox, Safari, real smartphones, or screen reader announcements.

A video of the form with a label reading "Operation 1 / 3" above it. The highlighted border changes with each operation. In the first step, the "Name" input is clicked, and the Name border gets a thick light-blue edge and a pale light-blue background. In the second step, the Tab key is pressed and the highlight moves to the email field. In the third step, an empty area to the right of the form is clicked, and no border is highlighted. A panel below shows the name of the highlighted field, and it flashes pink only at the moment it changes
How the highlighted border moves with mouse clicks and the Tab key. The top banner, the cursor circle, the Tab key display, and the name panel below were added for the recording. The labels in the image are in Japanese.

Write :focus-within on the element that wraps the label and input

The form I used for testing wraps each field's label, input, and help text in a <div class="field">.

html
<form>
  <div class="field">
    <label for="name">お名前</label>
    <input id="name" type="text" autocomplete="name">
    <p class="help">フルネームで入力してください。</p>
  </div>
  <div class="field">
    <label for="mail">メールアドレス</label>
    <input id="mail" type="email" autocomplete="email">
    <p class="help">確認のメールを送ります。</p>
  </div>
</form>

Put the normal look of the border on .field, and the highlighted look on .field:focus-within. The following example leaves out other styles from the test page, such as font size and input width.

css
.field {
  padding: 12px 16px;
  border: 1px solid #c5d0d5;
  border-radius: 12px;
}

.field:focus-within {
  border-color: #00c8e8;
  outline: 3px solid rgb(0 200 232 / 35%);
  background: #eefafd;
}

As described on MDN's :focus-within page, :focus-within matches an element when the element itself or one of its descendants has focus. The same page gives an example of emphasizing the whole form while one of its inputs has focus.

In the test environment, when I clicked the "Name" input, the Name border's border-color changed from rgb(197, 208, 213) to rgb(0, 200, 232), its background changed from transparent to rgb(238, 250, 253), and its outline changed from none to solid 3px. The light-blue edge outside the border is drawn with the outline I put on .field. The reasons for using outline instead of box-shadow or border-width are explained in the next two sections.

Which actions make the border stand out

In addition to the two fields above, the test form included a "Contact method" group (<fieldset class="field">) with radio buttons, and a "Keyword" field with an input next to a "Clear" button. For each action, these are the borders that matched :focus-within.

Action Element with focus Highlighted border
Right after loading body None
Click the "Name" input Name input Name
Type text in the input Name input Name
Press the Tab key Email input Email address
Click the text of the "Name" label Name input Name
Click empty space in the border (to the right of the help text) body None
Click outside the form body None
Tab into the radio buttons, then change the selection with the → key Selected radio button Contact method

Clicking a label linked with <label for> moves focus to its input, so the border is highlighted too. On the other hand, when I clicked an area inside a border that had no label or input, such as the empty space in the border, focus moved to body and no border was highlighted. Whether a border is highlighted depends on whether the focused element is inside it, not on whether you clicked inside it.

With radio buttons, pressing the → key to change the selection from "Email" to "Phone" also moved focus to the "Phone" button. Both buttons are inside the same <fieldset>, so the "Contact method" border stayed highlighted while the selection changed.

When making the border thicker, do not change border-width

If you change border-width to make the border thicker when it is highlighted, the size of the border changes and the fields below move. I compared the top edge position of the "Email address" border (its distance from the top of the page) before and after clicking the "Name" input.

What I wrote in .field:focus-within Before click After click
border-width: 2px (1px normally) about 162.0px about 164.0px
box-shadow: 0 0 0 3px … about 162.0px about 162.0px
outline: 3px solid … about 162.0px about 162.0px

Making the border 1px thicker made it 2px taller in total, top and bottom, and the Email address border below moved down by 2px. The inside of the border also shifted by 1px, and the left edge of the Name input moved from 41px to 42px. Each click on an input moved the fields below it by 2px. Neither box-shadow nor outline changed any positions.

In forced colors mode, only outline remains

box-shadow and outline both leave positions unchanged, but they differ in forced colors mode. MDN's forced-colors page states that in forced colors mode, colors such as border-color, outline-color, and background-color are replaced with colors chosen by the browser, and that box-shadow becomes none.

I reproduced forced colors mode with Playwright and compared the computed values of the focused "Name" border and the unfocused "Email address" border.

CSS Border border-color box-shadow outline-style
Version wrapped with box-shadow Name (focused) rgb(0, 0, 0) none none
Version wrapped with box-shadow Email address rgb(0, 0, 0) none none
Version wrapped with outline Name (focused) rgb(0, 0, 0) none solid
Version wrapped with outline Email address rgb(0, 0, 0) none none

In the version wrapped with box-shadow, the focused and unfocused borders had the same border color and no shadow. The background was white on the focused border and transparent on the unfocused one, so on a white page they could not be told apart. In the version wrapped with outline, only the focused border kept its 3px line.

Two screenshots of forced colors mode with the "Name" input focused, stacked vertically. The top one, wrapped with box-shadow, shows the Name border as only a thin black line that looks the same as the Email address border below it. The bottom one, wrapped with outline, shows a thick black line outside the Name border
Forced colors mode reproduced with Playwright, with Name focused. Top: wrapped with box-shadow. Bottom: wrapped with outline. The labels in the image are in Japanese.
Point

If you draw the highlight line with outline, the fields below do not move, and the focused border stays visible in forced colors mode.

Borders that contain buttons also stand out on mouse presses

In a border that has a button next to the input, such as the "Keyword" field, the border also stands out when you click the button with the mouse. In Chromium, clicking a button moves focus to it, and the button is a descendant of the border.

If you want the highlight only while using the keyboard, you can combine :has() with :focus-visible and write .field:has(:focus-visible). According to MDN's :focus-visible page, when a button is clicked with the mouse, browsers generally do not show focus, but they do show it for text input fields. I checked whether the Keyword border matched each selector in the test environment.

Action :focus-within :has(:focus-visible)
Click the input Matches Matches
Click the "Clear" button Matches Does not match
Tab from the input to the "Clear" button Matches Matches
Shift+Tab from the "Clear" button back to the input Matches Matches

Text input fields matched :focus-visible both when clicked and when reached by keyboard, so the two selectors made no difference for them. The difference appeared only when a button was clicked with the mouse. Radio buttons behaved like buttons: clicking "Phone" in "Contact method" did not match :has(:focus-visible), but moving to it with the Tab key did.

Outer elements also match :focus-within

:focus-within matches every ancestor of the focused element. When the Name input had focus, the elements that matched :focus-within were html, body, form, the Name div.field, and the input itself. The Selectors Level 4 specification also defines it as matching an element that matches :focus and elements that have such an element as a descendant.

When I wrote a broad selector such as form :focus-within, only two elements matched while the Name input had focus: the Name div.field and the input itself. The input also receives the same styles as the border. Give the border you want to highlight a class, and write the selector for that border alone, such as .field:focus-within.

Browser support

According to the compatibility data in @mdn/browser-compat-data 8.1.8, :focus-within is available in Chrome 60, Edge 79, Firefox 52, and Safari 10.1 (iOS 10.3). :has(), which I used for borders containing buttons, is available in Chrome and Edge 105, Firefox 121, and Safari 15.4 (iOS 15.4). :focus-visible is available in Chrome and Edge 86, Firefox 85, and Safari 15.4, so whether .field:has(:focus-visible) works depends on the :has() version.

​Posted by 86