Sub topic 7
Style containment
This chapter assumes you know the contain property from the previous chapters and have used CSS counters (counter-reset, counter-increment). Style containment is the least-used containment type. It limits a small set of style effects to one subtree.
What it scopes
contain: style stops certain style effects inside an element from affecting the rest of the page. In the current specification, two features are scoped:
- Counters.
counter-incrementandcounter-setinside the element behave as if the element had its owncounter-resetfor those counters. - Quotes. Nesting depth for
quotes(the opening and closing quote marks) starts fresh inside the element.
Style containment does not stop inheritance, selectors, or custom properties. A contained element still inherits color and font-family from its parent, and normal style rules still match across the boundary. The name is broader than the effect.
Counters with and without containment
Without containment, every element that increments a counter changes the shared value. The page below numbers the paragraphs, and one of them sits in a contained sidebar.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Style containment and counters</title>
<style>
body { counter-reset: item; }
p.item { counter-increment: item; }
p.item::before { content: counter(item) ". "; }
aside { contain: style; border: 1px solid gray; padding: 0.5em; }
</style>
</head>
<body>
<p class="item">First</p>
<aside>
<p class="item">Inside A</p>
<p class="item">Inside B</p>
</aside>
<p class="item">Second</p>
</body>
</html>With contain: style, the aside's paragraphs are numbered 1 and 2 inside the box. The outer paragraphs are numbered 1 and 2. The aside's increments never reach the outer counter. If you remove the contain line, the paragraphs are numbered 1, 2, 3, 4 in document order.
Why it matters for performance
Counters and quotes can make a change in one place alter the output somewhere else on the page. The browser must account for that when it recalculates style. Style containment tells it that a subtree cannot cause such effects outside itself. The benefit is real, but small compared with layout and paint containment.
Common errors
- Expecting variable isolation. Custom properties set inside the element still inherit downward and are not scoped. Do not use
contain: styleto sandbox them. - Unexpected numbering. If a numbered list continues across a component boundary, check whether an ancestor has
contain: content,strict, orcontent-visibility. Any of them resets counter scope. - Assuming browser parity. Support for
contain: stylehas been less uniform than for layout and paint. Test numbered or quoted content in each target browser.
Exercise
A page numbers figures with a counter named fig. A developer wraps each article in content-visibility: auto, and figure numbers now restart in every article. Why does this happen, and how would you keep one running number across the page?
Show answer
content-visibility: auto applies style containment, which scopes counter-increment to each article as if the article had its own counter-reset. To keep a running number, do not apply containment to elements that contain the counter increments. For example, put content-visibility on inner blocks that hold no figures, or compute the numbers another way, such as in HTML or with JavaScript.