Performance Tuning
Two CSS properties let you give the browser advance notice about how your page will render: will-change tells it which properties will animate so it can prepare compositor layers, and content-visibility tells it which sections are off-screen so it can skip rendering them entirely. Both are targeted fixes, not defaults. Applied carelessly they consume GPU memory and introduce layout bugs. Applied at the right moment they produce measurable improvements you can see in a DevTools Performance trace.
This choose topic is different from the others. The CSS authoring surface is small: a handful of lines on the right selectors. The real skill is DevTools literacy: knowing what to look for in a trace, recognizing when these properties actually help, and being able to explain the rendering pipeline to someone who has never seen it. Your job here is to understand the concepts deeply enough to teach them. The guided exercise at the end gives you one concrete trace to record so you have seen the evidence with your own eyes before you walk your teammates through it.
The Keyframes and Reduced Motion learning activity covered will-change in the context of animation performance. This page builds on that foundation and adds the DevTools trace workflow and content-visibility. If you have not read that page, start there for the compositor pipeline explanation.
will-change: Promoting Layers at the Right Time
will-change hints to the browser that a property is about to change, prompting it to promote the element to its own GPU compositor layer in advance. This eliminates the brief jank that can occur at animation start while the browser scrambles to set up the layer mid-frame.
/* Applied just before animation starts */
.card.is-animating {
will-change: transform, opacity;
}
/* Removed after animation ends */
.card {
will-change: auto; /* default: no hint */
}
The GPU Memory Cost
Every promoted layer consumes GPU memory for its own texture. On a page with a handful of animated elements this is negligible. On a page where will-change is applied to every card, every list item, or as a blanket rule on *, the total texture memory can exceed what the GPU can handle efficiently and actually slow rendering down. The Layers panel in Chrome DevTools (three-dot menu → More tools → Layers) shows every promoted layer and its memory cost. Open it before and after adding will-change to verify the trade is worth making.
The Correct Pattern
Apply will-change dynamically via JavaScript just before the animation starts, and remove it as soon as the animation ends. For hover-triggered animations, applying it on the parent's :hover state gives the browser one frame of preparation:
/* Hint fires one frame before the animation needs it */
.card:hover {
will-change: transform;
}
.card:hover .card-inner {
transform: translateY(-4px);
transition: transform 200ms ease-out;
}
If your animation changes a layout-triggering property like width, height, or margin, will-change cannot make it smooth. The layout recalculation still happens on the main thread every frame regardless of layer promotion. The correct fix is switching to transform and opacity. Only reach for will-change after you have already confirmed you are animating compositor-friendly properties.
content-visibility: auto
content-visibility: auto instructs the browser to skip layout, paint, and compositing for off-screen sections entirely. The browser treats them as if they exist but have not been rendered yet. When the user scrolls them into view, they render on demand. On long pages with many complex sections, this can reduce initial render time dramatically.
.article-section {
content-visibility: auto;
contain-intrinsic-size: auto 600px; /* estimated height placeholder */
}
contain-intrinsic-size
When content-visibility: auto skips a section, that section has no rendered height. Without a placeholder, the scrollbar would shrink and jump as sections render. contain-intrinsic-size provides an estimated height the browser uses as a placeholder before the section renders. Use a reasonable estimate based on your content; the actual rendered height takes over once the section enters view. The auto keyword prefix caches the last known rendered size, reducing placeholder inaccuracy after the first scroll through:
.article-section {
content-visibility: auto;
contain-intrinsic-size: auto 400px; /* auto caches actual height after first render */
}
When to Use It
content-visibility: auto earns its place on pages with many long, complex off-screen sections: long-form articles, documentation pages with many code examples, or dashboards with multiple data-heavy panels. It is less useful on short pages where most content is near the viewport on load.
What It Is Not
content-visibility: auto does not hide content from users or assistive technology. The content is still in the DOM and accessible to screen readers. It is a rendering optimization, not a visibility toggle. Do not confuse it with display: none or visibility: hidden.
If a section uses animation-timeline: view() or a ScrollTrigger that fires on entry, applying content-visibility: auto to that section can interfere with the trigger timing. The browser may not have rendered the element when the scroll position passes it. Test carefully before combining these features.
Reading a Performance Trace
The Chrome DevTools Performance panel records a frame-by-frame trace of everything the browser does. Recording one before and after a performance change is how you verify that a fix actually helped.
Recording a Trace
Open DevTools, go to the Performance tab, click the record button, interact with the page for two to three seconds, then stop. For animation performance, interact during a triggered animation. For content-visibility gains, scroll the full page slowly during recording.
What to Look For
flowchart TD
A["Performance Trace"] --> B["Main Thread"]
A --> C["Compositor Thread"]
B --> D["Long yellow blocks\n= JavaScript"]
B --> E["Purple blocks\n= Layout recalc"]
B --> F["Green blocks\n= Paint"]
C --> G["Thin green bars\n= Compositor-only frames\n(good: off main thread)"]
E --> H{"Width > 50ms?"}
H -->|Yes| I["Layout thrash\ncheck for forced reflows"]
H -->|No| J["Acceptable"]
- Main thread blocks longer than 50ms cause visible jank. They appear as tall colored bars in the main thread lane.
- Purple Layout bars indicate layout recalculation. If these appear on every animation frame, your animation is triggering layout.
-
Green Paint bars indicate pixel repainting. Compositor-only animations (
transform,opacity) should not produce these during animation. - The Compositor thread running independently of the main thread is the goal. A smooth animation should show minimal main thread activity and consistent compositor thread frames.
The Before and After Story
A useful trace workflow: record a baseline with no optimizations, note the main thread block durations and frame rate, apply will-change or switch to compositor-friendly properties, record again, and compare. The improvement should be visible as shorter or absent layout and paint bars and a steadier frame rate line at the top of the trace.
For content-visibility, compare the initial render time shown in the Network waterfall before and after. Pages with many complex sections can see initial render drop by hundreds of milliseconds.
Guided DevTools Exercise
You do not need to build a new page for this topic. Instead, pick any existing page on your Practice Site that has multiple sections and at least one animation. You are going to record two traces and compare them.
Step 1: Baseline Trace
Open your chosen page in Chrome. Open DevTools and go to the Performance tab. Click record, scroll the full page slowly from top to bottom, trigger any animations on the page, then stop. Save this trace or screenshot the main thread lane. Note the longest main thread block duration and whether you see purple Layout or green Paint bars during animation.
Step 2: Add the Properties
In your page's stylesheet, add content-visibility: auto and contain-intrinsic-size to your main content sections. Add will-change to the hover or animation state of one element that animates. Keep both additions minimal and commented:
/* content-visibility: skips rendering off-screen sections */
.page-section {
content-visibility: auto;
contain-intrinsic-size: auto 500px;
}
/* will-change: promotes layer before hover animation fires */
.card:hover {
will-change: transform;
}
Step 3: After Trace
Record the same interaction again. Compare the two traces. On a short Practice Site page the gains may be subtle. That is a valid finding: note it in your report rather than pretending the numbers improved dramatically. The honest observation is the point.
Step 4: Layers Panel
Open the Layers panel (three-dot menu → More tools → Layers). Hover over your animated element and confirm it shows as a promoted layer. Remove will-change temporarily and refresh — the layer should disappear. Add it back and screenshot both states for your teaching video.
Teaching Video Checklist
Your teammates have not recorded a Performance trace before. Your job is to make the rendering pipeline story concrete and show them evidence. Cover these points:
- Explain the layout, paint, composite pipeline in plain language before opening any tools. Use an analogy if it helps. The goal is that teammates understand what the browser is doing on each frame before they see it in the trace.
-
Show the Layers panel with
will-changeoff, then on. Point at the promoted layer and explain what GPU memory cost means in practice. Show the before and after screenshot. - Walk through your two traces side by side. Point to the main thread lane and narrate what you see: "here the browser is doing layout, here it is painting, here the compositor is running independently." Connect what you see to the concepts you just explained.
- Be honest about whether your Practice Site page showed meaningful gains. A short page with simple animations may show little difference. Explain why that is the expected result and what kind of page would show a larger improvement.
-
End with the "when not to use it" message:
will-changeon everything costs memory,content-visibilityon short pages buys nothing, and neither property fixes a non-compositor animation.
The most valuable thing you can give teammates is the confidence to open the Performance panel and know what they are looking at. A five-minute trace walkthrough with narration is more useful than a perfect before/after number.
Resources
- MDN: content-visibility — Full property reference including auto, hidden, and visible values
- MDN: contain-intrinsic-size — Placeholder size reference and the auto caching keyword
- MDN: will-change — Property reference with memory cost cautions and correct usage patterns
- Chrome DevTools: Performance panel — Official guide to recording and reading traces including main thread and compositor lane explanations