CSS :focus-visible Selector

Beginner
6 min read
Updated: Nov 2026
3 examples
Live preview

Definition and Usage

The :focus-visible selector matches a focused element when the browser determines that a visible focus indicator is helpful. This usually includes keyboard navigation, while a pointer click may match :focus without matching :focus-visible.

Baseline · Widely available.

Use it for keyboard-friendly focus rings, but never leave focused controls without a visible indicator.

CSS Syntax

syntax.css
E:focus-visible{ … }

Selector Forms

FormDescriptionTry it
button:focus-visibleShow a ring on a keyboard-focused buttonTry it
input:focus-visibleHighlight a focused form fieldTry it
:focus:not(:focus-visible)Distinguish pointer focus when appropriateTry it
:focus-visible + .hintReveal adjacent help for keyboard focusTry it

How to Use :focus-visible

  1. Choose the element or scope you want to match.
  2. Write the selector using E:focus-visible.
  3. Add declarations that communicate the state or grouping clearly.
  4. Test the rule with matching and non-matching markup.
PieceRequired?Role
Base selectorOptionalLimits which elements are candidates
:focus-visibleYesApplies this selector's matching behavior
Declaration blockYesDefines the styles for matching elements

Live Preview

Use the controls in the editable examples to observe when :focus-visible matches.

:focus-visible preview

Rule: button:focus-visible

The ring appears when the button receives focus and the browser's focus-visible heuristic applies.

Open live editor

CSS Examples

Each example isolates one practical :focus-visible pattern.

Example 1 — Keyboard Focus Ring

The ring appears when the button receives focus and the browser's focus-visible heuristic applies.

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

<button type="button">Tab to me</button>
Try It Yourself

How It Works

  1. The browser evaluates button:focus-visible against the document.
  2. Only elements satisfying that selector receive the declarations.
  3. The rule changes the visible state without changing the HTML structure.
  4. Open the editor to test the selector with different markup.

Example 2 — Focused Input

Keyboard focus adds a high-contrast border and shadow without removing the native focus behavior.

CSS
<style>
  input:focus-visible {
    border-color: #0f766e;
      box-shadow: 0 0 0 3px #99f6e4;
  }
</style>

<label>Email <input type="email"></label>
Try It Yourself

How It Works

  1. The browser evaluates input:focus-visible against the document.
  2. Only elements satisfying that selector receive the declarations.
  3. The rule changes the visible state without changing the HTML structure.
  4. Open the editor to test the selector with different markup.

Example 3 — Pointer and Keyboard States

The link keeps a strong keyboard indicator while the browser decides whether pointer focus needs it.

CSS
<style>
  .action:focus-visible {
    outline: 3px solid #7c3aed;
      outline-offset: 2px;
  }
</style>

<a class="action" href="#next">Continue</a>
Try It Yourself

How It Works

  1. The browser evaluates .action:focus-visible against the document.
  2. Only elements satisfying that selector receive the declarations.
  3. The rule changes the visible state without changing the HTML structure.
  4. Open the editor to test the selector with different markup.

Edge Cases & Pitfalls

Check browser behavior, specificity, and accessibility before shipping.

specificity

Know what contributes weight

This pseudo-class contributes normal pseudo-class specificity.

support

Test target browsers

Use a sensible fallback when older browser support matters.

scope

Keep the selector intentional

Add a base selector or component scope when a page-wide match would be too broad.

Browser Support

:focus-visible is a browser CSS selector; server runtimes do not evaluate it.

✓ Baseline · Widely available

:focus-visible

Chrome 86+, Edge 86+, Firefox 85+, Safari 15.4+.

96% Modern browsers
Google Chrome Supported
Full support
Mozilla Firefox Supported
Full support
Apple Safari Supported
Full support
Microsoft Edge Supported
Full support
Opera Supported
Full support
Samsung Internet Supported
Full support
:focus-visible Widely supported

Bottom line: Safe for modern production sites. Test the interaction and cascade in the browsers your audience uses.

Key Takeaways

  • Purpose: matches a focused element when the browser determines that a visible focus indicator is helpful. This usually includes keyboard navigation, while a pointer click may match :focus without matching :focus-visible.
  • Syntax: E:focus-visible
  • Practice: keep selectors scoped and easy to override.
  • Testing: verify both matching and non-matching states.

One line: Use it for keyboard-friendly focus rings, but never leave focused controls without a visible indicator.

Frequently Asked Questions

It matches a focused element when the browser decides a visible focus indicator should be shown, commonly during keyboard navigation.
:focus matches every focused element. :focus-visible is conditional and follows browser heuristics about when a focus indicator is useful.
Usually keyboard navigation triggers it, but browsers may also show it for controls that require keyboard input or when user preferences call for it.
Only if you provide an equally visible replacement. A common pattern styles :focus-visible and leaves native behavior as a fallback.
Yes. Put a basic :focus rule first, then refine it with :focus-visible in supporting browsers.
Yes. Current Chrome, Edge, Firefox, and Safari support it.

Next: :focus-within Selector

Continue through the CSS tutorial reference.

:focus-within Selector tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

6 people found this page helpful