CSS :focus-within Selector

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

Definition and Usage

The :focus-within selector matches an element when that element or any of its descendants has focus. It lets a parent react to a focused input, button, link, or other focusable descendant.

Baseline · Widely available.

The focused descendant still matches :focus; :focus-within adds a state to its ancestor chain.

CSS Syntax

syntax.css
E:focus-within{ … }

Selector Forms

FormDescriptionTry it
.field:focus-withinHighlight a field wrapperTry it
form:focus-withinStyle a form while any control has focusTry it
nav:focus-withinKeep keyboard navigation visually openTry it
.card:focus-withinEmphasize a card containing a focused linkTry it

How to Use :focus-within

  1. Choose the element or scope you want to match.
  2. Write the selector using E:focus-within.
  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-withinYesApplies 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-within matches.

:focus-within preview

Rule: .field:focus-within

Focusing the input causes its parent field wrapper to match.

Open live editor

CSS Examples

Each example isolates one practical :focus-within pattern.

Example 1 — Field Wrapper

Focusing the input causes its parent field wrapper to match.

CSS
<style>
  .field:focus-within {
    border-color: #2563eb;
      background: #eff6ff;
  }
</style>

<div class="field"><label>Name <input type="text"></label></div>
Try It Yourself

How It Works

  1. The browser evaluates .field:focus-within 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 — Form State

The form remains highlighted while focus is anywhere inside it.

CSS
<style>
  form:focus-within {
    box-shadow: 0 0 0 3px #99f6e4;
  }
</style>

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

How It Works

  1. The browser evaluates form:focus-within 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 — Navigation Group

Tabbing to either link styles the whole navigation region.

CSS
<style>
  nav:focus-within {
    background: #f0fdfa;
  }
</style>

<nav><a href="#one">One</a> <a href="#two">Two</a></nav>
Try It Yourself

How It Works

  1. The browser evaluates nav:focus-within 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-within is a browser CSS selector; server runtimes do not evaluate it.

✓ Baseline · Widely available

:focus-within

Chrome 60+, Edge 79+, Firefox 52+, Safari 10.1+.

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-within Widely supported

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

Key Takeaways

  • Purpose: matches an element when that element or any of its descendants has focus. It lets a parent react to a focused input, button, link, or other focusable descendant.
  • Syntax: E:focus-within
  • Practice: keep selectors scoped and easy to override.
  • Testing: verify both matching and non-matching states.

One line: The focused descendant still matches :focus; :focus-within adds a state to its ancestor chain.

Frequently Asked Questions

It matches an element if the element itself or any descendant currently has focus.
It styles whichever element appears before :focus-within. Use a separate :focus or :focus-visible rule for the child.
Yes. Every ancestor in the focused element's chain that has a matching :focus-within selector can match.
Yes. It is commonly used to highlight a label, field wrapper, fieldset, or entire form around the active control.
Yes. If JavaScript moves focus to a descendant, matching ancestors gain :focus-within.
Yes. It works in current major browsers and has broad production support.

Next: :fullscreen Pseudo-Class

Continue through the CSS tutorial reference.

:fullscreen Pseudo-Class 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