:focus-within
Quick Summary for :focus-within
The :focus-within CSS pseudo-class matches an element if the element or any of its descendants are focused. In other words, it represents an element that is itself matched by the :focus pseudo-class or has a descendant that is matched by :focus. (This includes descendants in shadow trees.)
Code Usage for :focus-within
/* Selects a <div> when one of its descendants is focused */ div:focus-within {   background: cyan; } 
More Details for :focus-within

:focus-within

The :focus-within CSS pseudo-class matches an element if the element or any of its descendants are focused. In other words, it represents an element that is itself matched by the :focus pseudo-class or has a descendant that is matched by :focus. (This includes descendants in shadow trees.)

/* Selects a <div> when one of its descendants is focused */ div:focus-within {   background: cyan; } 

This selector is useful, to take a common example, for highlighting an entire <form> container when the user focuses on one of its <input> fields.

Syntax

:focus-within

Examples

In this example, the form will receive special coloring styles when either text input receives focus.

HTML

<p>Try typing into this form.</p>  <form>   <label for="given_name">Given Name:</label>   <input id="given_name" type="text">   <br>   <label for="family_name">Family Name:</label>   <input id="family_name" type="text"> </form> 

CSS

form {   border: 1px solid;   color: gray;   padding: 4px; }  form:focus-within {   background: #ff8;   color: black; }  input {   margin: 4px; } 

Result

Specifications

Specification
Selectors Level 4 # the-focus-within-pseudo

See also

:focus :focus-visible Grab your user's attention with the focus-within selector

Last modified: Aug 12, 2021, by MDN contributors

Select your preferred language English (US)EspañolFrançais日本語한국어Português (do Brasil)Русский中文 (简体) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

You could also follow me on twitter. I have a couple of youtube channels if you want to see some video related content. RuneScape 3, Minecraft and also a coding channel here Web Dev.

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
When I realized that, no individual step is hard in any process. Building this airport I'm standing in right now started with a guy writing the architectural plans on paper. That's not hard for him to do. Then laying the first beam isn't had. The whole thing is really hard. So, just take each step kind of piece by piece and when I was able to do that and stop trying to chase this prize and started putting in the work, things just started coming together.
Unknown
Random CSS Property

background-repeat

The background-repeat CSS property sets how background images are repeated. A background image can be repeated along the horizontal and vertical axes, or not repeated at all.
background-repeat css reference