Skip to content

Lesson 23 of 52Modern CSS

Modern CSS Features

Nesting, :has(), :is(), :where(), logical properties, @layer, @scope, field-sizing and colour functions: newer CSS that replaces old hacks.

20 min read0/7 questions

CSS has changed more in the last few years than in the ten before. Things that used to need a preprocessor like Sass, a JavaScript library, or a clever hack are now built into every modern browser. You can nest rules, style a parent based on what it contains, keep images in shape without padding tricks, and decide the order of your whole stylesheet in one line. This lesson tours the features you will meet in modern codebases, shows each one live, and tells you how well browsers support it.

Native nesting

In plain CSS you used to repeat the parent selector over and over: .card, then .card h2, then .card:hover. Nesting lets you write rules inside other rules, the way Sass did for years. The browser now understands it directly.

HTML & CSS
<style> .card { padding: 16px; border: 2px solid #222; border-radius: 12px; max-width: 260px; h2 { margin: 0 0 8px; font-size: 20px; } p { margin: 0; color: #555; } &:hover { background: #fff6d6; } &.featured { border-color: #e4572e; } @media (min-width: 500px) { max-width: 320px; } }</style><div class="card featured"> <h2>Lemon tart</h2> <p>Bright, sharp, and ready in 40 minutes.</p></div>

Preview

The & symbol means "the selector I am nested inside". So &:hover becomes .card:hover, and &.featured (no space) becomes .card.featured, a card that also has the featured class. Without the &, a nested selector is treated as a descendant: h2 inside .card means .card h2. You can even nest a media query, which keeps a component's responsive rules right next to it.

Try removing the featured class from the div and watch the orange border go. Then hover the card.

Nesting reached all modern browsers (Chrome, Edge, Firefox and Safari) in December 2023 and became Baseline Widely available in June 2026. One warning: just because you can nest five levels deep does not mean you should. Every level adds specificity and ties your CSS to the exact shape of your HTML. One or two levels is plenty.

Quick check
Inside a .button { ... } rule, what does the nested selector &.primary select?
Need a nudge? A hint rules out a wrong answer.

:is() and :where(): grouping without repetition

Say you want the same style for links inside your header, nav and footer. The old way was a long comma list: header a, nav a, footer a. The :is() pseudo-class takes a list of selectors and matches anything that matches any of them:

:is(header, nav, footer) a {
  color: inherit;
  text-decoration: none;
}

:where() does exactly the same matching. The only difference is specificity, the score the browser uses to decide which rule wins when two rules disagree.

Pseudo-classMatchesSpecificity
:is(.a, #b)anything matching .a or #bthe most specific item in the list (here, an id)
:where(.a, #b)the same elementsalways zero

That zero is very useful. Base styles wrapped in :where() are as easy to override as anything can be: a single class beats them. Many CSS resets use :where() for exactly this reason.

Predict the outputHTML & CSS

What colour is the paragraph's text? Type the colour name.

/* HTML: <p class="note">Hello</p> */

:where(p.note) { color: red; }
p { color: blue; }
Need a nudge? Hints walk you through it.

:has(): the parent selector

For about twenty years developers asked for a way to style an element based on what is inside it. :has() finally does that. .card:has(img) selects every card that contains an image. form:has(input:invalid) selects a form that has at least one invalid field.

Here is a pricing picker. There is no JavaScript: the chosen plan lights up because its label contains a checked radio button.

HTML & CSS
<style> .plans { display: flex; gap: 12px; flex-wrap: wrap; } .plan { flex: 1; min-width: 120px; padding: 16px; border: 2px solid #ccc; border-radius: 12px; cursor: pointer; } .plan:has(input:checked) { border-color: #2a6fdb; background: #eaf1fd; } .plan strong { display: block; font-size: 22px; }</style><div class="plans"> <label class="plan"><input type="radio" name="plan"> Starter <strong>$0</strong></label> <label class="plan"><input type="radio" name="plan" checked> Pro <strong>$9</strong></label> <label class="plan"><input type="radio" name="plan"> Team <strong>$29</strong></label></div>

Preview

Click the other plans. Then add a rule like .plans:has(input:checked) .plan:not(:has(input:checked)) { opacity: 0.6; } to fade the plans you did not pick.

:has() works in every modern browser since December 2023, when Firefox shipped it, and it has been Baseline Widely available since June 2026. It can also look at siblings: h2:has(+ p) selects an h2 that is directly followed by a paragraph.

Quick check
Which selector styles a .field wrapper red when the input inside it is invalid?
Need a nudge? A hint rules out a wrong answer.

Logical properties

Properties like margin-left and padding-top describe physical directions. That breaks when your page is translated into a right-to-left language like Arabic or Hebrew, where "the start of the line" is on the right. Logical properties describe directions relative to the flow of text instead:

  • The inline direction is the way a line of text runs (left to right in English).
  • The block direction is the way paragraphs stack (top to bottom).
  • Start and end are the beginning and end of each direction.
Physical (old)Logical (new)
margin-leftmargin-inline-start
margin-left and margin-rightmargin-inline
padding-top and padding-bottompadding-block
widthinline-size
heightblock-size
left (with positioning)inset-inline-start
text-align: lefttext-align: start

margin-inline: auto is a modern way to centre a block, and padding-block: 24px sets top and bottom padding in one go. In English they behave just like the physical versions, so you lose nothing by using them, and your layout flips correctly if someone adds dir="rtl" to the page. All modern browsers support them.

Match the pairs0/4 matched

Match each logical property to what it controls on an English page.

Choose an item, then its match on the other side.

aspect-ratio and accent-color

Two small properties remove a lot of old pain.

aspect-ratio keeps a box in proportion. Before it existed, people used a "padding-top: 56.25%" trick to make a 16 by 9 video frame. Now you write aspect-ratio: 16 / 9 and set a width. It is perfect for video embeds, avatar circles (aspect-ratio: 1) and image cards combined with object-fit: cover.

accent-color sets the colour of built-in form controls: checkboxes, radio buttons, range sliders and progress bars. Restyling a checkbox used to mean hiding it and drawing a fake one. Now one line matches your brand, in browsers that support it.

HTML & CSS
<style> body { --brand: #e4572e; } .thumb { width: 220px; aspect-ratio: 16 / 9; background: linear-gradient(135deg, #ffd166, #e4572e); border-radius: 10px; display: grid; place-items: center; color: white; font-weight: bold; } input { accent-color: var(--brand); }</style><div class="thumb">16 / 9</div><p><label><input type="checkbox" checked> Email me new recipes</label></p><p><label>Spice level <input type="range" value="70"></label></p>

Preview

Change the ratio to 1 to get a square, or 4 / 5 for a portrait card. Change --brand to #2a6fdb and the checkbox and slider turn blue.

The two properties have very different support stories, and it is a good lesson in reading compatibility data. aspect-ratio has been in every major browser since 2021 and is Baseline Widely available. accent-color shipped in Chrome and Firefox in 2021, and Safari has accepted it since 15.4, but the compatibility data marks both Safari before 26.2 and Chrome on Android as partial: they do not adjust the control to keep it legible when your accent colour has poor contrast. Because of that, the web-features data (version 3.44, October 2026) does not list accent-color as Baseline. That is fine for this use: where it is unsupported or partial, controls fall back to their default colours and still work. Treat it as an enhancement, and pick an accent colour with enough contrast yourself rather than relying on the browser to fix it.

@layer: deciding who wins in advance

In a big project, styles come from many places: a reset, your base styles, component styles, third-party widgets, utilities. Without a plan, the winner of every conflict depends on specificity and file order, which is fragile.

Cascade layers let you name groups of styles and set their priority once:

@layer reset, base, components, utilities;
 
@layer reset {
  * { margin: 0; }
}
 
@layer components {
  .card #title { color: navy; }
}
 
@layer utilities {
  .text-red { color: red; }
}

The first line sets the order. Later layers beat earlier layers, no matter the specificity. So .text-red in utilities beats .card #title in components, even though an id selector normally crushes a class. Specificity still matters, but only between rules inside the same layer.

How cascade layers stack up
  1. Unlayered stylesplain CSS outside any @layer
  2. @layer utilitiesdeclared last
  3. @layer componentsdeclared third
  4. @layer basedeclared second
  5. @layer resetdeclared first, weakest

The first layer named in @layer reset, base, components, utilities; has the lowest priority.

1/4

Tailwind CSS version 4, which you will meet soon, is built on cascade layers: its output is organised into theme, base, components and utilities layers. @layer is supported in all modern browsers since 2022.

Quick check
With @layer base, components; declared, #hero h1 { color: black } sits in base and .title { color: purple } sits in components. The h1 has class title. What colour is it?
Need a nudge? A hint rules out a wrong answer.

@scope: styles with a boundary

@layer decides which group of rules wins. @scope decides where rules apply at all. It limits a block of selectors to a subtree of the page, and it can also set a lower boundary where the scope stops.

@scope (.card) to (.card-content) {
  p { margin: 0; color: #555; }
  img { border-radius: 8px; }
}

Read it as: "inside .card, but not inside .card-content". The p rule styles the card's own paragraphs and leaves alone any paragraph someone puts into the content slot. That hole in the middle is why people call it a donut scope. Before @scope, the closest you could get was strict class naming such as BEM, which only works if everyone follows it.

Two more behaviours matter:

  • Proximity. When two scoped rules with equal specificity conflict, the one whose scope root is closer to the element wins, before source order is considered. A light card inside a dark panel takes the card's colours even if the panel's rules come later in the file.
  • No extra specificity. The scope root does not add to the selectors inside. p in the example above still scores like a plain p. Inside the block, :scope matches the root itself.

You can also write @scope with no selector inside a <style> element. The scope is then the style element's parent, which is a neat way to keep a one-off component's CSS next to its HTML:

HTML & CSS
<style> p { color: #b00020; }</style><div class="card" style="border: 2px solid #222; padding: 12px; max-width: 280px;"> <style> @scope { :scope { background: #eef4ff; } p { color: #1d4ed8; font-weight: 600; margin: 0 0 8px; } } </style> <p>Inside the scope: blue.</p> <div class="card-content" style="border: 1px dashed #888; padding: 8px;"> <p>Also inside the scope, for now.</p> </div></div><p>Outside the scope: still red.</p>

Preview

The paragraph outside the card keeps the global red. Change the inner @scope to @scope to (.card-content) (the root is still the style element's parent) and the paragraph in the dashed box turns red again, because the donut now cuts it out.

@scope reached the last engine when Firefox 146 shipped it in December 2025, and with Safari 26.4 it became Baseline Newly available in March 2026. Users on older browsers ignore the whole block, so do not put essential layout in it yet. CSS Architecture for Design Systems shows how a component library uses it together with custom properties and container queries.

Quick check
With @scope (.article) to (.comments) { a { color: green; } }, which links turn green?
Need a nudge? A hint rules out a wrong answer.

text-wrap: balance and pretty

Two values of text-wrap improve line breaks with one line each, and you met them in Typography. text-wrap: balance evens out the lines of short blocks such as headings, so a title does not end with one lonely word. text-wrap: pretty asks the browser to avoid a very short last line in paragraphs.

Their support differs, which is worth knowing in an interview. balance is Baseline Newly available (since 2024). pretty works in Chrome, Edge and Safari but not yet in Firefox, so it is not Baseline. Both are perfect enhancements: a browser that does not understand them simply wraps text the normal way, so you can use them today without a fallback.

field-sizing: content

A comment box that grows as you type used to need JavaScript that measured scrollHeight on every keystroke. field-sizing: content makes a form control size itself to its content instead of a fixed size.

HTML & CSS
<style> textarea, input, select { font: inherit; padding: 8px; border: 2px solid #777; border-radius: 6px; } textarea { field-sizing: content; min-block-size: 3lh; max-block-size: 10lh; inline-size: 100%; max-inline-size: 360px; } .grow { field-sizing: content; min-inline-size: 6ch; } label { display: block; font-weight: 600; margin-top: 10px; }</style><label for="comment">Comment</label><textarea id="comment">Type a few lines here and watch the box grow.</textarea> <label for="tag">Tag</label><input id="tag" class="grow" value="css">

Preview

Press Enter a few times in the comment box: it grows, then stops at ten lines and starts scrolling. Type into the tag field and it widens to fit. Remove the max-block-size line and the box grows without limit, which is why you almost always set one. The lh unit is one line height, so 3lh means "at least three lines tall".

Support is newer than most of this lesson. Chrome has had it since 2024 and Safari since 26.2, and Firefox 152 completed the set, making it Baseline Newly available in June 2026. In browsers without it, the textarea keeps its normal fixed size and its scrollbar, which is a perfectly usable fallback, so you can ship it as an enhancement.

color-mix() and relative colour syntax

Two colour functions let CSS derive colours from your tokens instead of storing every shade by hand. You already used one in CSS Variables and Theming.

  • color-mix(in oklch, var(--brand), white 30%) mixes two colours: here 70% brand and 30% white, a lighter tint. It is Baseline Widely available.
  • Relative colour syntax rewrites the channels of an existing colour: oklch(from var(--brand) calc(l - 0.1) c h) is the brand colour a little darker, and rgb(from var(--brand) r g b / 0.2) is the brand at 20% opacity. It has been Baseline Newly available since September 2024.
HTML & CSS
<style> :root { --brand: #e4572e; } .swatches { display: flex; gap: 8px; flex-wrap: wrap; } .swatches div { inline-size: 90px; block-size: 70px; display: grid; place-items: end start; padding: 4px; font: 12px/1.2 monospace; } .base { background: var(--brand); color: white; } .tint { background: color-mix(in oklch, var(--brand), white 40%); } .shade { background: oklch(from var(--brand) calc(l - 0.15) c h); color: white; } .glass { background: rgb(from var(--brand) r g b / 0.2); }</style><div class="swatches"> <div class="base">brand</div> <div class="tint">mix 40% white</div> <div class="shade">l - 0.15</div> <div class="glass">20% alpha</div></div>

Preview

Change --brand to #2a6fdb and every swatch follows, because each is computed from the one token. Mixing in oklch rather than srgb keeps tints from going gray or shifting hue as much. CSS Architecture for Design Systems covers OKLCH, hover and disabled states built this way, and the pitfalls of mixing neutral grays.

Checking support before you use something

Most features in this lesson work in current versions of Chrome, Edge, Firefox and Safari, but "works in the current version" is not the same as "safe to rely on". Some users run old browsers, and some features are only partly supported. The industry shorthand is Baseline: a feature is Newly available once it works in the current version of every major browser on desktop and mobile, and Widely available 30 months later, when older browsers in use have caught up. Cross-Browser CSS and Progressive Enhancement explains how teams turn that into a support policy.

Here is where this lesson's features stood in the web-features data (version 3.44) in October 2026. Support changes every few months, so check again before you quote it.

FeatureBaseline status
:is(), :where(), logical properties, aspect-ratio, @layer, @supportsWidely available
Native nesting, :has()Widely available (since June 2026)
color-mix()Widely available (since November 2025)
text-wrap: balance, relative colour syntaxNewly available (2024)
@scopeNewly available (March 2026)
field-sizingNewly available (June 2026)
accent-colorNot Baseline: partial in Chrome on Android and in Safari before 26.2
text-wrap: prettyNot Baseline: no Firefox support yet
Anchor positioningNot Baseline as a whole feature: the core properties work in all three engines since January 2026, but parts of it do not yet

Anchor positioning is the one people get wrong most often, because the headline properties do work everywhere now. Dialogs, Popovers and Anchored UI Without a Library shows how to use it as an enhancement with a sensible fallback.

Two habits help:

  1. Look the feature up on MDN, Can I Use or webstatus.dev before relying on it. MDN shows the Baseline badge, and Can I Use lists the known bugs and partial implementations in its notes.
  2. Use @supports to add an enhancement only where it works:
.gallery { display: block; }
 
@supports selector(:has(img)) {
  .gallery:has(img:hover) img:not(:hover) { opacity: 0.6; }
}

The page still works without the extra effect. That approach is called progressive enhancement: a solid base for everyone, extras for browsers that can handle them.

Quick check
Your team's policy allows Baseline Widely available features freely and everything else only as an enhancement. Which of these can you use with no fallback at all?
Need a nudge? A hint rules out a wrong answer.
Key terms
nesting
Writing CSS rules inside other rules. & stands for the parent selector.
:has()
A pseudo-class that matches an element if it contains (or is followed by) something matching the selector inside.
:where()
Matches any selector in its list with zero specificity. :is() does the same but keeps the highest specificity.
logical property
A property named by text flow (inline, block, start, end) instead of physical sides, like margin-inline-start.
aspect-ratio
Keeps a box in a fixed width-to-height proportion, such as 16 / 9.
cascade layer
A named group of styles made with @layer. Later layers beat earlier ones regardless of specificity.
progressive enhancement
Building a solid base that works everywhere, then adding extras where the browser supports them.
@scope
An at-rule that limits selectors to a subtree, optionally with a lower boundary that cuts a hole in the scope (a donut).
field-sizing
A property that lets form controls such as textarea size themselves to their content.
Baseline
A status from the web-features project: Newly available when every major browser supports a feature, Widely available 30 months later.
color-mix()
A function that mixes two colours in a chosen colour space, such as color-mix(in oklch, var(--brand), white 30%).