Sub topic 8
The strict and content shorthands
This chapter assumes you know the four basic containment values from earlier chapters: layout, paint, size, and style. It also assumes you are comfortable with the CSS box model and with overflow. Here you will learn the two shorthand keywords that bundle those values, and how to choose between them.
What the shorthands expand to
Writing several values for every container gets repetitive. The contain property therefore offers two keywords that stand for fixed sets of values.
| Shorthand | Expands to |
|---|---|
| `contain: strict` | `size layout paint style` |
| `contain: content` | `layout paint style` |
The only difference is size. strict includes size containment, and content leaves it out.
.panel-strict {
contain: strict;
width: 300px;
height: 200px;
}
.panel-content {
contain: content;
}Why the size difference matters
Size containment makes the element's size independent of its contents. The browser does not look at the children to work out how big the box is. With no explicit size, the element collapses to zero in the contained dimension.
For that reason, contain: strict always needs a width and a height from you, or from a layout that sizes the element, such as a grid track or a flex stretch. contain: content lets the element grow to fit its children as usual, so it is safe on boxes whose height depends on their text.
Common errors
- A collapsed element. You add
contain: strictto a card whose height came from its text, and the card disappears. Fix it by adding a height, or switch tocontain: content. - Clipped overflow. Both shorthands include paint containment, so descendants are not drawn outside the element's bounds. A dropdown, tooltip, or box shadow that extends past the edge gets cut off.
- Broken positioning. Layout containment makes the element a containing block for absolutely and fixed positioned descendants. A
position: fixedchild now positions relative to the contained element, not the viewport. - Using
strictby habit. It is the stronger guarantee, but it is only correct when the size really is independent of the content.
Picking one
Use contain: content as your default for repeated components such as list items, comments, or cards. It gives the browser the layout, paint, and style isolation it needs, and it does not constrain sizing. Reach for contain: strict when the box has a fixed or externally imposed size, such as a fixed-height scroll region or a tile in a grid with set tracks.
You can also write the individual values yourself when you want something in between, for example contain: layout paint. The shorthands are only a convenience.
Exercise
A comment box sizes itself to its text. You want layout, paint, and style containment without fixing its height. Which shorthand should you use, and why would the other one be a problem?
Show answer
Use contain: content. It expands to layout paint style. contain: strict also adds size, so the box would no longer size itself from its text and would collapse to zero height unless you gave it an explicit height.
Exercise
A tooltip inside a contain: content card appears cut off at the card's edge. Which part of the shorthand causes this, and what are two ways to fix it?
Show answer
Paint containment causes it, because descendants are not drawn outside the element's bounds. You can move the tooltip outside the contained element, or drop paint by writing contain: layout style on the card instead of the shorthand.