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.
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.
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.
.button { ... } rule, what does the nested selector &.primary select?: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-class | Matches | Specificity |
|---|---|---|
:is(.a, #b) | anything matching .a or #b | the most specific item in the list (here, an id) |
:where(.a, #b) | the same elements | always 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.
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; }: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.
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.
.field wrapper red when the input inside it is invalid?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-left | margin-inline-start |
margin-left and margin-right | margin-inline |
padding-top and padding-bottom | padding-block |
width | inline-size |
height | block-size |
left (with positioning) | inset-inline-start |
text-align: left | text-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 each logical property to what it controls on an English page.
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.
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.
The first layer named in @layer reset, base, components, utilities; has the lowest priority.
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.
@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?@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.
pin the example above still scores like a plainp. Inside the block,:scopematches 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:
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.
@scope (.article) to (.comments) { a { color: green; } }, which links turn green?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.
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, andrgb(from var(--brand) r g b / 0.2)is the brand at 20% opacity. It has been Baseline Newly available since September 2024.
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.
| Feature | Baseline status |
|---|---|
:is(), :where(), logical properties, aspect-ratio, @layer, @supports | Widely available |
Native nesting, :has() | Widely available (since June 2026) |
color-mix() | Widely available (since November 2025) |
text-wrap: balance, relative colour syntax | Newly available (2024) |
@scope | Newly available (March 2026) |
field-sizing | Newly available (June 2026) |
accent-color | Not Baseline: partial in Chrome on Android and in Safari before 26.2 |
text-wrap: pretty | Not Baseline: no Firefox support yet |
| Anchor positioning | Not 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:
- 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.
- Use
@supportsto 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.
- 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%).