css · level 3 · unit 3 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
lets a parent react to what is inside it.
For twenty years CSS could style an element by what it sits in, but never by what it holds. :has() changes that. .card:has(img) matches a card when an image sits anywhere inside it.
The element you style is the one before :has(). The selector in the brackets is only the test. So .card:has(img) styles the card, not the image.
Real uses: give cards with a photo a different border, mark a table row that holds a warning, or space a form out once it holds an error.
<div class="card">
<img src="cat.svg" alt="Pickles">
<p>Pickles, 3 years</p>
</div>
<div class="card">
<p>Name to come</p>
</div> .card {
padding: 12px;
border: 3px solid #e5e7eb;
}
.card:has(img) {
border-color: #0f766e;
} Press show my page to see it here.
guess first
Which element does this rule colour?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<ul class="tasks">
<li>Buy milk</li>
<li>Call the vet <strong>today</strong></li>
</ul> li:has(strong) {
background-color: gold;
}
your turn
Your turn. Flag orders that need a signature.
Fill in the selector so any order row holding a .sign badge gets a gold left border. The other rows stay grey.
the page you’re styling
<div class="order">Order 1041</div>
<div class="order">Order 1042 <span class="sign">Sign</span></div>
<div class="order">Order 1043</div>your css
Press show my page to see it here.
- order 1042 has a gold bordernot checked yet
- order 1041 stays greynot checked yet
- order 1043 stays greynot checked yet
fix it
Only the picture frame turned yellow. Can you fix it?
the page you’re styling
<div class="card">
<figure>
<img src="dog.svg" alt="Rex">
</figure>
<p>Rex, 5 years</p>
</div>your css
Press show my page to see it here.
- the whole card is yellownot checked yet
- the frame has no fill of its ownnot checked yet
- Something inside the card turned yellow, not the card itself. Look at the gap in the selector.
- A space means inside, so
.card :has(img)styles elements inside the card that hold an image. - Remove the space,
.card:has(img), then press run.
nailed it
you let a parent see inside itself. :D
:has()matches an element by what it contains.- The rule styles the element written before
:has(). - No space between the element and
:has().
new words in your collection
- :has()
This lesson isn’t saved. A free account remembers every lesson you finish.