Pseudo-Classes, :is(), :where() & :has()
Style interactive states cleanly, simplify verbose selector lists with :is() and :where(), and style parent containers based on their children with :has().
What you'll learn
- Style a parent element based on its children or checked inputs using :has()
- Group complex selectors concisely with :is() and zero-specificity :where()
- Add decorative accents cleanly using ::before and ::after pseudo-elements
Modern CSS selectors eliminate dozens of JavaScript helper classes.
The :has() “Parent Selector”
Want to highlight a card only when the checkbox inside it is checked, with zero JavaScript?
.plan-card:has(input:checked) {
border-color: #2563eb;
background: #eff6ff;
}
:is() vs. :where()
Both simplify repetitive selector lists like :is(h1, h2, h3) a { ... }, with one key difference:
:is(...)takes the specificity of its most specific argument.:where(...)always has zero specificity(0, 0, 0), making it ideal for base/reset styles that are easy to override!
Fill-in-the-Blank Exercise
Modern Selector Practice
Highlight .option whenever its inner checkbox is checked, and use a zero-specificity selector for headings.
.option:(input:checked) {
border-color: #2563eb;
}
:(h1, h2, h3) {
margin-top: 0;
}Try It Yourself
Check and uncheck the tasks below, the card border, background, and strikethrough are powered 100% by CSS :has()!
Check Your Understanding
1. What is the specificity of :where(.card, #hero, h1)?
Finished this lesson?
Your progress is saved locally in your browser, no account needed.