AdvancedLesson 25 of 306 min read

Modern CSS selectors: :is, :where, :not, :has and nesting

Write shorter, smarter CSS with :is(), :where(), :not(), the parent selector :has(), nth-child formulas and native nesting.

In this lesson you will

  • Group selectors with :is() and :where()
  • Exclude with :not()
  • Style parents with :has()
  • Use native nesting

About 16 minutes

Shorter, smarter selectors

Think of selectors as questions you ask the page. In the beginner lessons you asked simple ones: all paragraphs, everything with this class. Modern CSS lets you ask far smarter questions: “every heading inside either the header or the footer”, “this card, but only if it contains an image”, “the form, but only when one of its fields is invalid”. These powers arrived in the last few years and are now supported by all major browsers. They make stylesheets shorter, and they remove many jobs that used to need JavaScript.

:is(): group without repeating

Without it, styling links in three places means writing three long selectors.

/* before */
header a:hover, nav a:hover, footer a:hover { color: #5A28C8; }

/* after */
:is(header, nav, footer) a:hover { color: #5A28C8; }

:is() takes a list of selectors and matches an element that fits any of them. It is especially valuable when you need the same pattern at several levels: :is(ul, ol) :is(ul, ol) li for nested lists.

:where(): the same, with zero specificity

:where(.card, .panel) h3 { margin-top: 0; }

:where() works exactly like :is(), except that it adds nothing to the specificity of the selector. That makes it perfect for base styles you intend to override easily. Think of it as writing defaults that never get in the way. For a reminder of how specificity counts, look back at the cascade lesson.

:not(): everything except

li:not(:last-child) { border-bottom: 1px solid #E6E1F5; }
a:not([class]) { text-decoration: underline; }
input:not([type="checkbox"], [type="radio"]) { width: 100%; }

The first line draws a divider between list items, but not after the last one. The second styles only plain links with no class. The third gives a full width to text inputs while leaving checkboxes alone. It accepts a list, so you can exclude several things at once.

Use :not(:last-child) to draw a bottom border under every list item except the last one.

<style>
 ul { list-style:none; padding:0; font-family:Arial; width:240px; background:#fff; border-radius:12px; box-shadow:0 4px 14px rgba(0,0,0,.1); }
 li { padding:12px 16px; /* border rule */ }
 body { background:#F4F1FC; padding:20px; }
</style>
<ul><li>HTML</li><li>CSS</li><li>JavaScript</li><li>Python</li></ul>

li:not(:last-child) { border-bottom: 1px solid #E6E1F5; }

<style>
 ul { list-style:none; padding:0; font-family:Arial; width:240px; background:#fff; border-radius:12px; box-shadow:0 4px 14px rgba(0,0,0,.1); }
 li { padding:12px 16px; }
 li:not(:last-child) { border-bottom: 1px solid #E6E1F5; }
 body { background:#F4F1FC; padding:20px; }
</style>
<ul><li>HTML</li><li>CSS</li><li>JavaScript</li><li>Python</li></ul>

:has(): the parent selector

For twenty years, CSS could only look downwards. You could style a child based on its parent, but never a parent based on its child. :has() changed that. It selects an element if it contains something that matches.

/* a card that contains an image gets a different layout */
.card:has(img) { display: grid; grid-template-columns: 120px 1fr; }

/* a label turns pink when its input is invalid */
label:has(input:user-invalid) { color: #D63384; }

/* highlight a whole row when its checkbox is ticked */
tr:has(input:checked) { background: #F1EDFE; }

/* style a section only if it is empty */
.notes:not(:has(*)) { display: none; }

The possibilities are huge. You can switch the page theme when a checkbox is ticked, change a form’s look when any field is invalid, or tweak a layout depending on how many items it holds. Things that once needed JavaScript to add classes now take one line of CSS.

Style the whole form row in violet when its checkbox is checked, using label:has(input:checked). Tick the box in the result to see it.

<style>
 body { padding:20px; font-family:Arial; }
 label { display:flex; align-items:center; gap:10px; padding:14px 16px; border:2px solid #E6E1F5; border-radius:12px; cursor:pointer; }
 /* has rule here */
</style>
<label><input type="checkbox"> I want the weekly newsletter</label>

label:has(input:checked) { border-color: #5A28C8; background: #F1EDFE; }

<style>
 body { padding:20px; font-family:Arial; }
 label { display:flex; align-items:center; gap:10px; padding:14px 16px; border:2px solid #E6E1F5; border-radius:12px; cursor:pointer; }
 label:has(input:checked) { border-color: #5A28C8; background: #F1EDFE; }
</style>
<label><input type="checkbox"> I want the weekly newsletter</label>

Quantity and position tricks

/* style an item when it is one of exactly three */
li:first-child:nth-last-child(3), li:first-child:nth-last-child(3) ~ li { width: 33.33%; }

/* every third item */
.grid > :nth-child(3n) { margin-right: 0; }

/* the first two */
li:nth-child(-n + 2) { font-weight: 700; }

/* odd and even rows */
tr:nth-child(odd) { background: #FAF8FF; }

The nth-child formulas read as an + b: 2n is every second item, 3n+1 is the first, fourth, seventh, and -n+2 is the first two. The of keyword filters first: :nth-child(2 of .visible) counts only visible items.

CSS nesting: less repetition

You may have seen preprocessors like Sass let you nest rules. Native CSS now supports nesting too, so you can group related rules without extra tools.

.card {
  padding: 20px;
  border-radius: 16px;

  h3 { margin: 0 0 .5rem; }

  a { color: inherit; }

  &:hover { box-shadow: 0 10px 24px rgba(20,17,46,.15); }

  &.featured { border: 2px solid #5A28C8; }

  @media (min-width: 700px) { padding: 28px; }
}
  • Inside a nested block, a plain selector like h3 means “an h3 inside .card”.
  • & stands for the parent selector. &:hover is .card:hover, and &.featured is .card.featured.
  • Media queries can sit inside a rule, right next to what they change, which keeps responsive tweaks easy to find.
Good to know

Do not nest deeper than two or three levels. Each level adds specificity and makes the stylesheet harder to read. If you need more, split the component.

Other handy selectors

  • :focus-within matches a parent when anything inside has focus. Great for highlighting a form group.
  • :empty matches elements with no children.
  • :target matches the element whose id is in the page address after the hash, useful for highlighting a section you jumped to.
  • ::selection styles highlighted text.
  • :user-invalid and :user-valid style form fields only after the user has interacted with them.
  • accent-color: #5A28C8 tints checkboxes, radio buttons and sliders in one line.
Common mistake

Using :has() with very broad selectors such as body:has(*) on large pages. It can be slow. Keep the scope tight and the condition specific.

Check browser support

These features are available in all current browsers, but if you must support older ones, wrap a modern rule in @supports selector(:has(*)) { ... } so it only applies where it works.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What does :is(header, footer) a select?

  1. Links in header or footer
  2. Only headers
  3. Only links in the body
  4. Nothing

is() matches any of the listed selectors.

What is the main difference between :is() and :where()?

  1. None
  2. :where() adds no specificity
  3. :is() is faster
  4. :where() is invalid

where() keeps specificity at zero, so it is easy to override.

What does .card:has(img) select?

  1. An image inside a card
  2. A card that contains an image
  3. An image with the class card
  4. Nothing

has() selects the element that contains the match.

What does & mean inside a nested rule?

  1. The parent selector
  2. An error
  3. AND logic
  4. A comment

& refers to the parent selector.

Which selector styles every second item?

  1. :nth-child(2n)
  2. :nth-child(2)
  3. :first-child
  4. :every(2)

2n matches the 2nd, 4th, 6th and so on.

Summary

  • :is() and :where() shorten repeated selectors.
  • :not() excludes things.
  • :has() styles a parent based on its children.
  • Nesting groups related rules, but keep it shallow.

Next lesson

Next: container queries, components that respond to their own space.

Questions

Is :has() safe to use?

Yes in all current browsers. Use @supports if you must support very old ones.

Does nesting replace Sass?

For nesting, yes. Sass still offers other features like mixins and loops.

Why does my nested rule not work?

Check that the parent selector is right and that & is used for states like :hover.

Progress is saved in this browser.