Sub topic 6
Combining :has() with :not() and :is()
This chapter assumes you know basic selectors and combinators, the :not() and :is() pseudo-classes in simple form, and how :has() matches descendants, children, and siblings. Here you will combine them. The order you nest them in changes the meaning, and this is the most common source of errors.
Why combine them
:has() asks whether a relationship exists. :not() negates a condition. :is() groups several alternatives. Together they let you write rules like "a card with no image" or "a field that contains an input or a textarea that is invalid".
:not(:has()) versus :has(:not())
These two look similar but test different things.
.card:not(:has(img)) {
padding: 2rem;
}
.card:has(:not(img)) {
border: 1px solid #ccc;
}.card:not(:has(img))matches a card that contains noimgat any depth. The negation applies to the whole relationship..card:has(:not(img))matches a card that contains at least one descendant that is not animg. A card with an image and a paragraph matches. A card whose only descendant is an image does not.
Read the selector from the outside in. For :not(:has(img)), the outer test is "not", so the question is "does the card lack an image?". For :has(:not(img)), the outer test is "has", so the question is "does the card contain something that is not an image?".
Grouping arguments with :is()
Use :is() inside :has() when several element types should trigger the same rule.
.field:has(:is(input, textarea, select):invalid) {
outline: 2px solid crimson;
}Without :is(), you would repeat the rest of the argument three times. The :has() argument is already a selector list, so .field:has(input:invalid, textarea:invalid, select:invalid) is equivalent. Choose :is() when it shortens the selector and makes the shared part obvious.
You can also put :has() inside :is() to group the parents:
:is(article, section, aside):has(> h2) {
margin-block-start: 2rem;
}This styles any of the three container types, but only when it has a direct h2 child.
Combining with state
:not() pairs well with state pseudo-classes. This rule marks a field as invalid only after the user has typed something:
.field:has(input:invalid:not(:placeholder-shown)) label {
color: crimson;
}Here :not(:placeholder-shown) sits inside :has(), next to :invalid on the same input. Both conditions must be true of the same element. This requires the input to have a placeholder attribute, even a single space.
Specificity
:not(), :is(), and :has() all take the specificity of their most specific argument. They add nothing themselves.
/* (0,1,0) + (0,1,0) from :not(.x) = (0,2,0) */
.card:not(.x) { }
/* (0,1,0) + (0,0,1) from :has(img) = (0,1,1) */
.card:has(img) { }
/* (0,1,0) + (1,0,0) from :is(#a, .b) = (1,1,0) */
.card:is(#a, .b) { }The last line shows a trap. :is(#a, .b) counts as an ID even when the element matches through .b. Keep ID selectors out of these lists unless you want the extra weight.
Common mistakes
- Reading
:has(:not(x))as "has no x". It means "has something that is not x". - Putting
:has()inside another:has(). This is invalid, so the whole rule is dropped. Flatten the logic with:is()or:not()outside instead. - Expecting one invalid argument inside a
:has()list to be ignored. The selector list is not forgiving, so one bad argument invalidates the rule.
Exercise
Write a selector for a .list element that contains no li with the class done. Then say whether .list:has(li:not(.done)) is equivalent, and why or why not.
Show answer
Use .list:not(:has(li.done)). The selector .list:has(li:not(.done)) is not equivalent. It matches a list that has at least one li without the class done, so a list with one done item and one open item matches it, while the first selector rejects that list.
Exercise
What is the specificity of section:is(.a, #b):has(p)?
Show answer
(1,0,2). The type selector section adds (0,0,1). :is(.a, #b) takes its most specific argument, the ID, which adds (1,0,0). :has(p) adds (0,0,1).