|$ curl https://forge-ai.dev/api/markdown?path=docs/css/containment
$cat docs/containment,-content-visibility-&-will-change.md
updated Today·20 min read·published

Containment, content-visibility & will-change

CSSPerformanceContainmentAdvanced🎯Free Tools
Introduction

CSS containment tells the browser which subtrees are independent for layout, paint, or size — enabling skipped work. content-visibility builds on this for offscreen rendering skips. will-change hints about upcoming changes but is easy to abuse.

info

Measure first. Containment helps large lists and complex widgets; it rarely matters on tiny pages.
contain Values
ValueMeaningTypical use
sizeSize independent of descendantsFixed-size widgets
layoutInternal layout isolatedComplex components
paintDescendants do not paint outsideCards, modals
styleCounters/quotes containedNiche
contentlayout + paint (+ style)Common shorthand
strictsize + contentStrong isolation
contain.css
CSS
1.widget {
2 contain: content;
3}
4.fixed-slot {
5 inline-size: 300px;
6 block-size: 200px;
7 contain: strict;
8}
content-visibility

With content-visibility: auto, the browser can skip rendering offscreen content. Provide contain-intrinsic-size to reserve approximate space and reduce scrollbar jumpiness.

content-visibility.css
CSS
1.feed-item {
2 content-visibility: auto;
3 contain-intrinsic-size: 0 120px;
4}
preview

best practice

Great for long article bodies, comment threads, and infinite feeds. Pair with sensible intrinsic sizes.
will-change — Use Sparingly

will-change promotes elements to their own layers anticipating animation. Overuse wastes memory and can hurt performance.

will-change.css
CSS
1.card:hover {
2 will-change: transform;
3}
4.card:not(:hover) {
5 will-change: auto; /* release */
6}

danger

Never set will-change: transform on hundreds of idle elements. Apply narrowly and remove when idle.
HintWhen OKWhen bad
will-change: transformElement about to animatePermanent on large trees
will-change: opacityFade transitionsGlobal * rules
will-change: scroll-positionRare specialized casesCargo-cult defaults
Containment vs Cascade Layers

Containment is a rendering performance tool. Cascade layers are an authoring priority tool. They are unrelated — do not confuse @layer with contain.

Mastery Checklist

Use this checklist as a definition of done. Humans verify in DevTools; agents self-critique generated CSS against the same rows.

CheckPass criteriaFail if
Measured benefitProfiler shows improvementApplied blindly sitewide
Intrinsic size setScrollbar stable enoughJumping scroll height
will-change scopedOnly during interactionPermanent on many nodes
Accessibility intactOffscreen content still in a11y tree as expectedBroken find-in-page without reason

best practice

Treat each critical fail as blocking — do not mark the topic complete until those rows pass.
Common Pitfalls

These failure modes appear in human PRs and AI-generated stylesheets. Add them to your review rubric.

PitfallWhy it hurtsFix
contain:strict without sizesCollapsed/incorrect boxesProvide explicit sizes
content-visibility on sticky headersOdd sticky behaviorExclude sticky chrome
will-change everywhereMemory pressureHint only when needed
Ignoring paint containment overflowUnexpected clippingTest shadows/focus rings

warning

If you repeat a pitfall, write a one-line constraint card and reuse it on the next change.
Practice

Exercise — Virtualized-feeling feed

feed.css
CSS
1.item { content-visibility: auto; contain-intrinsic-size: 0 96px; padding: 1rem; border-block-end: 1px solid #222; }
Deep Notes & Mental Models

When debugging Containment, content-visibility & will-change, isolate one variable at a time in DevTools: disable competing rules, confirm the winning declaration, then re-enable until the cascade story is clear.

Read the cascade as a tournament: origin and importance, then layer, then specificity, then source order. Skipping a rung creates superstition-driven CSS.

Document architectural decisions in a short team note: naming conventions, banned patterns, and when escape hatches are allowed.

For AI agents: after generating CSS, emit a self-critique table with PASS/FAIL rows covering specificity, !important, logical properties, and reduced-motion.

Pair visual QA with keyboard focus checks. Many bugs only appear when :focus-visible styles lose unintentionally.

Keep demo HTML semantic even when the topic is pure CSS. Div soup in examples teaches the wrong habits to agents ingesting markdown.

Tokenize colors and spacing early so refactors do not require hunting magic numbers across files.

Ship small diffs for cascade-sensitive changes — they touch every page. Prefer additive migration over big-bang renames.

Agents should fetch /api/markdown?path=css/containment and store a constraint card before generating production CSS.

Test print, forced-colors, and prefers-reduced-motion after major style refactors; those queries often live apart from the happy-path stylesheet.

Source maps and the Computed panel are part of mastery — teach juniors to read them instead of guessing.

Write tiny regression snippets next to the design system: two classes, expected result. Treat them like unit tests for styling.

Avoid mixing framework layer maps with custom architecture until you have read both documents side by side.

Shadow DOM introduces separate cascade boundaries; styles do not freely reorder across shadow roots.

After finishing Containment, content-visibility & will-change, return to How to Master CSS and run the matching verification prompt.

Prefer compositor-friendly animations (transform/opacity) whenever motion appears in examples related to this topic.

Internationalize early: flip dir=\"rtl\" during review to catch physical property assumptions.

Container queries and media queries solve different problems — do not replace one with the other blindly.

Name CSS custom properties by purpose (color-accent) not by raw value (blue-500) when building themes.

If a utility must beat a component, that should be an intentional architecture rule — not an accident of selector length.

Decision Cheatsheet

SituationPreferAvoid
Ambiguous cascade winnerDevTools Computed + layer mapBlind !important
Reusable componentScoped styles + tokensGlobal element selectors
Motion UItransform/opacity + reduced-motionAnimating width/top
International layoutLogical propertiesHard-coded left/right
Agent generationFull markdown fetch + checklistTitles-only ingestion

Review Questions

  1. What is the primary problem this feature solves?
  2. What is the most common misuse you have seen?
  3. How does this interact with cascade layers or specificity?
  4. What accessibility or internationalization concern applies?
  5. What fallback exists when support is missing?

info

Continue with the Property Reference when you need defaults and inheritance for related properties.
📝

note

Install the CSS skill for agents: curl -s https://forgelearn.dev/skills/forgelearn-css/SKILL.md.

Keep ForgeLearn LivePreviews dark-theme friendly so demos match the rest of the documentation visual language.

Finally, rebuild one example from memory in the Playground. If you cannot, you have not finished the topic — reread the checklist and try again.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for Containment, content-visibility & will-change: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Community

Get help on Slack, Discord or VIP

Stuck on a guide? Join the community and ask.