A well-made sign-up form often dims its Send button while the email you typed is still wrong. That kind of effect used to need JavaScript. Now one CSS selector does it.
Ordinary selectors pick from the outside in: .card img means "an image inside a card". :has() reverses that direction: it picks an element by what is inside it, for example "a card that has an image in it". Because it can pick a parent by its contents, :has() is often called the parent selector.
Pick an element by its contents
This example is a card with a checkbox inside it:
<label class="card">
<input type="checkbox"> Send me offers
</label>
<style>
.card:has(input:checked) {
background: #e0ffe0;
}
</style>
.cardis the element that gets the style. The background colour is set on.card, not on the checkbox.:has(...)is the condition: "that has ... inside it".input:checkedis what it looks for inside.card: an input that is ticked. The input can be anywhere inside.card, not only a direct child.
The result: when the checkbox is ticked, the whole card turns light green. When you untick it, the card is plain again.
Compare these two selectors:
.card imgstyles the image..card:has(img)styles the card that has an image. A card without an image gets no style.
React to what's in a form
This rule dims the button in a form that is not filled in correctly yet:
form:has(input:invalid) button {
opacity: 0.5;
}
input:invalid is an input whose value doesn't meet its rules yet, such as a type="email" holding abc, or a required input that is still empty. So form:has(input:invalid) picks a form with at least one input that isn't valid yet. The button after it picks the button inside that form, and opacity: 0.5 makes that button dim.
So the Send button from the start of this lesson can be made with no JavaScript. While any input isn't valid, the button stays dim. Once every input is valid, this rule no longer applies and the button looks normal again.
The opposite: :not(:has(...))
:not(...) means "that is not ...". Wrap :has() in :not() to pick elements that don't have something:
.cart:not(:has(li))::after {
content: "Your cart is empty";
}
A cart without a single <li> shows the message "Your cart is empty" through ::after. Once one item (one <li>) is added, that message disappears by itself.
⚠️ The wrong target, and rules dropped with no warning
- Styling the wrong element.
.card:has(img) { border: 2px solid; }puts the border on the card. If you want to change the image, use.card img. - A typo inside
:has(). One invalid selector inside:has()makes the browser ignore the whole rule, with no error message. :has()inside:has(). That isn't allowed, so the browser ignores the rule. Put the whole condition in one:has().
TL;DR: :has() picks an element by its contents. Because it can pick a parent by its contents, :has() is called the parent selector. .card:has(img) picks a card with an image, label:has(input:checked) picks a label whose checkbox is ticked, and form:has(input:invalid) picks a form with an input that isn't valid yet. Wrap :has() in :not() for the opposite: an element that does not have the contents named inside :has(). Many effects that used to need JavaScript now need only CSS.
⚡ Pick by contents
Write:
.card:has(input:checked) {
background: #e0ffe0;
}
Guess first: .card has a checkbox inside it. Is the background colour set on the checkbox or on the card?
On the card. :has() picks the element in front of it by what is inside it: here, a .card that has a ticked input (input:checked) inside it.
Meaning: When the checkbox is ticked, the whole card turns light green. Compare: .card img styles the image, while .card:has(img) styles the card that has an image.
⚡ A dim button with no JavaScript
Write:
form:has(input:invalid) button {
opacity: 0.5;
}
Meaning: input:invalid is an input whose value doesn't meet its rules yet, such as an email input holding abc. form:has(input:invalid) picks a form with at least one input that isn't valid yet, and button picks the button inside that form. opacity: 0.5 makes that button dim. Once every input is valid, the button looks normal again.
⚡ The opposite
Write:
.cart:not(:has(li))::after {
content: "Your cart is empty";
}
Meaning: :not(...) means "that is not ...". Wrap :has() in :not() to pick elements that don't have something. Here, a cart without an <li> shows the message "Your cart is empty". Once one item (one <li>) is added, the message disappears by itself.