- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS Performance
CSS Output & Performance
CSS Performance
Use containment and deferred rendering where measurement shows a real rendering cost. You will use layout and paint containment, content-visibility, intrinsic placeholder sizes, and measurement-driven performance checks.
Most CSS is fast enough without tricks. But on long pages, like a feed with hundreds of posts, a few properties let you tell the browser "you can skip this part for now". content-visibility: auto is the most useful of them.
Measure first. Open DevTools, record a performance profile, and only add these properties where the browser is actually spending time on rendering. Animation performance is touched on in CSS Animations, and expensive visual effects in CSS Filters.
Skip work you cannot see
Example
css
.dashboard-widget {
contain: layout paint;
}
.long-feed-item {
content-visibility: auto;
contain-intrinsic-size: auto 320px;
}contain: layout paintpromises that the widget's inside never affects the outside. The browser can then lay it out and paint it on its own, without checking the rest of the page.content-visibility: autolets the browser skip rendering feed items that are far off-screen, and render them just before they scroll into view. The content stays in the page, so search, links and screen readers still work.
Give skipped content a size
While an item is skipped, the browser does not know how tall it is. contain-intrinsic-size: auto 320px says "assume it is about 320px tall". Without it, the scrollbar jumps around as items render. With auto, the browser remembers the real height once it has seen it.
The fixes that matter more often
- Reduce unused CSS and avoid shipping whole frameworks for a few utilities.
- Compress and subset fonts; preload only critical files.
- Animate transforms and opacity when they produce the required effect.
- Avoid large filtered or blurred surfaces that repaint during scrolling.
- Measure in browser performance tools before and after an optimization.
Most slow pages are slow because of what they load, not because of how CSS is written: huge images, too many font files, unused framework CSS. Fix those first.
will-change, briefly
Example
css
.animated-panel.is-about-to-move {
will-change: transform;
}will-change tells the browser an element is about to animate, so it can prepare. That preparation costs memory and creates a new stacking context, so add it just before an animation starts and remove it afterwards. Setting will-change on many elements permanently usually makes things slower, not faster.
Containment Properties
| Property | Purpose | Example |
|---|---|---|
| contain | Isolates selected layout, paint, size, or style work. | contain: layout paint |
| content-visibility | Skips rendering work for off-screen content. | content-visibility: auto |
| contain-intrinsic-size | Provides a placeholder size for skipped content. | contain-intrinsic-size: auto 500px |
| will-change | Provides a temporary preparation hint for an expected change. | will-change: transform |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS Performance example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
.dashboard-widget {
contain: layout paint;
}
.long-feed-item {
content-visibility: auto;
contain-intrinsic-size: auto 320px;
}
.animated-panel.is-about-to-move {
will-change: transform;
}
</style>
</head>
<body>
<main><section class="dashboard-widget"><h1>Contained widget</h1><p>This region has layout and paint containment.</p></section><section class="long-feed-item"><h2>Deferred feed item</h2><p>Scroll it in and out of view.</p></section></main>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| contain | 52 | 79 | 69 | 15.4 |
| content-visibility | 85 | 85 | 125 | 18 |
| will-change | 36 | 79 | 36 | 9.1 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
Good to know
contain: paintclips anything that sticks out of the element, such as a dropdown menu. Test components after adding containment.content-visibilitycan breakposition: stickyand in-page anchor jumps in some layouts. Check them after the change.
CSS Performance exercises
Contain the layout
Tell the browser that the layout inside the widget does not affect the rest: contain layout.
The HTML your code works on
<div class="widget">Widget</div>
<div class="feed">Feed</div>Result
