Sub topic 4
Looking ahead: siblings and previous-sibling selection
This chapter assumes you know the adjacent sibling (+) and general sibling (~) combinators. It also assumes you have read the previous chapters on :has() syntax and on matching descendants and direct children.
Why siblings matter
The sibling combinators + and ~ only look forward. In h2 + p, the selected element is the p, which comes after the h2. Before :has(), CSS had no way to select the h2 because of the p that follows it.
:has() removes that limit. The element to the left of :has() is the subject, which is the element that receives the styles. The relative selector inside the parentheses can start with + or ~, so it looks at the subject's following siblings.
Immediate and later siblings
Here is a pair of rules that style an earlier sibling based on a later one:
h2:has(+ p) {
margin-block-end: 0.25rem;
}
h2:has(~ ul) {
color: rebeccapurple;
}h2:has(+ p)matches anh2whose very next element sibling is ap.h2:has(~ ul)matches anh2that has aulanywhere after it, among the same parent's children.
Given this markup, both rules apply to the first h2 only:
<section>
<h2>Ingredients</h2>
<p>You need three things.</p>
<ul>
<li>Flour</li>
</ul>
<h2>Method</h2>
<div>Steps go here.</div>
</section>The second h2 has no p right after it, and no ul follows it. The first ul comes before it, and ~ never looks backward.
Selecting previous siblings
:has() also gives you what CSS lacked for years: selecting elements that come before a given sibling. Put the later element inside :has() and style the earlier one.
li:has(~ li.selected) {
opacity: 0.5;
}This dims every li that has a li.selected somewhere after it. In a step indicator, that styles all completed steps. A star rating works the same way. With .star:has(~ .star:hover), hovering one star lights up the stars before it.
Subject versus target
The most common error is mixing up what is selected and what is only tested. Compare these:
| Selector | Styled element | Condition |
|---|---|---|
| `h2 + p` | the `p` | it directly follows an `h2` |
| `h2:has(+ p)` | the `h2` | a `p` directly follows it |
The element before :has() always receives the styles. The argument only tests for a match.
Details that cause bugs
- Only element siblings count. Whitespace, text nodes, and comments between elements are ignored, so
h2:has(+ p)still matches when a line break separates the two tags. - Both elements must share a parent.
h2:has(~ ul)never matches aulthat sits inside another container. ~is not symmetrical.:has(~ x)matches an element withxafter it, never before it.- Siblings can be chained.
h2:has(+ p + ul)matches anh2followed immediately by ap, which is followed immediately by aul.
Combining with what you know
You can put any compound selector after the combinator. For example, label:has(+ input:required) styles a label whose next sibling is a required input. Later chapters use this to build form states.
Exercise
A step list has li items, and one has the class current. Write a selector that matches only the items before li.current, and explain why li.current ~ li does not do this.
Show answer
Use li:has(~ li.current). The subject is each li that has li.current among its later siblings, so only earlier items match. li.current ~ li selects the items after the current one, because ~ only looks forward from the left-hand element.
Exercise
Given <h3>Title</h3><!-- note --><p>Text</p>, does h3:has(+ p) match the h3? Why?
Show answer
Yes. Comments and whitespace are not element siblings, so the p is still the next element sibling of the h3.