Sub topic 11
Reserving space with contain-intrinsic-size
This chapter assumes you know the CSS box model, block layout, and how to read a scrollbar. You should also have read the earlier chapters on size containment and on content-visibility. Here you will learn how contain-intrinsic-size gives a skipped element a size, and how the auto keyword keeps scrolling stable.
The problem: skipped content has no size
An element with content-visibility: auto that is off-screen gets size containment. Size containment tells the browser to lay the element out as if it had no children. If you set no width or height, the element's block size collapses to zero.
That is a problem for scrolling. Suppose a feed has 200 items that are each about 600px tall. While they are skipped, they take no space. The scrollbar thumb is huge, because the page looks short. As the user scrolls, items render and grow to their real height. The page gets taller, and the thumb shrinks and jumps. Content can also shift under the user's pointer.
contain-intrinsic-size fixes this. It gives a size-contained element a placeholder size to use instead of its content size.
The basic syntax
The property takes a width, a height, or both.
.feed-item {
content-visibility: auto;
contain-intrinsic-size: 800px 600px;
}With one value, it applies to both axes. With two values, the first is width and the second is height. A value of none means "no placeholder", which is the default.
There are also longhand properties for each axis, and they follow the logical-property pattern:
| Property | Axis |
|---|---|
| `contain-intrinsic-width` | Width |
| `contain-intrinsic-height` | Height |
| `contain-intrinsic-inline-size` | Inline axis |
| `contain-intrinsic-block-size` | Block axis |
For vertical scrolling in a horizontal writing mode, the height (block size) is the value that matters. Width usually comes from the parent's layout anyway.
When the placeholder is used
The placeholder applies only when the element is size-contained. That happens in these cases:
- You set
contain: sizeorcontain: strict. - You set
content-visibility: autoand the element is currently skipped. - You set
content-visibility: hidden.
When content-visibility: auto is on but the element is on-screen, it is not size-contained. The browser renders it and uses its real content size. The placeholder is ignored.
The placeholder also does not override an explicit size. If you set height: 300px, the element is 300px tall whether or not it is skipped.
The auto keyword
A fixed placeholder is always a guess. If real items vary between 400px and 900px, the scrollbar still jumps as each one renders. The auto keyword helps.
.feed-item {
content-visibility: auto;
contain-intrinsic-size: auto 600px;
}This means: use the last remembered rendered size if there is one. Otherwise use 600px. After the element has been rendered once, the browser remembers its real size. When the user scrolls away and the element is skipped again, it keeps that real size instead of falling back to 600px.
The first pass through the page still uses your guess. Later passes use measured values. This is why the guess only needs to be a reasonable average, not an exact value.
You can use auto per axis. This is useful when only the height varies:
.feed-item {
content-visibility: auto;
contain-intrinsic-width: auto 100%;
contain-intrinsic-height: auto 600px;
}Here the width falls back to 100% of the containing block. Many layouts do not need the width placeholder at all, because a block element already fills its container.
Worked example: a long article
Consider an article split into chunks. Each chunk is a section of roughly 800px.
<main>
<section class="chunk">...</section>
<section class="chunk">...</section>
<section class="chunk">...</section>
</main>
<style>
.chunk {
content-visibility: auto;
contain-intrinsic-size: auto 800px;
}
</style>Follow what happens as the user scrolls:
- On load, the browser renders chunks near the viewport. The others are skipped and each counts as 800px tall.
- The scrollbar reflects the estimated total height: the number of chunks times 800px.
- A chunk scrolls near the viewport and renders. Its real height is 950px, so the page grows by 150px below it.
- The user scrolls back up. The chunk is skipped again, and the browser remembers 950px rather than 800px.
The following diagram shows the decision the browser makes for each element.
Choosing a placeholder value
Pick the value from measurement, not from a guess in your head.
- Open DevTools and select a few rendered items.
- Read their computed heights in the Elements panel.
- Use a value near the median. A slightly low estimate is usually safer than a high one, because a page that grows as you scroll feels more natural than one that shrinks.
If item heights depend on viewport width, a single pixel value may be wrong on phones. You can set different values in media queries:
.feed-item {
content-visibility: auto;
contain-intrinsic-size: auto 500px;
}
@media (min-width: 900px) {
.feed-item {
contain-intrinsic-size: auto 320px;
}
}Common errors
- Forgetting the placeholder. Without it, skipped elements are zero tall. Scrolling jumps and anchor links may land in the wrong place.
- Using
contain: sizewithout a size. The element collapses to zero in that axis unless you give it an explicit size or acontain-intrinsic-size. - Expecting the placeholder to affect rendered elements. It is ignored while the element is rendered and not size-contained.
- Relying on
autobefore first render. The remembered size exists only after the element has rendered at least once, so the first scroll still depends on your estimate. - Expecting a remembered size to survive everything. The browser keeps it for the element's lifetime in the page. A reload or a re-created element starts over.
Summary of the key rules
contain-intrinsic-sizesets the size used in place of the content size when size containment is active.- The
autokeyword remembers the last rendered size and uses your length only as the fallback. - Set it whenever you use
content-visibility: autoorhidden, unless the element has an explicit size.
Exercise
A page has 100 .card elements with content-visibility: auto and contain-intrinsic-size: 400px. Cards are really 500px tall. A user scrolls down so that card 10 renders, then scrolls back up so that it is skipped again. What height does card 10 have while skipped, and what one-word change to the value would make it match its real height?
Show answer
It is 400px tall, because the plain length is used every time the card is skipped. Writing contain-intrinsic-size: auto 400px makes the browser remember the real 500px height after the first render and reuse it when the card is skipped again.
Exercise
You set .panel { contain: size; } with no width, height, or intrinsic size, and the panel contains three paragraphs. How tall does the panel appear, and give two ways to fix it.
Show answer
It collapses to zero height, because size containment ignores the content size. Fix it by giving the panel an explicit height, or by setting contain-intrinsic-block-size (or contain-intrinsic-size) to a placeholder length.