In the last lesson, :is() shortened your selector lists and :has() took over the free plots. One kind of repetition remains. Rules for the same component each start with the same selector. The event cards are the clearest case. Rosa wants the next event to stand out and each card to respond to the pointer, and in flat CSS, every one of those rules starts with .event-card again.
In this lesson, you’ll gather the card’s rules inside one .event-card block, attach states and classes to it with &, and read what each nested rule really stands for, specificity included. Start with the rule you already have for the card’s heading.
Put the Heading’s Rule Inside the Card
The card’s look is split across two rules in the components layer of garden-practice/style.css, and the second one repeats the first one’s selector:
.event-card {
background-color: white;
padding: var(--space-m) var(--space-l);
border: 2px solid var(--green);
border-radius: 8px;
box-shadow: 0 2px 8px oklch(0 0 0 / 0.2);
overflow-wrap: anywhere;
}
.event-card h3 {
margin-top: 0;
}
CSS nesting lets you write a rule inside another rule’s braces. If you’ve met nesting in Sass or another preprocessor, it’s the same idea, now built into the browser, with no build step. Replace the two rules with one, and move the heading’s rule inside the card’s:
.event-card {
background-color: white;
padding: var(--space-m) var(--space-l);
border: 2px solid var(--green);
border-radius: 8px;
box-shadow: 0 2px 8px oklch(0 0 0 / 0.2);
overflow-wrap: anywhere;
h3 {
margin-top: 0;
}
}
The browser joins the outer selector to the inner one with a space, so the nested rule means .event-card h3, the same selector as before. It’s a little like a relative path that’s resolved against the folder you’re in. The comparison stops at the separator. A path step means a direct child, while the space means any <h3> at any depth inside a card.