CSS :focus Selector

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The :focus pseudo-class matches an element that currently has focus—for example after a click, tap, or Tab key press on an input or button.

It applies only to the focused element itself. To style a parent that contains focus, use :focus-within. For keyboard-oriented rings, prefer :focus-visible when you can.

Baseline · Widely available.

Not deprecated. A core interactive pseudo-class from Selectors / HTML.

Accessibility warning:

Never set outline: none on :focus without a visible replacement that meets contrast needs (WCAG non-text contrast).

CSS Syntax

syntax.css
:focus { /* or */ E:focus { … }

Example: input:focus { background-color: lightblue; }

Selector Forms

FormDescriptionTry it
input:focusStyle a text field while it has focusTry it
select:focusHighlight a select menu on focusTry it
Custom ringReplace the default outline with a clear custom styleTry it
button:focusShow focus on a buttonTry it

How to Use :focus

Target interactive controls, then style a clear focus state users can see.

  1. Choose a focusable element (input, button, a[href], …).
  2. Write E:focus.
  3. Change background, border, or outline—keep contrast high.
  4. Test with Tab as well as mouse/touch.
PieceRequired?Role
Focusable elementYesMust be able to receive focus
:focusYesMatches while that element is focused
Visible indicatorYes (a11y)Outline, border, or shadow users can see

Live Preview

Click or Tab into the field, then switch presets. Styles apply through real :focus rules.

:focus presets

Focus the input to see the selected style. Keep a visible indicator for keyboard users.

Form control

Preset: background — light blue fill on focus.

CSS Examples

Each snippet focuses on :focus. Extra styles stay minimal for study. Use Try It Yourself for a live editor.

Example 1 — Focused Text Input

Change the background when an input has focus.

CSS
<style>
  input:focus {
    background-color: lightblue;
  }
</style>

<label>
  Full Name:
  <input name="firstName" type="text">
</label>
Try It Yourself

How It Works

  1. Target the control. input:focus runs only while that input is focused.
  2. Click or Tab. Focus moves to the field.
  3. Style applies. The background turns light blue.
  4. Blur clears it. Leaving the field removes :focus.

Example 2 — Focused Select

Give a different focus style to a select.

CSS
<style>
  select:focus {
    background-color: ivory;
  }
</style>

<label>
  Flavor:
  <select name="flavor">
    <option>Cherry</option>
    <option>Green Tea</option>
    <option>Mint Chip</option>
  </select>
</label>
Try It Yourself

How It Works

  1. Different control. select:focus targets dropdowns, not text fields.
  2. Focus the select. Click or Tab onto it.
  3. Ivory fill. The background changes while focused.
  4. Same idea. Any focusable form control can use :focus.

Example 3 — Custom Focus Ring

Replace the default outline with a clear custom ring.

CSS
<style>
  button:focus {
    outline: 3px solid #2563eb;
    outline-offset: 2px;
  }
</style>

<button type="button">Save</button>
Try It Yourself

How It Works

  1. Keep a ring. A thick blue outline stays visible on focus.
  2. Offset. outline-offset adds space outside the button edge.
  3. Tab to test. Keyboard focus should still be obvious.
  4. Safe pattern. You customized focus without removing it entirely.

Edge Cases & Pitfalls

Accessibility mistakes around focus are the most common issues.

a11y

Do not remove outline alone

outline: none without a replacement hides keyboard focus. Always provide another clear indicator.

scope

Only the focused element

:focus does not style parents. Use :focus-within for a container that holds focus.

mouse

:focus vs :focus-visible

:focus also matches mouse clicks. Prefer :focus-visible when you want rings mainly for keyboard users.

tabindex

Divs are not focusable by default

A plain div will not match :focus unless you add tabindex="0" (and usually a role/keyboard handler).

hover

Not the same as :hover

Hover is pointer-over. Focus is keyboard/programmatic activation. Style both when needed: see :hover.

Browser Support

:focus is a browser CSS selector. Node.js, Deno, and Bun do not evaluate it.

✓ Baseline · Widely available

:focus

Supported in all major browsers for many years (Chrome 1+, Firefox 1+, Safari 1+, Edge 12+, IE 8+).

98% Global support
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1+
Full support
Microsoft Edge 12+
Full support
Opera 7+
Full support
Samsung Internet 1+
Full support
Internet Explorer 8+
Full support
:focus Widely supported

Bottom line: Safe for production focus styling. Related selectors like :focus-visible are also widely supported in modern browsers. It is not a JavaScript runtime API.

Key Takeaways

  • Role: style the element that currently has focus.
  • Form: input:focus, button:focus, a:focus, …
  • A11y: never remove the focus ring without a visible replacement.
  • Related: :focus-visible for keyboard rings; :focus-within for parents.

One line: :focus styles the control that currently has keyboard or pointer focus—keep that state visible.

Frequently Asked Questions

It matches the element that currently has focus—usually after a click/tap or Tab key navigation on an interactive control.
Interactive elements such as input, textarea, select, button, and a[href], plus any element given a tabindex of 0 or higher.
Do not remove the focus indicator without a clear replacement. Keyboard users need a visible ring. Replace outline with another high-contrast style if you customize it.
:focus matches whenever the element is focused, including mouse clicks. :focus-visible is for when the browser shows a focus ring—typically keyboard focus.
:focus matches only the focused element. :focus-within matches an ancestor that contains a focused descendant—useful for styling a whole form field group.
No. It is a CSS selector evaluated by the browser layout engine, not a Node.js or server runtime feature.

Next: :fullscreen

Style an element while it is displayed fullscreen.

:fullscreen tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful