What You Need to Know about Modern CSS (Spring 2024 Edition)

https://frontendmasters.com/blog/what-you-need-to-know-about-modern-css-spring-2024-edition/

My goal with this bookmarkable guide is to provide a list of (frankly: incredible) new additions to CSS lately. There is no hardline criteria for this list other than that these things are all fairly new and my sense is that many people aren’t aware of these things. Or even if they are, they don’t have a great understanding of them and could use a plain language explanation of what it is, why they should care, and a bit of reference code. Maybe that’s you.

I’d like to work on our collective muscle memory on these features. Like I said, “even if you know about this stuff, it takes time to build the muscle memory around it.”

There is quite a bit more syntax, detail, and nuance to these things than I am presenting here. I want you to know what’s possible, reference the most basic usage and syntax, then dig deeper when you need to.

Container Queries (Size)

What are Size Container Queries?

Container Queries allow you to write styles that apply to the children of a container element when that container matches certain media conditions, typically a width measurement.

<div class="element-wrap">
  <div class="element">
  </div>
</div>Code language: HTML, XML (xml)
.element-wrap {
  container: element / inline-size;
}
@container element (min-inline-size: 300px) {
  .element {
    display: flex;
    gap: 1rem;
  }
}Code language: CSS (css)

When should you care?

If you’ve ever thought: I wish I could make styling decisions based on the size of this element, not the entire page like @media queries force me to do. Then using @container queries are for you! People who work on design systems or heavily component-based websites will probably mostly use Container Queries to style things based on size, because the size of the entire page is a poor proxy for the size of a component.

Support

Browser SupportFull
Progressive Enhancement?Potentially — if it’s not critical what you are styling, then yes.
PolyfillableYes

Basic Demo of Usage

Use the resizer in the middle to see the calendar change layout depending on how much space it has. It has three breakpoints of its own.

Container Queries (Style)

What are Style Container Queries?

Container Style Queries allow you to apply styles when a given Custom Property has a given value.

.container {
  --variant: 1;
  &.variant2 {
    --variant: 2;
  }
}
@container style(--variant: 1) {
  button { } /* You can't style .container, but can select inside it */
  .other-things { }
}
@container style(--variant: 2) {
  button { }
  .whatever { }
}Code language: CSS (css)

When should you care?

Have you ever wanted a mixin in CSS? As in, you set one property but get multiple properties. Sass made mixins fairly popular. You can do that with a Style Container Query. But just like how Sass had variables then CSS variables turned out to be more powerful and useful, Style Container Queries are likely to be more powerful and useful, because they respect the cascade and can be calculated and such.

Support

Browser Support✅ Chrome ‘n’ Friends
🔜 Safari
❌ Firefox
Progressive Enhancement?Potentially — It depends on what you’re doing with the styles, but let’s say not really.
PolyfillableNo

Basic Demo of Usage

Container Units

What are Container Units?

Container Units (literally units, like px, rem, or vw) allow you to set the size of things based on the current size of a container element. Similar to how with viewport units 1vw is 1% of the browser window width, 1cqw is 1% of the width of the container (although I’d recommend you use cqi instead, the “logical equivalent”, meaning the “inline direction”).

The units are cqw (“container query width”), cqh (“container query height”), cqi (“container query inline”), cqb (“container query block”), cqmin (smaller of cqi and cqb), and cqmax (larger of cqi and cqb).

When should you care?

If the sizing of anything in an element feels as if it should be based on the current size of the container, container units are essentially the only way. An example of this is typography. A typical Card element may deserve a larger header text when it happens to be rendered larger, without something like a class name needing to be added to control that. (I’m a fan of this demo.) Even a container query is clunky comparatively.

Support

Browser SupportFull
Progressive Enhancement?Yes — you could list a declaration using fallback units right before the declaration using container query units.
PolyfillableYes

Basic Demo of Usage

This element uses container query units for not only the font-size, but the padding, border, and margin as well.

The :has() Pseudo Selector

What is the :has() selector?

The :has() selector allows you to conditionally select an element when elements deeper in the DOM tree of the original element match the selector you put inside :has().

figure:has(figcaption) {
  border: 1px solid black;
  padding: 0.5rem;
}Code language: CSS (css)

When should you care?

If you’ve ever wanted a “parent” selector in CSS, :has() can do that, but it’s more powerful than that, as once you’ve selected the parent you want, you can again drill back down. Jhey Tompkins once called it a “family selector” which a nice way to think about it. You can also combine it with :not() to build a selector when an element doesn’t “have” a matching element inside.

Support

Basic Demo of Usage

View Transitions

What are View Transitions?

There are two types of View Transitions:

  1. Same-Page Transitions (Require JavaScript)
  2. Multi-Page Transitions (CSS Only)

They are both useful. A same-page transition involves and animation when the DOM is changed without the page changing, like a list being sorted. A multi-page transition is for animating elements between page loads, like a video thumbnail transitioning into a video element. This is the basic syntax for a same-page transition:

if (!document.startViewTransition) {
  updateTheDOM();
} else {
  document.startViewTransition(() => updateTheDOM());
}Code language: JavaScript (javascript)

For multi-page transitions: you need this meta tag:

<meta name="view-transition" content="same-origin">Code language: HTML, XML (xml)

Then any element you want to transition between pages you make sure has a totally unique view-transition-name applied in the styles, on both the outgoing page and incoming page.

When should you care?

Users can understand an interface better if an element moves to a new position rather than instantly being there. There is an animation concept called tweening where the animation is automatically created based on a starting and ending state. View Transitions are essentially tweening. You can control aspects of the animation, but for the most part the animation is automatically created based on the starting and ending state of the DOM, rather than you having to be really specific about the animation details.

Support

Browser Support✅ Chrome ‘n’ Friends
❌ Safari
❌ Firefox
Progressive Enhancement?Yes — the transitions can just not run, or you could provide a fallback animation.
PolyfillableNo

Basic Demo of Usage

This is an example of a same-page view transition:

Nesting

What is nesting?

Nesting is a way of writing CSS that allow you to write additional selectors within an existing ruleset.

.card {
  padding: 1rem;

  > h2:first-child {
    margin-block-start: 0;
  }

  footer {
    border-block-start: 1px solid black;
  }
}
Code language: CSS (css)

When should you care?

Nesting is mostly a CSS authoring convenience, but the fact that it can group related CSS nicely together and prevent you from having to repeat writing a selector can mean avoiding mistakes and making the CSS easier to read. Nested CSS can also be something of a footgun in that may encourage writing CSS that matches the nesting of HTML in an unnecessary way, increasing the specificity and decreasing the reusability of some CSS.

.card {
  container: card / inline-size;
  
  display: grid;
  gap: 1rem;
  
  @container (min-inline-size: 250px) {
    gap: 2rem;
  }
}
Code language: CSS (css)

The only major difference from Sass-style nesting is that you can’t combine the & directly.

.card {
  body.home & { /* totally fine */ }
  & .footer { /* totally fine, don't even need the & */
  &__big { /* nope, can't do this */ }
}Code language: CSS (css)

Support

Browser SupportFull
Progressive Enhancement?No
PolyfillableYou could use a processor like LightningCSS, Sass, Less, etc.

Scroll-Driven Animations

What are Scroll-Driven Animations?

Any animation that is tied to the scrolling of an element (often the page itself) can now be done in CSS rather than needing to bind DOM scrolling events in JavaScript. They come in two varieties:

  • The scroll progress of the element. (animation-timeline: scroll())
  • An element’s current viewable position within the element. (animation-timeline: view())

When should you care?

Imagine a reading progress indicator bar that fills from 0% to 100% as the user scrolls down the page. That can be done with an animation moving the background-position of an element tried to the overall scroll position of the page. Doing this in CSS instead of JavaScript is good for performance.

The other major use case covered by scroll-driven animations is to run an animation as an element enters (or leaves!) the viewport. You have lots of control over the details, like when the animation starts and ends based on how visible the element is.

Support

Browser Support✅ Chrome ‘n’ Friends
❌ Safari
🔜 Firefox
Progressive Enhancement?Yes — these effects tend to be visual flair, not required functionality.
PolyfillableYes

Basic Example of Usage

This is the demo from when we looked at image zooming and page scrolling.

Anchor Positioning

What is Anchor Positioning?

Anchor positioning allows you to place items relative to where another element is. Seems pretty obvious when put like that, but that’s what it is. You declare an element an anchor and give it a name, then can position elements to the top/right/bottom/left (or center, or the logical equivalents) of the anchor.

When should you care?

Once you can use this freely, you’ll have to care less about exact DOM positioning of elements (aside from accessibility concerns). The way it is now, the element you want to position relative to another has to be a child element and for there to be a positioning context to work within. This can dictate where elements go in the DOM, whether or not that makes sense.

The big use cases are going to be tooltips and custom context menus.

Support

Browser Support🔜 Chrome ‘n’ Friends
❌ Safari
❌ Firefox
Progressive Enhancement?Possibly — if you can tolerate a totally different position for elements.
PolyfillableYes

Basic Example of Usage

At the time I’m publishing this, this only works in Chrome Canary with the “Experimental Web Platform Features” flag enabled.

Scoping

What is Scoped CSS?

Scoping in CSS is in the form of an @scope at-rule that declares a block of CSS to only apply to the given selector. And optionally, stop applying at another given selector.

When should you care?

You can also scope CSS by applying a class and nesting within that class. But @scope has a few tricks up it’s sleeve that can make it interesting. The “donut scope” option is a unique ability it has:

@scope (.card) to (.markdown-output) {
  h2 {
    background: tan; /* stop doing this when we get to the Markdown */
  }
}Code language: CSS (css)

More logical proximity styling is another useful feature. This is a bit tricky to explain but once you see it you can’t unsee it. Consider theming. You have a .dark selector and a .light selector. If you only ever use one on the entire page, that’s fine, but if you end up nesting them at all, because they have the same specificity, whichever one you define later is technically a bit more powerful, and can win out even if it doesn’t make sense to. Minimal example:

.purple-paragraphs p { color: purple; }
.red-paragraphs p { color: red; }Code language: CSS (css)
<div class="purple-paragraphs">
  <div class="red-paragraphs">
    <div class="purple-paragraphs">
       <p>some text</p>
    </div>
  </div>
</div>Code language: HTML, XML (xml)

You might think the paragraph element in there would have the color purple, but it will actually be red. That’s just awkward, but it can be fixed with @scope. When scoped selectors match, as Bramus says, “it weighs both selectors by proximity to their scoping root”, and since “light” is closer here, it would win.

My favorite though is the ability to drop in a <style> tag in the DOM and have it apply scoped styles only to that bit of the DOM, without having to name anything.

<div class="my-cool-component">
  <style>
    @scope {
      :scope { /* selects the div above, without having to select it by class or anything */
      }
      .card {
      }
    }
  </style>
  <article class="card">
  </article>
</div>Code language: HTML, XML (xml)

Support

Browser Support✅ Chrome
✅ Safari
❌ Firefox
Progressive Enhancement?No
PolyfillableNo

Basic Example of Usage

Cascade Layers

What are Layers?

Cascade Layers in CSS are an extremely powerful syntax that affects the styling strength of a chunk of styles. You can optionally name and order layers (if you don’t explicitly order them, they order in source order). Styles in higher layers automatically beat styles in lower layers, regardless of selector strength. Styles not within layers are the most powerful.

<body id="home">Code language: HTML, XML (xml)
@layer base {
  body#home {
    margin: 0;
    background: #eee;
  }
}

body {
  background: white;
}
Code language: CSS (css)

We’re used to thinking that body#home is a much more powerful selector, thus the background will be #eee. But because there are unlayered styles here, that will win, making the background white.

You may have as many layers as you like and can order them upfront. I think layering is likely to become a best practice on new greenfield projects, and take shape something like:

@layer reset, default, themes, patterns, layouts, components, utilities;Code language: CSS (css)

One gotcha is that !important rules on lower layers are actually more powerful.

When should you care?

One clear way you get a lot of value out of CSS layers if you work on a project that uses a third-party styling library. You can put that library on a lower layer than the styles that your team writes, and you won’t have to worry about fighting the third-party library in terms of selector strength. Your styles on a higher layer will always win, which is likely to create cleaner and more maintainable CSS.

For example, put all of Bootstrap on a lower layer just using the layer keyword and then any styles you write after that will win, even if Bootstrap itself uses a higher power selector.

@import url("https://cdn.com/bootstrap.css") layer;
h5 {
  margin-bottom: 2rem;
}Code language: CSS (css)

Support

Basic Example of Usage

Logical Properties

What are Logical Properties?

Logical properties are alternatives to properties that specify a direction. For example, in a left-to-right language like English, the inline direction is horizontal and the block direction is vertical, so margin-right is equivalent to margin-inline-end and margin-top is equivelant to margin-block-start. In a right-to-left language like Arabic, margin-inline-end changes to the equivalent of margin-left, because that is the end side of the inline flow of elements. There are a lot of CSS properties and values that have a directional component like this (border, padding, offset, set), so the trick is understanding inline and block flow and using the correct start or end value.

When should you care?

Often when you are declaring directional information in CSS, what you mean is “in the inline direction of text”. That might sound strange, but imagine a button and the space between an icon and the text. If you apply margin-right to the icon to space it away from the text, but then the page is translated to a right-to-left language, that spacing is now on the wrong side of the icon. What you meant was margin-inline-end on the icon. If you code your side using logical properties in this way, it will automatically translate better without writing any additional conditional code.

Support

Basic Example of Usage

P3 Colors

What is the Display P3 Color Space?

We’re largely used to the sRGB color space on the web. That’s what hex colors use, and the rgb(), hsl(), and hsb() functions. Many displays these days are capable of display a much wider range of colors than sRGB is capable of describing, so being limited to that color space sucks. The Display P3 color space is about 50% wider than sRGB, expanding in the direction of more rich and vibrant colors. New CSS functions, which can even use different color models that have their own useful properties, allow us to declare colors in this space.

When should you care?

If you want to use colors that are quite vibrant, you’ll need to tap into colors in the P3 Color Space. Using newer color models (and functions) can do this, and are very useful for a variety of other things.

For example, the oklch() function (and thus OKLCH color model) can display any color any other method can (plus P3), has a similar human readability in common with hsl(), and has “uniform perceived brightness”, so that the first number (lightness) behaves way more predictably than it does in hsl(). That’s awfully nice for color on the web. But it’s not the only new color model and function! I find the oklab color model generally best for gradients.

Support

Browser SupportFull (e.g. oklab)
Progressive Enhancement?Yes — you can declare fallback colors and displays that can’t display the color you declare will come back down into range.
PolyfillableYes

Basic Example of Usage

You can edit these <style> blocks because I made them display: block; and contenteditable:

Color Mixing

What is color-mix()?

The color-mix() function in CSS allows you to, wait for it, mix colors. This kind of thing has been baked into CSS processing tools for a long time, and as typical of CSS evolution, now that it’s in native CSS, it’s more thoughtful and powerful than it ever was in a processor.

When you should care?

Have you ever wanted to darken or lighten a color you already have on the fly? That’s one of the things color-mix() can do. Color mixing can happen in a specific color model which means you can take advantage of how that models works. For example, the perceptually uniform brightness of OKLCH makes it sensible to use for adjusting brightness. You can make whole color palettes using color-mix().

Browser Support

Browser SupportFull
Progressive Enhancement?Yes, you could declare fallback colors.
PolyfillableCould be but I don’t know of one.

Basic Example of Usage

Margin Trim

What is margin-trim?

The margin-trim property removes any margin in the direction specified from the selected container at the end of that direction. Imagine you have five blocks in a row that all have right margin on them in a container. You might select the :last-child to remove the right margin. With margin-trim you can ensure that margin is removed from the parent element itself.

.container {
  /* prevent "extra" margin at the end of the element */
  margin-trim: block-end;
  /* an element like this might be the culprit, but it could be anything */
  > p {
    margin-block-end: 1rem;
  }
}Code language: CSS (css)

When should you care?

You know how the gap property of flexbox and grid is… awesome? It only puts spacing between elements. Well, if you need to apply spacing between elements but you’re in a position where you can’t use gap, margin-trim is awfully nice as it means you apply directional margin to all the children and not worry about an additional fancy selector to select the first or last one and remove that unneeded final margin. It might end up a best practice.

Support

Browser Support✅ Safari
❌ Chrome
❌ Firefox
Progressive Enhancement?Yes. A little extra space likely isn’t a major problem.
PolyfillableNo

Basic Example of Usage

The last paragraph here is a notorious situation where the bottom margin on it creates more space at the bottom than any of the other edges. With margin-trim we can ensure it’s sliced off without having to select that last paragraph and manually remove it.

Text Wrapping

What is text-wrap?

The text-wrap property likely isn’t in your long term CSS memory. It’s capable of text-wrap: nowrap;, but we generally think of white-space: nowrap; for this. But now, text-wrap has two new tricks up it’s sleeve:

  • text-wrap: balance; — Attempt to make equal-width lines when text wraps.
  • text-wrap: pretty; — Avoid orphans.

When should you care?

A headline with one word orphaned onto the next line just looks really awkward and could be considered poor typography. There wasn’t a great way to solve this before, short of somewhat awkward tricks like inserting a &nbsp; instead of a normal space between the last two words. Balancing a headline prevents this, but goes further in making the multiple lines of text generally the same width. Using pretty is more focused just on orphan prevention alone, making it more appropriate for body text.

Support

Browser SupportDepends on which value. balance has decent support with all browsers ready or coming soon. pretty, less-so.
Progressive Enhancement?Yes. While slightly less aesthetic, widows and orphans are not that big of a problem, so if this property doesn’t work, it’s no big deal.
PolyfillableYes.

Basic Example of Usage

Subgrid

What is Subgrid?

Subgrid is an optional part of using CSS grid that is relevant when you are nesting gridded elements. By setting grid-template-columns: subgrid; or grid-template-rows: subgrid; on a grid-level element, you’re saying “inherit these columns or rows from my parent grid, where relevant”.

When should you care?

The point of using grids for layout is generally lining things up. Without subgrid, it means that child elements of a grid don’t have access to the grid lines of the parent grid, and thus lack the opportunity help line things up. Subgrid fills that gap. When DOM nesting is important for functionality or accessibility, like in a <form>, subgrid can help ensure things line up sensibly.

Support

Browser SupportFull
Progressive Enhancement?Yes. You can fall back to defining your own grid lines that are workable if not perfect.
PolyfillableNo. There is a grid polyfill but it doesn’t do subgrid.

Basic Example of Usage

Things to keep an eye on…

The speed of CSS development doesn’t seem to have slowed down. There is plenty to continue to watch for and look forward to.

  • CSS Mixins & Functionsactual mixins and functions that take parameters
  • Relative Color Syntax — a way to manipulate the parts of colors in an intuitive and powerful way.
  • Interop 2024 — All the things that we can essentially bet on for being cross-browser compatible soon, including the relative color syntax above.
  • The CSS property field-sizing should help the long-standing difficult issue of auto-resizing form elements like textareas and input to the content they contain.
  • <selectmenu> in HTML is essentially a fully CSS styleable <select>, which is wild.

That’s just a few things to watch. You might as well subscribe to our feed as we’ll be doing the watching for you and then you’ll catch the next one.

Did I miss a relatively new favorite of yours? Let me know.

{
"by": "fagnerbrack",
"descendants": 21,
"id": 40248530,
"kids": [
40252861,
40250337,
40252964,
40250197,
40252647,
40250131,
40252351,
40262948,
40253145,
40253422,
40253710
],
"score": 131,
"time": 1714748826,
"title": "What You Need to Know about Modern CSS (Spring 2024 Edition)",
"type": "story",
"url": "https://frontendmasters.com/blog/what-you-need-to-know-about-modern-css-spring-2024-edition/"
}
{
"author": "Chris Coyier",
"date": "2024-03-26T12:00:00.000Z",
"description": "My goal with this bookmarkable guide is to provide a list of (frankly: incredible) new additions to CSS lately. There is no hardline criteria for this list other than that these things are all fairly new and my sense is that many people aren’t aware of these things. Or even if they are, they don’t […]",
"image": "https://frontendmasters.com/blog/wp-json/social-image-generator/v1/image/1013",
"logo": "https://logo.clearbit.com/frontendmasters.com",
"publisher": "Frontend Masters",
"title": "What You Need to Know about Modern CSS (2024 Edition) – Frontend Masters Boost",
"url": "https://frontendmasters.com/blog/what-you-need-to-know-about-modern-css-spring-2024-edition/"
}
{
"url": "https://frontendmasters.com/blog/what-you-need-to-know-about-modern-css-spring-2024-edition/",
"title": "What You Need to Know about Modern CSS (2024 Edition) – Frontend Masters Boost",
"description": "My goal with this bookmarkable guide is to provide a list of (frankly: incredible) new additions to CSS lately. There is no hardline criteria for this list other than that these things are all fairly new and my sense is that many people aren’t aware of these things. Or even if they are, they don’t […]",
"links": [
"https://frontendmasters.com/blog/?p=1013",
"https://frontendmasters.com/blog/what-you-need-to-know-about-modern-css-spring-2024-edition/"
],
"image": "https://frontendmasters.com/blog/wp-json/social-image-generator/v1/image/1013",
"content": "<div>\n<p>My goal with this bookmarkable guide is to provide a list of (frankly: incredible) new additions to CSS lately. There is no hardline criteria for this list other than that these things are all <em>fairly</em> new and my sense is that many people aren’t aware of these things. Or even if they are, they don’t have a great understanding of them and could use a <strong>plain language</strong> explanation of what it is, <em>why they should care</em>, and a bit of reference code. Maybe that’s you.</p>\n<p>I’d like to work on our collective muscle memory on these features. <a target=\"_blank\" href=\"https://twitter.com/chriscoyier/status/1757826338722058444\">Like I said</a>, “even if you <em>know</em> about this stuff, it takes time to build the muscle memory around it.”</p>\n<p>There is quite a bit more syntax, detail, and nuance to these things than I am presenting here. I want you to know what’s possible, reference the most basic usage and syntax, then dig deeper when you need to. </p>\n<div>\n<h2>Container Queries <span>(Size)</span></h2>\n<h3>What are Size Container Queries?</h3>\n<p>Container Queries allow you to write styles that apply to the children of a container element when that container matches certain media conditions, typically a width measurement.</p>\n<div>\n<div><pre><span><code><span>&lt;<span>div</span> <span>class</span>=<span>\"element-wrap\"</span>&gt;</span>\n <span>&lt;<span>div</span> <span>class</span>=<span>\"element\"</span>&gt;</span>\n <span>&lt;/<span>div</span>&gt;</span>\n<span>&lt;/<span>div</span>&gt;</span></code></span><small><span>Code language:</span> <span>HTML, XML</span> <span>(</span><span>xml</span><span>)</span></small></pre></div>\n<div><pre><span><code><span>.element-wrap</span> {\n <span>container</span>: element / inline-size;\n}\n<span>@container</span> element (<span>min-inline-size:</span> <span>300px</span>) {\n <span>.element</span> {\n <span>display</span>: flex;\n <span>gap</span>: <span>1rem</span>;\n }\n}</code></span><small><span>Code language:</span> <span>CSS</span> <span>(</span><span>css</span><span>)</span></small></pre></div>\n</div>\n<h3>When should you care?</h3>\n<p>If you’ve ever thought: I wish I could make styling decisions based on the size of <em>this</em> element, not the entire page like <code>@media</code> queries force me to do. Then using <code>@container</code> queries are for you! People who work on design systems or heavily component-based websites will probably <em>mostly</em> use Container Queries to style things based on size, because the size of the entire page is a poor proxy for the size of a component. </p>\n<h3>Support</h3>\n<figure><table><tbody><tr><td><a target=\"_blank\" href=\"https://caniuse.com/css-container-queries\">Browser Support</a></td><td>Full</td></tr><tr><td>Progressive Enhancement?</td><td>Potentially — if it’s not critical what you are styling, then yes.</td></tr><tr><td>Polyfillable</td><td><a target=\"_blank\" href=\"https://github.com/GoogleChromeLabs/container-query-polyfill\">Yes</a></td></tr></tbody></table></figure>\n<h3>Basic Demo of Usage</h3>\n<p>Use the resizer in the middle to see the calendar change layout depending on how much space it has. It has three breakpoints of its own.</p>\n</div>\n<div>\n<h2>Container Queries <span>(Style)</span></h2>\n<h3>What are Style Container Queries?</h3>\n<p>Container Style Queries allow you to apply styles when a given Custom Property has a given value. </p>\n<pre><span><code><span>.container</span> {\n <span>--variant</span>: <span>1</span>;\n &amp;.variant2 {\n <span>--variant</span>: <span>2</span>;\n }\n}\n<span>@container</span> style(<span>--variant:</span> <span>1</span>) {\n <span>button</span> { } <span>/* You can't style .container, but can select inside it */</span>\n <span>.other-things</span> { }\n}\n<span>@container</span> style(<span>--variant:</span> <span>2</span>) {\n <span>button</span> { }\n <span>.whatever</span> { }\n}</code></span><small><span>Code language:</span> <span>CSS</span> <span>(</span><span>css</span><span>)</span></small></pre>\n<h3>When should you care?</h3>\n<p>Have you ever wanted a mixin in CSS? As in, you set one property but get multiple properties. <a target=\"_blank\" href=\"https://sass-lang.com/documentation/at-rules/mixin/\">Sass made mixins</a> fairly popular. You can do that with a Style Container Query. But just like how Sass had variables then CSS variables turned out to be more powerful and useful, Style Container Queries are likely to be more powerful and useful, because they respect the cascade and can be calculated and such.</p>\n<h3>Support</h3>\n<figure><table><tbody><tr><td><a target=\"_blank\" href=\"https://caniuse.com/css-container-queries-style\">Browser Support</a></td><td>✅ Chrome ‘n’ Friends<br />🔜 Safari<br />❌ Firefox</td></tr><tr><td>Progressive Enhancement?</td><td>Potentially — It depends on what you’re doing with the styles, but let’s say <em>not really.</em></td></tr><tr><td>Polyfillable</td><td>No</td></tr></tbody></table></figure>\n<h3>Basic Demo of Usage</h3>\n</div>\n<div>\n<h2>Container Units</h2>\n<h3>What are Container Units?</h3>\n<p>Container Units (literally units, like <code>px</code>, <code>rem</code>, or <code>vw</code>) allow you to set the size of things based on the current size of a container element. Similar to how with viewport units <code>1vw</code> is 1% of the browser window width, <code>1cqw</code> is 1% of the width of the container (although I’d recommend you use <code>cqi</code> instead, the “logical equivalent”, meaning the “inline direction”).</p>\n<p>The units are <code>cqw</code> (“container query width”), <code>cqh</code> (“container query height”), <code>cqi</code> (“container query inline”), <code>cqb</code> (“container query block”), <code>cqmin</code> (smaller of <code>cqi</code> and <code>cqb</code>), and <code>cqmax</code> (larger of <code>cqi</code> and <code>cqb</code>).</p>\n<h3>When should you care?</h3>\n<p>If the sizing of anything in an element feels as if it should be based on the current size of the container, container units are essentially the only way. An example of this is typography. A typical Card element may deserve a larger header text when it happens to be rendered larger, without something like a class name needing to be added to control that. (I’m a fan of <a target=\"_blank\" href=\"https://codepen.io/scottkellum/details/jOwmOZE\">this demo</a>.) Even a container query is clunky comparatively.</p>\n<h3>Support</h3>\n<figure><table><tbody><tr><td><a target=\"_blank\" href=\"https://caniuse.com/css-container-query-units\">Browser Support</a></td><td>Full</td></tr><tr><td>Progressive Enhancement?</td><td>Yes — you could list a declaration using fallback units right before the declaration using container query units.</td></tr><tr><td>Polyfillable</td><td><a target=\"_blank\" href=\"https://github.com/GoogleChromeLabs/container-query-polyfill\">Yes</a></td></tr></tbody></table></figure>\n<h3>Basic Demo of Usage</h3>\n<p>This element uses container query units for not only the <code>font-size</code>, but the <code>padding</code>, <code>border</code>, and <code>margin</code> as well.</p>\n</div>\n<div>\n<h2>The <code>:has()</code> Pseudo Selector</h2>\n<h3>What is the <code>:has()</code> selector?</h3>\n<p>The <code>:has()</code> selector allows you to conditionally select an element when elements deeper in the DOM tree of the original element match the selector you put inside <code>:has()</code>. </p>\n<pre><span><code><span>figure</span><span>:has(figcaption)</span> {\n <span>border</span>: <span>1px</span> solid black;\n <span>padding</span>: <span>0.5rem</span>;\n}</code></span><small><span>Code language:</span> <span>CSS</span> <span>(</span><span>css</span><span>)</span></small></pre>\n<h3>When should you care?</h3>\n<p>If you’ve ever wanted a “parent” selector in CSS, <code>:has()</code> can do that, but it’s more powerful than that, as once you’ve selected the parent you want, you can again drill back down. Jhey Tompkins once called it <a target=\"_blank\" href=\"https://developer.chrome.com/blog/has-m105/\">a “family selector”</a> which a nice way to think about it. You can also combine it with <code>:not()</code> to build a selector when an element <em>doesn’t </em>“have” a matching element inside.</p>\n<h3>Support</h3>\n<figure></figure>\n<h3>Basic Demo of Usage</h3>\n</div>\n<div>\n<h2>View Transitions</h2>\n<h3>What are View Transitions?</h3>\n<p>There are two types of View Transitions:</p>\n<ol>\n<li>Same-Page Transitions (Require JavaScript)</li>\n<li>Multi-Page Transitions (CSS Only)</li>\n</ol>\n<p>They are both useful. A same-page transition involves and animation when the DOM is changed without the page changing, like a list being sorted. A multi-page transition is for animating elements between page loads, like a video thumbnail transitioning into a video element. This is the basic syntax for a same-page transition:</p>\n<pre><span><code><span>if</span> (!<span>document</span>.startViewTransition) {\n updateTheDOM();\n} <span>else</span> {\n <span>document</span>.startViewTransition(<span><span>()</span> =&gt;</span> updateTheDOM());\n}</code></span><small><span>Code language:</span> <span>JavaScript</span> <span>(</span><span>javascript</span><span>)</span></small></pre>\n<p>For multi-page transitions: you need this meta tag:</p>\n<pre><span><code><span>&lt;<span>meta</span> <span>name</span>=<span>\"view-transition\"</span> <span>content</span>=<span>\"same-origin\"</span>&gt;</span></code></span><small><span>Code language:</span> <span>HTML, XML</span> <span>(</span><span>xml</span><span>)</span></small></pre>\n<p>Then any element you want to transition between pages you make sure has a totally unique <code>view-transition-name</code> applied in the styles, on both the outgoing page and incoming page. </p>\n<h3>When should you care?</h3>\n<p>Users can understand an interface better if an element moves to a new position rather than instantly being there. There is an animation concept called <em>tweening</em> where the animation is automatically created based on a starting and ending state. View Transitions are essentially tweening. You can control aspects of the animation, but for the most part the animation is automatically created based on the starting and ending state of the DOM, rather than you having to be really specific about the animation details.</p>\n<h3>Support</h3>\n<figure><table><tbody><tr><td><a target=\"_blank\" href=\"https://caniuse.com/view-transitions\">Browser Support</a></td><td>✅ Chrome ‘n’ Friends<br />❌ Safari<br />❌ Firefox</td></tr><tr><td>Progressive Enhancement?</td><td>Yes — the transitions can just not run, or you could provide a fallback animation. </td></tr><tr><td>Polyfillable</td><td>No</td></tr></tbody></table></figure>\n<h3>Basic Demo of Usage</h3>\n<p>This is an example of a same-page view transition:</p>\n</div>\n<div>\n<h2>Nesting</h2>\n<h3>What is nesting?</h3>\n<p>Nesting is a way of writing CSS that allow you to write additional selectors within an existing <strong>ruleset</strong>. </p>\n<pre><span><code><span><span><span>.card</span> {\n</span></span><span><span> <span>padding</span>: <span>1rem</span>;\n</span></span><span><span>\n</span></span><span> &gt; h2:first-child {\n</span><span><span> <span>margin-block-start</span>: <span>0</span>;\n</span></span><span><span> }\n</span></span><span><span>\n</span></span><span> <span>footer</span> {\n</span><span><span> <span>border-block-start</span>: <span>1px</span> solid black;\n</span></span><span><span> }\n</span></span><span><span>}\n</span></span></code></span><small><span>Code language:</span> <span>CSS</span> <span>(</span><span>css</span><span>)</span></small></pre>\n<h3>When should you care?</h3>\n<p>Nesting is mostly a CSS authoring convenience, but the fact that it can group related CSS nicely together and prevent you from having to repeat writing a selector can mean avoiding mistakes and making the CSS easier to read. Nested CSS can also be something of a footgun in that may encourage writing CSS that matches the nesting of HTML in an unnecessary way, increasing the specificity and decreasing the reusability of some CSS. </p>\n<pre><span><code><span><span><span>.card</span> {\n</span></span><span><span> <span>container</span>: card / inline-size;\n</span></span><span><span> \n</span></span><span><span> <span>display</span>: grid;\n</span></span><span><span> <span>gap</span>: <span>1rem</span>;\n</span></span><span><span> \n</span></span><span> @container (<span>min-inline-size</span>: <span>250px</span>) {\n</span><span><span> gap: <span>2rem</span>;\n</span></span><span><span> }\n</span></span><span><span>}\n</span></span></code></span><small><span>Code language:</span> <span>CSS</span> <span>(</span><span>css</span><span>)</span></small></pre>\n<p>The only major difference from Sass-style nesting is that you can’t combine the <code>&amp;</code> directly. </p>\n<pre><span><code><span>.card</span> {\n body.home &amp; { <span>/* totally fine */</span> }\n &amp; <span>.footer</span> { <span>/* totally fine, don't even need the &amp; */</span>\n &amp;__big { <span>/* nope, can't do this */</span> }\n}</code></span><small><span>Code language:</span> <span>CSS</span> <span>(</span><span>css</span><span>)</span></small></pre>\n<h3>Support</h3>\n<figure><table><tbody><tr><td><a target=\"_blank\" href=\"https://caniuse.com/css-nesting\">Browser Support</a></td><td>Full</td></tr><tr><td>Progressive Enhancement?</td><td>No</td></tr><tr><td>Polyfillable</td><td>You could use a processor like LightningCSS, Sass, Less, etc.</td></tr></tbody></table></figure>\n</div>\n<div>\n<h2>Scroll-Driven Animations</h2>\n<h3>What are Scroll-Driven Animations?</h3>\n<p>Any animation that is tied to the scrolling of an element (often the page itself) can now be done in CSS rather than needing to bind DOM scrolling events in JavaScript. They come in two varieties:</p>\n<ul>\n<li>The scroll progress of the element. (<code>animation-timeline: scroll()</code>)</li>\n<li>An element’s current viewable position within the element. (<code>animation-timeline: view()</code>)</li>\n</ul>\n<h3>When should you care?</h3>\n<p>Imagine a reading progress indicator bar that fills from 0% to 100% as the user scrolls down the page. That can be done with an animation moving the <code>background-position</code> of an element tried to the overall scroll position of the page. Doing this in CSS instead of JavaScript is <a target=\"_blank\" href=\"https://developer.chrome.com/blog/scroll-animation-performance-case-study/\">good for performance</a>.</p>\n<p>The other major use case covered by scroll-driven animations is to run an animation as an element enters (or leaves!) the viewport. You have lots of control over the details, like when the animation starts and ends based on how visible the element is.</p>\n<h3>Support</h3>\n<figure><table><tbody><tr><td><a target=\"_blank\" href=\"https://caniuse.com/mdn-css_properties_animation-timeline_scroll\">Browser Support</a></td><td>✅ Chrome ‘n’ Friends<br />❌ Safari<br />🔜 Firefox</td></tr><tr><td>Progressive Enhancement?</td><td>Yes — these effects tend to be visual flair, not required functionality.</td></tr><tr><td>Polyfillable</td><td><a target=\"_blank\" href=\"https://github.com/flackr/scroll-timeline\">Yes</a></td></tr></tbody></table></figure>\n<h3>Basic Example of Usage</h3>\n<p>This is the demo from when we looked at <a target=\"_blank\" href=\"https://frontendmasters.com/blog/background-size-zooming-with-scroll-driven-animations/\">image zooming and page scrolling</a>.</p>\n</div>\n<div>\n<h2>Anchor Positioning</h2>\n<h3>What is Anchor Positioning?</h3>\n<p>Anchor positioning allows you to place items relative to where another element is. Seems pretty obvious when put like that, but that’s what it is. You declare an element an anchor and give it a name, then can position elements to the top/right/bottom/left (or center, or the logical equivalents) of the anchor.</p>\n<h3>When should you care?</h3>\n<p>Once you can use this freely, you’ll have to care less about exact DOM positioning of elements (aside from accessibility concerns). The way it is now, the element you want to position relative to another has to be a <em>child</em> element and for there to be a positioning context to work within. This can dictate where elements go in the DOM, whether or not that makes sense. </p>\n<p>The big use cases are going to be tooltips and custom context menus. </p>\n<h3>Support</h3>\n<figure><table><tbody><tr><td><a target=\"_blank\" href=\"https://caniuse.com/css-anchor-positioning\">Browser Support</a></td><td>🔜 Chrome ‘n’ Friends<br />❌ Safari<br />❌ Firefox</td></tr><tr><td>Progressive Enhancement?</td><td>Possibly — if you can tolerate a totally different position for elements.</td></tr><tr><td>Polyfillable</td><td><a target=\"_blank\" href=\"https://github.com/oddbird/css-anchor-positioning\">Yes</a></td></tr></tbody></table></figure>\n<h3>Basic Example of Usage</h3>\n<p>At the time I’m publishing this, this only works in Chrome Canary with the “Experimental Web Platform Features” flag enabled.</p>\n</div>\n<div>\n<h2>Scoping</h2>\n<h3>What is Scoped CSS?</h3>\n<p>Scoping in CSS is in the form of an <code>@scope</code> at-rule that declares a block of CSS to only apply to the given selector. And optionally, <em>stop</em> applying at another given selector. </p>\n<h3>When should you care?</h3>\n<p>You can also scope CSS by applying a class and nesting within that class. But <code>@scope</code> has a few tricks up it’s sleeve that can make it interesting. The “donut scope” option is a unique ability it has:</p>\n<pre><span><code><span>@scope</span> (.card) to (.markdown-output) {\n <span>h2</span> {\n <span>background</span>: tan; <span>/* stop doing this when we get to the Markdown */</span>\n }\n}</code></span><small><span>Code language:</span> <span>CSS</span> <span>(</span><span>css</span><span>)</span></small></pre>\n<p>More logical proximity styling is another useful feature. This is a bit tricky to explain but once you see it you can’t unsee it. Consider theming. You have a <code>.dark</code> selector and a <code>.light</code> selector. If you only ever use one on the entire page, that’s fine, but if you end up <em>nesting</em> them at all, because they have the same specificity, whichever one you define later is technically a bit more powerful, and can win out even if it doesn’t make sense to. Minimal example:</p>\n<pre><span><code><span>.purple-paragraphs</span> <span>p</span> { <span>color</span>: purple; }\n<span>.red-paragraphs</span> <span>p</span> { <span>color</span>: red; }</code></span><small><span>Code language:</span> <span>CSS</span> <span>(</span><span>css</span><span>)</span></small></pre>\n<pre><span><code><span>&lt;<span>div</span> <span>class</span>=<span>\"purple-paragraphs\"</span>&gt;</span>\n <span>&lt;<span>div</span> <span>class</span>=<span>\"red-paragraphs\"</span>&gt;</span>\n <span>&lt;<span>div</span> <span>class</span>=<span>\"purple-paragraphs\"</span>&gt;</span>\n <span>&lt;<span>p</span>&gt;</span>some text<span>&lt;/<span>p</span>&gt;</span>\n <span>&lt;/<span>div</span>&gt;</span>\n <span>&lt;/<span>div</span>&gt;</span>\n<span>&lt;/<span>div</span>&gt;</span></code></span><small><span>Code language:</span> <span>HTML, XML</span> <span>(</span><span>xml</span><span>)</span></small></pre>\n<p>You might think the paragraph element in there would have the color purple, but it <a target=\"_blank\" href=\"https://codepen.io/chriscoyier/pen/oNOpqJK\">will actually be red</a>. That’s just awkward, but it <a target=\"_blank\" href=\"https://codepen.io/web-dot-dev/pen/MWZqazx\">can be fixed with <code>@scope</code></a>. When scoped selectors match, <a target=\"_blank\" href=\"https://developer.chrome.com/docs/css-ui/at-scope\">as Bramus says</a>, “it weighs both selectors by proximity to their scoping root”, and since “light” is closer here, it would win.</p>\n<p>My favorite though is the ability to drop in a <code>&lt;style&gt;</code> tag in the DOM and have it apply scoped styles only to that bit of the DOM, without having to name anything.</p>\n<pre><span><code><span>&lt;<span>div</span> <span>class</span>=<span>\"my-cool-component\"</span>&gt;</span>\n <span>&lt;<span>style</span>&gt;</span><span>\n <span>@scope</span> {\n <span>:scope</span> { <span>/* selects the div above, without having to select it by class or anything */</span>\n }\n <span>.card</span> {\n }\n }\n </span><span>&lt;/<span>style</span>&gt;</span>\n <span>&lt;<span>article</span> <span>class</span>=<span>\"card\"</span>&gt;</span>\n <span>&lt;/<span>article</span>&gt;</span>\n<span>&lt;/<span>div</span>&gt;</span></code></span><small><span>Code language:</span> <span>HTML, XML</span> <span>(</span><span>xml</span><span>)</span></small></pre>\n<h3>Support</h3>\n<figure><table><tbody><tr><td><a target=\"_blank\" href=\"https://caniuse.com/css-cascade-scope\">Browser Support</a></td><td>✅ Chrome<br />✅ Safari<br />❌ Firefox</td></tr><tr><td>Progressive Enhancement?</td><td>No</td></tr><tr><td>Polyfillable</td><td>No</td></tr></tbody></table></figure>\n<h3>Basic Example of Usage</h3>\n</div>\n<div>\n<h2>Cascade Layers</h2>\n<h3>What are Layers?</h3>\n<p>Cascade Layers in CSS are an extremely powerful syntax that affects the styling strength of a chunk of styles. You can optionally name and order layers (if you don’t explicitly order them, they order in source order). Styles in higher layers automatically beat styles in lower layers, <strong>regardless of selector strength.</strong> Styles <em>not</em> within layers are the most powerful. </p>\n<pre><span><code><span>&lt;<span>body</span> <span>id</span>=<span>\"home\"</span>&gt;</span></code></span><small><span>Code language:</span> <span>HTML, XML</span> <span>(</span><span>xml</span><span>)</span></small></pre>\n<pre><span><code><span><span>@layer</span> base {\n</span><span><span> <span>body</span><span>#home</span> {\n</span></span><span><span> <span>margin</span>: <span>0</span>;\n</span></span><span><span> <span>background</span>: <span>#eee</span>;\n</span></span><span><span> }\n</span></span><span><span>}\n</span></span><span><span>\n</span></span><span><span><span>body</span> {\n</span></span><span><span> <span>background</span>: white;\n</span></span><span><span>}\n</span></span></code></span><small><span>Code language:</span> <span>CSS</span> <span>(</span><span>css</span><span>)</span></small></pre>\n<p>We’re used to thinking that <code>body#home</code> is a <em>much</em> more powerful selector, thus the background will be <code>#eee</code>. But because there are unlayered styles here, that will win, making the background <code>white</code>. </p>\n<p>You may have as many layers as you like and can order them upfront. I think layering is likely to become a best practice on new greenfield projects, and take shape something like:</p>\n<pre><span><code><span>@layer</span> reset, default, themes, patterns, layouts, components, utilities;</code></span><small><span>Code language:</span> <span>CSS</span> <span>(</span><span>css</span><span>)</span></small></pre>\n<p>One gotcha is that <code>!important</code> rules on <em>lower</em> layers are actually <em>more </em>powerful.</p>\n<h3>When should you care?</h3>\n<p>One clear way you get a lot of value out of CSS layers if you work on a project that uses a third-party styling library. You can put that library on a lower layer than the styles that your team writes, and you won’t have to worry about <strong>fighting</strong> the third-party library in terms of selector strength. Your styles on a higher layer will always win, which is likely to create cleaner and more maintainable CSS.</p>\n<p>For example, put all of Bootstrap on a lower layer just using the <code>layer</code> keyword and then any styles you write after that will win, even if Bootstrap itself uses a higher power selector.</p>\n<pre><span><code><span>@import</span> url(<span>\"https://cdn.com/bootstrap.css\"</span>) layer;\n<span>h5</span> {\n <span>margin-bottom</span>: <span>2rem</span>;\n}</code></span><small><span>Code language:</span> <span>CSS</span> <span>(</span><span>css</span><span>)</span></small></pre>\n<h3>Support</h3>\n<figure></figure>\n<h3>Basic Example of Usage</h3>\n</div>\n<div>\n<h2>Logical Properties</h2>\n<h3>What are Logical Properties?</h3>\n<p>Logical properties are alternatives to properties that specify a direction. For example, in a left-to-right language like English, the <code>inline</code> direction is horizontal and the <code>block</code> direction is vertical, so <code>margin-right</code> is equivalent to <code>margin-inline-end</code> and <code>margin-top</code> is equivelant to <code>margin-block-start</code>. In a right-to-left language like Arabic, <code>margin-inline-end</code> changes to the equivalent of <code>margin-left</code>, because that is the end side of the inline flow of elements. There are <em>a lot</em> of CSS properties and values that have a directional component like this (border, padding, offset, set), so the trick is understanding <code>inline</code> and <code>block</code> flow and using the correct <code>start</code> or <code>end</code> value.</p>\n<h3>When should you care?</h3>\n<p>Often when you are declaring directional information in CSS, what you <em>mean</em> is “in the inline direction of text”. That might sound strange, but imagine a button and the space between an icon and the text. If you apply <code>margin-right</code> to the icon to space it away from the text, but then the page is translated to a right-to-left language, that spacing is now <em>on the wrong side</em> of the icon. What you meant was <code>margin-inline-end</code> on the icon. If you code your side using logical properties in this way,<strong> it will automatically translate better without writing any additional conditional code. </strong></p>\n<h3>Support</h3>\n<figure></figure>\n<h3>Basic Example of Usage</h3>\n</div>\n<div>\n<h2>P3 Colors</h2>\n<h3>What is the Display P3 Color Space?</h3>\n<p>We’re largely used to the sRGB color space on the web. That’s what hex colors use, and the <code>rgb()</code>, <code>hsl()</code>, and <code>hsb()</code> functions. Many displays these days are capable of display a much <a target=\"_blank\" href=\"https://webkit.org/blog/10042/wide-gamut-color-in-css-with-display-p3/\">wider range of colors</a> than sRGB is capable of describing, so being limited to that color space sucks. The Display P3 color space is about 50% wider than sRGB, expanding in the direction of more rich and vibrant colors. New CSS functions, which can even use different color <em>models</em> that have their own useful properties, allow us to declare colors in this space.</p>\n<h3>When should you care?</h3>\n<p>If you want to use colors that are quite vibrant, you’ll need to tap into colors in the P3 Color Space. Using newer color models (and functions) can do this, and are very useful for a variety of other things.</p>\n<p>For example, the <code>oklch()</code> function (and thus <a target=\"_blank\" href=\"https://oklch.com/#61.88,0.286,342.4,100\">OKLCH color model</a>) can display any color any other method can (plus P3), has a similar human readability in common with <code>hsl()</code>, and has “uniform perceived brightness”, so that the first number (lightness) behaves way more predictably than it does in <code>hsl()</code>. That’s awfully nice for color on the web. But it’s not the only new color model and function! I find the <code>oklab</code> color model generally best for gradients.</p>\n<h3>Support</h3>\n<figure><table><tbody><tr><td>Browser Support</td><td>Full (e.g. <a target=\"_blank\" href=\"https://caniuse.com/mdn-css_types_color_oklab\">oklab</a>)</td></tr><tr><td>Progressive Enhancement?</td><td>Yes — you can declare fallback colors and displays that can’t display the color you declare will come back down into range.</td></tr><tr><td>Polyfillable</td><td><a target=\"_blank\" href=\"https://www.npmjs.com/package/@csstools/postcss-oklab-function?activeTab=readme\">Yes</a></td></tr></tbody></table></figure>\n<h3>Basic Example of Usage</h3>\n<p>You can edit these <code>&lt;style&gt;</code> blocks because I made them <code>display: block;</code> and <code>contenteditable</code>:</p>\n</div>\n<div>\n<h2>Color Mixing</h2>\n<h3>What is <code>color-mix()</code>?</h3>\n<p>The <code>color-mix()</code> function in CSS allows you to, <em>wait for it</em>, mix colors. This kind of thing has been baked into CSS processing tools for a long time, and as typical of CSS evolution, now that it’s in native CSS, it’s more thoughtful and powerful than it ever was in a processor. </p>\n<h3>When you should care?</h3>\n<p>Have you ever wanted to darken or lighten a color you already have on the fly? That’s one of the things <code>color-mix()</code> can do. Color mixing can happen <em>in a specific color model</em> which means you can take advantage of how that models works. For example, the perceptually uniform brightness of OKLCH makes it sensible to use for adjusting brightness. <a target=\"_blank\" href=\"https://developer.mozilla.org/en-US/blog/color-palettes-css-color-mix/\">You can make whole color palettes</a> using <code>color-mix()</code>. </p>\n<h3>Browser Support</h3>\n<figure><table><tbody><tr><td><a target=\"_blank\" href=\"https://caniuse.com/?search=color-mix\">Browser Support</a></td><td>Full</td></tr><tr><td>Progressive Enhancement?</td><td>Yes, you could declare fallback colors.</td></tr><tr><td>Polyfillable</td><td>Could be but I don’t know of one.</td></tr></tbody></table></figure>\n<h3>Basic Example of Usage</h3>\n</div>\n<div>\n<h2>Margin Trim</h2>\n<h3>What is <code>margin-trim</code>?</h3>\n<p>The <code>margin-trim</code> property removes any margin in the direction specified from the selected container <em>at the end of that direction.</em> Imagine you have five blocks in a row that all have right margin on them in a container. You might select the <code>:last-child</code> to remove the right margin. With <code>margin-trim</code> you can ensure that margin is removed from the parent element itself.</p>\n<pre><span><code><span>.container</span> {\n <span>/* prevent \"extra\" margin at the end of the element */</span>\n <span>margin-trim</span>: block-end;\n <span>/* an element like this might be the culprit, but it could be anything */</span>\n &gt; p {\n <span>margin-block-end</span>: <span>1rem</span>;\n }\n}</code></span><small><span>Code language:</span> <span>CSS</span> <span>(</span><span>css</span><span>)</span></small></pre>\n<h3>When should you care?</h3>\n<p>You know how the <code>gap</code> property of flexbox and grid is… awesome? It only puts spacing <em>between</em> elements. Well, if you need to apply spacing between elements but you’re in a position where you can’t use <code>gap</code>, <code>margin-trim</code> is awfully nice as it means you apply directional margin to all the children and not worry about an additional fancy selector to select the first or last one and remove that unneeded final margin. It might end up <a target=\"_blank\" href=\"https://chriscoyier.net/2023/06/12/margin-trim-as-a-best-practice/\">a best practice</a>.</p>\n<h3>Support</h3>\n<figure><table><tbody><tr><td><a target=\"_blank\" href=\"https://caniuse.com/mdn-css_properties_margin-trim\">Browser Support</a></td><td>✅ Safari<br />❌ Chrome<br />❌ Firefox</td></tr><tr><td>Progressive Enhancement?</td><td>Yes. A little extra space likely isn’t a major problem.</td></tr><tr><td>Polyfillable</td><td>No</td></tr></tbody></table></figure>\n<h3>Basic Example of Usage</h3>\n<p>The last paragraph here is a notorious situation where the bottom margin on it creates more space at the bottom than any of the other edges. With <code>margin-trim</code> we can ensure it’s sliced off without having to select that last paragraph and manually remove it.</p>\n</div>\n<div>\n<h2>Text Wrapping</h2>\n<h3>What is <code>text-wrap</code>?</h3>\n<p>The <code>text-wrap</code> property likely isn’t in your long term CSS memory. It’s capable of <code>text-wrap: nowrap;</code>, but we generally think of <code>white-space: nowrap;</code> for this. But now, <code>text-wrap</code> has two new tricks up it’s sleeve:</p>\n<ul>\n<li><code>text-wrap: balance;</code> — Attempt to make equal-width lines when text wraps.</li>\n<li><code>text-wrap: pretty;</code> — Avoid orphans.</li>\n</ul>\n<h3>When should you care?</h3>\n<p>A headline with one word orphaned onto the next line just looks really awkward and could be considered poor typography. There wasn’t a great way to solve this before, short of somewhat awkward tricks like inserting a <code>&amp;nbsp;</code> instead of a normal space between the last two words. <em>Balancing </em>a headline prevents this, but goes further in making the multiple lines of text generally the same width. Using <code>pretty</code> is more focused just on orphan prevention alone, making it more appropriate for body text.</p>\n<h3>Support</h3>\n<figure><table><tbody><tr><td><a target=\"_blank\" href=\"https://caniuse.com/css-text-wrap-balance\">Browser Support</a></td><td>Depends on which value. <code>balance</code> has decent support with all browsers ready or coming soon. <code>pretty</code>, <a target=\"_blank\" href=\"https://caniuse.com/css-text-wrap-balance\">less-so.</a></td></tr><tr><td>Progressive Enhancement?</td><td>Yes. While slightly less aesthetic, widows and orphans are not that big of a problem, so if this property doesn’t work, it’s no big deal.</td></tr><tr><td>Polyfillable</td><td><a target=\"_blank\" href=\"https://github.com/adobe/balance-text\">Yes.</a></td></tr></tbody></table></figure>\n<h3>Basic Example of Usage</h3>\n</div>\n<div>\n<h2>Subgrid</h2>\n<h3>What is Subgrid?</h3>\n<p>Subgrid is an optional part of using CSS grid that is relevant when you are nesting gridded elements. By setting <code>grid-template-columns: subgrid;</code> or <code>grid-template-rows: subgrid;</code> on a grid-level element, you’re saying “inherit these columns or rows from my parent grid, where relevant”. </p>\n<h3>When should you care?</h3>\n<p>The point of using grids for layout is generally <em>lining things up</em>. Without <code>subgrid</code>, it means that child elements of a grid don’t have access to the grid lines of the parent grid, and thus lack the opportunity help line things up. Subgrid fills that gap. When DOM nesting is important for functionality or accessibility, like in a <code>&lt;form&gt;</code>, subgrid can help <a target=\"_blank\" href=\"https://codepen.io/chriscoyier/pen/YzxqJap\">ensure things line up sensibly</a>.</p>\n<h3>Support</h3>\n<figure><table><tbody><tr><td><a target=\"_blank\" href=\"https://caniuse.com/css-subgrid\">Browser Support</a></td><td>Full</td></tr><tr><td>Progressive Enhancement?</td><td>Yes. You can fall back to defining your own grid lines that are workable if not perfect.</td></tr><tr><td>Polyfillable</td><td>No. There is <a target=\"_blank\" href=\"https://github.com/FremyCompany/css-grid-polyfill\">a grid polyfill</a> but it doesn’t do subgrid.</td></tr></tbody></table></figure>\n<h3>Basic Example of Usage</h3>\n</div>\n<h2>Things to keep an eye on…</h2>\n<p>The speed of CSS development doesn’t seem to have slowed down. There is plenty to continue to watch for and look forward to.</p>\n<ul>\n<li><a target=\"_blank\" href=\"https://css.oddbird.net/sasslike/mixins-functions/\">CSS Mixins &amp; Functions</a> — <em>actual</em> mixins and functions that take parameters</li>\n<li><a target=\"_blank\" href=\"https://developer.chrome.com/blog/css-relative-color-syntax\">Relative Color Syntax</a> — a way to manipulate the parts of colors in an intuitive and powerful way.</li>\n<li><a target=\"_blank\" href=\"https://webkit.org/blog/14955/the-web-just-gets-better-with-interop/\">Interop 2024</a> — All the things that we can essentially bet on for being cross-browser compatible soon, including the relative color syntax above.</li>\n<li><a target=\"_blank\" href=\"https://developer.chrome.com/docs/css-ui/css-field-sizing\">The CSS property <code>field-sizing</code></a> should help the long-standing difficult issue of auto-resizing form elements like textareas and input to the content they contain. </li>\n<li><a target=\"_blank\" href=\"https://open-ui.org/prototypes/selectmenu/\"><code>&lt;selectmenu&gt;</code> in HTML</a> is essentially a fully CSS styleable <code>&lt;select&gt;</code>, which is wild. </li>\n</ul>\n<p>That’s just a few things to watch. You might as well <a target=\"_blank\" href=\"https://frontendmasters.com/blog/feed/\">subscribe to our feed</a> as we’ll be doing the watching for you and then you’ll catch the next one.</p>\n<p>Did I miss a relatively new favorite of yours? Let me know.</p>\n </div>",
"author": "",
"favicon": "https://frontendmasters.com/favicon-16x16.png",
"source": "frontendmasters.com",
"published": "",
"ttr": 758,
"type": ""
}