Sub topic 7
Style containment
This chapter assumes you know the contain property from the overview chapter. It also assumes you know what layout, paint, and size containment each guarantee, and that you can read basic HTML and CSS. Style containment is the least dramatic of the four values. It does not change how anything looks by default, and it is easy to misread, so this chapter spends its time on what it does and what it doesn't do.
What style containment guarantees
You enable it with contain: style. In the current specification it makes one promise: CSS counters are scoped to the element's subtree. A counter is a number the browser keeps while it walks the document. You change it with counter-reset, counter-increment, and counter-set, and you print it with counter() in generated content.
Without style containment, a counter is shared across the document. An element deep in a widget that increments item also changes the number that elements outside the widget will see later. With contain: style, the browser treats the contained element as if it were the root of its own document for counter purposes. Increments made inside it cannot leak out, and the outside counter value is unaffected.
The reason this matters for performance is the same reason the other containment types matter. If the browser knows a subtree cannot change style-derived values outside it, it can compute that subtree without tracking its effects on the rest of the page. Style containment is mostly a guarantee that other features rely on. Few people reach for it by hand.
A worked example
This page numbers items with a counter. The middle group is a widget with its own items.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Style containment demo</title>
<style>
body { counter-reset: item; font-family: sans-serif; }
.item::before {
counter-increment: item;
content: counter(item) ". ";
}
.widget { margin-left: 1.5rem; }
.widget.contained { contain: style; }
</style>
</head>
<body>
<h2>Without contain: style</h2>
<div class="item">Alpha</div>
<div class="widget">
<div class="item">Widget one</div>
<div class="item">Widget two</div>
</div>
<div class="item">Omega</div>
<h2>With contain: style</h2>
<div class="item">Alpha</div>
<div class="widget contained">
<div class="item">Widget one</div>
<div class="item">Widget two</div>
</div>
<div class="item">Omega</div>
</body>
</html>Both lists share one counter, because body resets it once. Note that the ::before pseudo-element is what increments it, so the increments happen in document order.
In the first list, the numbers run straight through. Alpha is 1, the widget items are 2 and 3, and Omega is 4.
In the second list, the widget is a counter scope of its own. Alpha is 1, but the widget items start a fresh counter and read 1 and 2. Omega then continues the outer count and reads 2, because the widget's increments never reached it.
Notice the second list's continuing numbers. The counter outside the widget did not reset. It simply never saw the widget's increments. Because the heading h2 elements do not touch the counter, the second list also keeps counting from where the first one ended. If you want each list to start at 1, add counter-reset: item to each list's container.
What style containment does not do
Most mistakes with this value come from expecting more than it gives. These properties are not scoped by contain: style:
- Custom properties. Variables like
--accentstill inherit down into the subtree. A custom property set inside the subtree still applies only to descendants, as it always did. Style containment does not stop inheritance. - Normal inherited properties.
color,font-family, and the rest inherit into a contained element as usual. - Selectors. A rule outside the element can still match elements inside it. Style containment is not a shadow boundary and does not scope CSS rules.
Older drafts of the specification also listed the quotes property as part of style containment. That part was dropped, so do not design anything that depends on it.
Where you get it without asking
Because it is cheap and rarely visible, style containment is bundled into other features. You often have it already.
| Declaration | Includes style containment? |
|---|---|
| `contain: style` | Yes, alone |
| `contain: content` | Yes (layout, paint, style) |
| `contain: strict` | Yes (size, layout, paint, style) |
| `content-visibility: auto` | Yes, along with layout, paint, and size while skipped |
| `content-visibility: hidden` | Yes, along with layout, paint, and size |
| `container-type: inline-size` | Yes, along with layout and inline-size containment |
| `container-type: size` | Yes, along with layout and size containment |
This table explains a real surprise. If you add content-visibility: auto to a long list of numbered sections, a counter incremented inside one section stops reaching later sections. The numbering that used to run through the document now restarts in every section. Nothing in your CSS says contain: style, yet the effect is the same.
Fixing broken numbering
When style containment breaks a numbering scheme, you have three options:
- Remove the feature that implies it. This is the right choice if the numbering matters more than the speed-up.
- Move the counter work out of the contained subtree. Number the section headings in a parent that is not contained.
- Compute the numbers in HTML or JavaScript. An ordered list (
<ol>) withstartorvalueattributes sets numbers explicitly and does not depend on CSS counters.
/* Fragile: numbering lives inside the contained section */
.chunk { content-visibility: auto; }
.chunk h2::before {
counter-increment: section;
content: counter(section) ". ";
}
/* Safer: the counter is incremented on the uncontained wrapper */
.chapter { counter-increment: section; }
.chapter > .chunk { content-visibility: auto; }
.chapter::before { content: counter(section) ". "; }In the second version, the element that increments the counter sits outside the contained element, so the increment stays visible to later siblings.
Checking for it in practice
You will not see a visual change from contain: style unless counters are involved, so verify it deliberately. Open DevTools and select the element. Look at the Computed pane for the contain value, and for content-visibility and container-type, since those imply it. Then compare the rendered numbers against the counter values you expect. If a number restarts where you did not expect it, search your stylesheet for the declarations in the table above.
Exercise
A long article numbers its sections with a CSS counter incremented in each section h2::before. After you add content-visibility: auto to every section, the section numbers all show 1. Why, and name one fix.
Show answer
content-visibility: auto implies style containment, which scopes counters to each section's subtree. Each section's increment starts a new counter instead of continuing the document-wide one. Fixes: increment the counter on an element outside the contained section (such as a wrapper), use an <ol> with explicit value attributes, or drop content-visibility: auto from the numbered sections.
Exercise
A component sets --theme: dark inside an element with contain: style. Does an element outside the component see --theme? Does a child inside the component see a custom property set on an ancestor outside it?
Show answer
No for the first question: custom properties only inherit downward, so outside elements never saw it, with or without containment. Yes for the second question: style containment does not stop inheritance, so the child still inherits the ancestor's custom property.