Guesstimate of what the code would look like
.section {
background: #eee;
border-bottom: 1px solid #ddd;
margin-bottom: 8px;
padding: 8px;
}
.box {
background: #fff;
border: 1px solid rgba(0, 0, 0, .5);
border-radius: 8px;
margin-bottom: .5em;
padding: 4px;
}
.section > .box:first-of-type:has(~ .box:nth-of-type(3)) {
background: #0f0;
}
.section > .box:first-of-type:not(:has(~ .box:nth-of-type(3))) {
background: #f00;
}
What's going on
This pen is intended to demonstrate how a developer could possibly target a preceding sibling using
:has()
.
If there is a third
.box
element ahead of the first
.box
element, the first .box would be shaded green.
If the CSS is unable to find a third
.box
sibling ahead of the first, it would be shaded red instead.
Emulating
.section > .box:first-of-type:has(~ .box:nth-of-type(3))
Box 1
Box 2
Box 3
Emulating
.section > .box:first-of-type:not(:has(~ .box:nth-of-type(3)))
Box 1
Box 2