How to Supercharge Web Rendering with CSS content-visibility, will-change & contain
This article explains how modern CSS features such as content-visibility, will-change, contain, font-display, scroll-behavior, @import avoidance, media‑query splitting, and CSS custom properties can be used together with practical code examples to dramatically reduce page‑render time, improve smooth scrolling, and avoid layout shifts for faster, smoother web experiences.
Content Visibility (content-visibility)
The
content-visibilityproperty, part of the CSS Containment Module Level 2, lets browsers skip rendering off‑screen elements, dramatically cutting render time for pages with many hidden items.
It accepts
visible,
autoand
hidden. Using
content-visibility:autoon a large list of cards reduces a 1037 ms render to about 150 ms, roughly a six‑fold speed‑up.
<code>.card {
content-visibility: auto;
}
</code>When height is not explicitly set, the element’s height is treated as
0, which can break scrolling; the
contain-intrinsic-sizeproperty can provide a placeholder size to keep layout stable.
<code>.card {
content-visibility: auto;
contain-intrinsic-size: 200px;
}
</code>Using will-change for GPU‑accelerated animations
The
will-changehint tells the browser which properties will change, allowing it to create a separate compositing layer and move work to the GPU. It accepts values such as
auto,
scroll-position,
content, or a custom property like
transformor
opacity.
Example:
<code>.animate {
will-change: opacity;
}
</code>Apply the hint only when needed and remove it after the animation to avoid unnecessary memory usage.
<code>var el = document.getElementById('element');
el.addEventListener('mouseenter', function() { this.style.willChange = 'transform, opacity'; });
el.addEventListener('animationend', function() { this.style.willChange = 'auto'; });
</code>Containment with contain
The
containproperty isolates an element from the rest of the DOM, allowing the browser to limit layout, paint, and size calculations to that subtree. Values include
layout,
paint,
size,
content(shorthand for
layout paint) and
strict(shorthand for
layout paint size).
Example with a large list of 10 000 items shows render time dropping from ~4 ms to ~0.04 ms when
contain:strictis applied.
<code>.item {
contain: strict;
}
</code>Font loading with font-display
When using
@font-face, the
font-displaydescriptor controls how fallback fonts are shown while the custom font loads, preventing Flash‑of‑Unstyled‑Text (FOUT). Values include
auto,
block,
swap,
fallbackand
optional.
<code>@font-face {
font-family: "Open Sans Regular";
src: url("fonts/OpenSans-Regular.woff2") format("woff2");
font-display: swap;
}
</code>Smooth scrolling with scroll-behavior
Setting
scroll-behavior:smoothon the
htmlelement enables native smooth scrolling for anchor navigation and programmatic scroll calls.
<code>html { scroll-behavior: smooth; }
</code>Avoiding @import and splitting CSS
Using multiple
<link>tags with appropriate
mediaqueries reduces render‑blocking time compared to nesting stylesheets with
@import, which forces sequential network requests.
<code><!-- Critical CSS -->
<link rel="stylesheet" href="styles.css" media="all" />
<!-- Form‑factor specific CSS -->
<link rel="stylesheet" href="sm.css" media="(min-width: 20em)" />
<link rel="stylesheet" href="md.css" media="(min-width: 64em)" />
<link rel="stylesheet" href="lg.css" media="(min-width: 90em)" />
</code>CSS Custom Properties (variables)
Defining variables on
:rootcreates global values that can be reused throughout the stylesheet. Changing a root variable with
style.setPropertycan trigger large style recalculations, so it should be used sparingly.
<code>:root { --color: red; }
button { color: var(--color); }
</code>Performance varies across browsers; Safari handles inline style updates quickly, while Firefox is slower, making
setPropertythe preferred method for many cases.
Conclusion
Even with modern hardware and fast networks, careful CSS optimisation—using
content-visibility,
will-change,
contain, proper font‑loading strategies, smooth scrolling, media‑query splitting, and disciplined use of custom properties—remains essential for delivering fast, fluid web experiences.
Taobao Frontend Technology
The frontend landscape is constantly evolving, with rapid innovations across familiar languages. Like us, your understanding of the frontend is continually refreshed. Join us on Taobao, a vibrant, all‑encompassing platform, to uncover limitless potential.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.