CSS performance

Advanced · 18 min

Where CSS really costs time, from render-blocking stylesheets to large-page recalculation, and the fixes: content-visibility, containment and measuring first.

Before thisAccessible CSSAnimation performance

Chrome for Developers · 18 min

CSS performance

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Chrome for DevelopersReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • CSS costs time in two places: getting the stylesheet to the browser (it blocks rendering until it arrives) and making the browser work out styles, layout and paint on a large page.
  • Most selector-level micro-optimisation is not worth your time in modern browsers. A huge DOM, a lot of unused CSS, and rendering work for content nobody can see matter much more.
  • `content-visibility: auto` tells the browser it can skip layout and paint for off-screen sections until they are near the viewport. Pair it with `contain-intrinsic-size` so the scrollbar does not jump.
  • CSS containment (`contain: layout paint style`, or `content`) promises the browser that a subtree is independent, so changes inside it do not force the rest of the page to be recalculated.
  • Measure before and after with the Performance panel and Lighthouse. A technique that helps one page can do nothing for another.

Measure first

A common mistake is to treat CSS performance as a list of rules, such as “avoid descendant selectors” or “do not use *”. Most of those rules came from browsers of fifteen years ago. Today’s engines match selectors very fast, and a page almost never loses time to a clumsy selector.

CSS costs time in two other ways, and those are worth knowing:

  1. Delivery. The browser cannot paint until it has the stylesheets in the <head>.
  2. Work. On a big page, style recalculation, layout and paint can each take real time, especially when something changes often.

The frontend roadmap’s web performance and Core Web Vitals lesson covers the metrics. This lesson covers the CSS-specific levers.

Delivery: stylesheets block rendering

By default, a <link rel="stylesheet"> in the head blocks rendering. The browser waits for it, because painting without styles flashes an unstyled page. A slow or huge stylesheet therefore delays everything the user sees.

Ways to help:

  • Ship less. Remove unused rules (Tailwind-style generation does this by design, as that lesson showed), minify, and serve with Brotli or gzip. Lightning CSS does minification and merging.
  • Avoid @import chains in CSS files. A browser that reads @import url(b.css) inside a.css finds out about b.css only after downloading a.css, so the downloads happen one after another. Use <link> tags, or let a bundler inline the imports.
  • Split by media. A stylesheet with a media attribute that does not match, such as <link rel="stylesheet" href="print.css" media="print">, does not block rendering on screens. The print stylesheets lesson uses this.
  • Inline critical CSS. For a few kilobytes of above-the-fold styles, inlining them avoids a request in the critical path, with the rest loaded after. Tools can extract this automatically. Weigh the extra build complexity against the gain.
  • Fonts. Custom fonts delay text. font-display and preloading are covered in font loading.

Work: style, layout and paint on large pages

The browser’s rendering steps are covered in animation performance. On a big page, two properties help the browser do less.

content-visibility

.article-section {
  content-visibility: auto;
  contain-intrinsic-size: auto 600px;
}

content-visibility: auto says that while this element is off-screen, the browser may skip rendering its contents (style, layout and paint) and render them when the element approaches the viewport. For a long page of independent sections, such as a feed or an article, that can cut the initial rendering cost sharply.

The catch: a skipped element has no size from its content, so it collapses, and the scrollbar jumps as sections render. contain-intrinsic-size gives it a placeholder size, and the auto keyword remembers the real size after the first render.

The Chrome video here is titled “Slashing layout cost with content-visibility”. Its description says that a quick CSS change to the HTML specification took layout time from 50 seconds down to 400 ms. The skipped content stays in the accessibility tree and findable with the browser’s find-in-page, according to the web.dev article. Support, per MDN’s compatibility data: Chrome 85, Firefox 125 and Safari 18.

Containment

.widget { contain: layout paint style; }
/* or the shorthand */
.widget { contain: content; }

contain is a promise to the browser: this subtree does not affect layout outside it, and nothing paints outside it. With that guarantee, when something inside the widget changes, the browser does not have to re-check the rest of the page. content-visibility: auto applies containment automatically. Containment can change behaviour: contained elements become their own containing block for positioned descendants and clip painting, so test the visuals. contain: size also needs explicit dimensions or the element collapses.

Smaller habits

  • Prefer fewer, flatter DOM nodes. DOM size is a larger factor than selector choice.
  • Avoid animating layout properties, and apply will-change sparingly, as the animation lesson explains.
  • Do not load CSS you do not use on a page, such as a whole component library for two components.
  • Reduce repaints from expensive effects: big blurs, backdrop-filter and large box shadows on many elements.
  • Check for layout shifts caused by CSS: images without dimensions, fonts that swap with different metrics, late-injected content. Aspect ratio and responsive media shows how to reserve space.

How to measure

  • Lighthouse and PageSpeed Insights flag render-blocking resources and unused CSS.
  • Chrome Coverage tool (DevTools, More tools) shows how much of each stylesheet ran on the page.
  • Performance panel shows time in “Recalculate Style”, “Layout” and “Paint” per frame or interaction. The DevTools lesson covers the panels.
  • Real-user monitoring for field data, because your laptop is not your visitor’s phone.

Make one change at a time and measure before and after. Many CSS performance tips help one page and do nothing for another.

What the video covers

Chrome for Developers’ “Slashing layout cost with content-visibility - HTTP 203” (October 2020, about 18 minutes) is an episode of the HTTP 203 series. Per its description, Jake makes a quick change to the CSS of the HTML specification and takes layout time from 50 seconds down to 400 ms, then spends longer discussing it with Surma. It covers one technique, content-visibility, which is only part of this lesson; the delivery and containment sections come from the documentation. It is from 2020, and Safari support has since arrived, so its browser-support remarks are out of date. I could not read a transcript. A 2026 PodRocket conversation with Michael Hladky on content-visibility, containment and INP is in further viewing; it is a podcast, so it is light on visuals.

Where this fits

This lesson extends animation performance from single animations to whole pages. The last lesson in the roadmap uses the media attribute trick: print stylesheets.

Check yourself

  1. 01Why does a large stylesheet delay the first paint, and what are two ways to reduce that?

    Browsers treat stylesheets in the `<head>` as render-blocking: they will not paint until the CSS is downloaded and parsed, because painting without it would show a flash of unstyled content. You can reduce the cost by shipping less CSS (remove unused rules, minify, compress with Brotli or gzip) and by inlining the small amount of critical CSS while loading the rest without blocking, for example with the `media` attribute trick or a tool that automates it.

  2. 02What does `content-visibility: auto` do, and what problem does `contain-intrinsic-size` solve with it?

    It lets the browser skip rendering an element's contents while the element is off-screen, saving style, layout and paint for long pages, and then render it as it nears the viewport. A skipped element has no content size, so it collapses and the scrollbar jumps as sections render. `contain-intrinsic-size: auto 500px` gives it a placeholder height (and `auto` remembers the real one once rendered).

  3. 03A page has 20,000 DOM nodes and animates a small widget, and every frame recalculates layout for the whole page. What would you try?

    Isolate the widget with containment (`contain: layout paint` or `content`) so its changes cannot affect outside layout, and reduce what is in the DOM at once, for instance with `content-visibility: auto` on long off-screen sections. Then confirm in the Performance panel that style and layout times per frame dropped.

  4. 04Is rewriting `.nav ul li a` to a single class a worthwhile optimisation?

    Rarely. Browsers match selectors right to left and are heavily optimised, so selector complexity almost never shows up as a bottleneck. Specificity and maintainability are better reasons to prefer flat selectors. Look first at stylesheet size, render blocking, DOM size and what changes each frame.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark