Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 37 ยท CSS and Rendering

Containment, content-visibility and Smooth Animations

Speed up rendering with CSS contain and content-visibility, animate with transform and opacity, use will-change wisely and respect prefers-reduced-motion.

Rendering cost grows with the number of elements the browser must consider. Two CSS features let you tell the browser "this part of the page is independent, so you do not have to look at it unless you must": containment and content-visibility. Another set of habits keeps animations smooth by staying on the compositor. This lesson covers both.

CSS containment

contain promises that a subtree is isolated from the rest of the page. The browser can then skip recalculating outside work when something inside changes.

CSS
.card {
  contain: layout paint style;
}
/* shorthand values */
.widget { contain: content; } /* layout + paint + style */
.panel  { contain: strict; }  /* size + layout + paint + style; you must set a size */
  • layout: nothing inside affects layout outside, and vice versa.
  • paint: descendants are clipped to the box and are not painted outside it.
  • size: the box's size does not depend on its children (you must give it a size).
  • style: counters and quotes inside do not leak out.

Use containment on repeated components such as cards, comments and table rows, where changing one should not trigger work for 500 siblings.

content-visibility: skip offscreen rendering

content-visibility: auto goes further: the browser skips layout and paint entirely for elements outside the viewport until they are near it. On long pages, this can reduce rendering work dramatically.

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

Because the browser does not lay out skipped content, it does not know its height, so the scrollbar could jump. contain-intrinsic-size gives a placeholder height (the auto keyword remembers the real height once it has been rendered).

Skipping offscreen sections

Run it once with the box unticked and once ticked. Browsers that support the property typically render the second case much faster because only visible sections are laid out. (Unsupported browsers ignore the rule and show equal times.) Keep content visible to search and find-in-page; auto does, but hidden does not render at all.

Which properties are cheap to animate

Recall from the critical rendering path that transform and opacity can be animated by the compositor without layout or paint. For smooth animations use only those two:

CSS
/* janky: triggers layout every frame */
.menu { transition: left 300ms; }
.menu.open { left: 0; }

/* smooth: composited */
.menu { transform: translateX(-100%); transition: transform 300ms; }
.menu.open { transform: translateX(0); }

Use scale instead of animating width and height, translate instead of top and left, and fade with opacity instead of toggling display.

Compositor-friendly animation

will-change: a scalpel, not a hammer

will-change: transform tells the browser in advance to promote an element to its own layer. That can avoid a stutter at the start of an animation, but every layer uses GPU memory.

CSS
.drawer { will-change: transform; }

Apply it only to elements that will really animate soon, and remove it after the animation (add it on hover or via JavaScript just before the animation starts). Setting will-change on everything slows pages down.

Avoid expensive paint effects

Large blur filters (filter: blur(40px)), many overlapping box shadows and backdrop-filter on big areas can cost a lot of paint time, especially on phones. Test with the DevTools Rendering panel: "Paint flashing" shows what repaints, and the "Layers" panel shows compositing.

Respect reduced motion

Some people get dizzy from large motion. The prefers-reduced-motion media query lets you remove or soften animation, and it also saves work:

CSS
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

Other helpful features

  • will-change and contain pair well on scrolling lists.
  • For huge lists (thousands of rows), use virtualization: render only the rows in view and swap them as the user scrolls.
  • Reduce the DOM: each node costs memory and style work.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does content-visibility auto do?

    It saves rendering work for content the user cannot yet see.
  2. 2. Why pair content-visibility with contain-intrinsic-size?

    Skipped content has no layout, so the browser needs an estimated size.
  3. 3. Which pair of properties is best for smooth animation?

    Transform and opacity can be animated by the compositor without layout or paint.
  4. 4. What is the risk of putting will-change on many elements?

    Layers are not free; use will-change sparingly and remove it afterward.

Summary

  • contain isolates components so changes inside do not cause page-wide work.
  • content-visibility: auto skips rendering offscreen content; add contain-intrinsic-size.
  • Animate transform and opacity, not layout properties.
  • Use will-change sparingly and honor prefers-reduced-motion.

Next: Lighthouse, PageSpeed and Lab Testing