Sub topic 11
Reserving space with contain-intrinsic-size
This chapter assumes you know the box model, how contain: size works, and the basics of content-visibility: auto. The sections below cover how to give a skipped element a size and which mistakes to avoid.
Why a skipped element needs a size
Size containment makes an element's size independent of its contents. The browser lays the element out as if it were empty. content-visibility: auto applies size containment while the element is off-screen. If the element has no explicit height, it collapses to zero height.
Many collapsed sections cause a visible problem. The page is much shorter than it should be, so the scrollbar thumb is too large. As you scroll, sections render and grow, and the scrollbar jumps.
contain-intrinsic-size fixes this. It sets the size the browser uses for the element when size containment hides its contents.
Syntax
.feed-item {
content-visibility: auto;
contain-intrinsic-size: 600px; /* width and height both 600px */
}
.card {
content-visibility: auto;
contain-intrinsic-size: 300px 500px; /* width, then height */
}
.row {
content-visibility: auto;
contain-intrinsic-height: 120px; /* height only */
}One value sets both dimensions. Two values set width, then height. The longhands contain-intrinsic-width and contain-intrinsic-height set one axis. Use a longhand when the other axis comes from normal layout. For example, a block element's width usually fills its container, so only the height needs a placeholder.
The auto keyword
A fixed estimate is rarely exact. Adding auto tells the browser to remember the real size:
.feed-item {
content-visibility: auto;
contain-intrinsic-size: auto 600px;
}Before the element has ever rendered, the browser uses 600px. After it has rendered, the browser records its actual size. When the element is skipped again, it keeps that recorded size and ignores the placeholder. Scrolling back over content you have already seen therefore causes no jump.
Common errors
- Omitting the property. Skipped sections collapse to zero height and the scrollbar jumps.
- Choosing a poor estimate. The placeholder is used until the first render, so a value far from the real size still causes jumps on a first pass. Pick a typical height, such as the median of your sections.
- Expecting it to affect rendered content. The intrinsic size applies only while size containment is in effect. Once the element renders under
content-visibility: auto, its real contents determine its size. - Using
autoand expecting a measurement at load. The browser has nothing to remember until the element has rendered once. - Setting a fixed
heightinstead. An explicitheightclips or pads the content after it renders. The intrinsic size is only a placeholder.
Exercise
A page uses .chunk { content-visibility: auto; } with no other sizing rules. Users report that the scrollbar jumps while they scroll down. What one declaration would you add, and why would it help?
Show answer
Add contain-intrinsic-size: auto 800px;, using a height that is typical for your sections. Off-screen chunks are laid out as empty because of size containment, so they collapse to zero height. The placeholder reserves space and keeps the scrollbar length close to correct. The auto keyword also makes the browser remember each chunk's real height after it has rendered.