Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.5 — :not, :is, :where and :has
CSS · Example 3 of 3 Baseline 2023+

:has(): the parent and previous-sibling selector

Tick or untick a box: the label restyles with no JavaScript, because :has(input:checked) is re-checked as the state changes.

Four functional pseudo-classes add logic to selectors: :not() excludes, :is() and :where() say "any of these" (with full or zero specificity), and :has() selects an element by what it contains or what follows it, the long-awaited parent selector.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Give every button except those with class ghost a solid blue background, in one rule.

02

Write a reset that removes list bullets and padding from any ul or ol that has a class, with zero specificity so any later rule can add them back.

03

Highlight a .field wrapper with a red left border when the input inside it is invalid.

Explain it without notes

01

Compare :is() and :where(): how do they match, and how does their specificity differ?

02

What is the specificity of .btn:not(#primary)? Why can that cause trouble?

03

What problem does :has() solve, and give three practical examples.

04

What is a forgiving selector list, and which of these four functions has one?

:has(): the parent and previous-sibling selector Baseline 2023+

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

cards with a picture: 1
ticked options: 1
headings before a paragraph: 1