← Back to HTML & CSS, page 4

html-css-0077 · Hard senior

What does the contain property do, and how can it help performance and layout?

contain tells the browser that an element's layout, paint, size or style effects stay inside it, so the browser can limit recalculation. layout and paint containment change behaviour (formatting context, clipping), and size containment requires a set size. It should be used where measured.

Full answer

contain is a promise to the browser: "what happens inside this element does not affect the outside in this way". With the promise, the browser can skip work. When something inside changes, it can limit recalculation to that box and not the whole page.

The values correspond to kinds of containment:

  • layout: the inside layout does not affect the outside, and outside layout does not reach the inside. As a side effect, the element becomes an independent formatting context. Floats stay inside it and its margins do not collapse with children.
  • paint: nothing is drawn outside the box. Content is clipped to the padding box, like overflow: clip. Descendants that are positioned or fixed use this box as their containing block.
  • size: the element's size does not depend on its content. With no explicit size, it collapses to zero in both directions.
  • inline-size: the same, only for the inline direction. This is what container queries use.
  • style: counters and some properties do not leak out.
  • content is shorthand for layout paint style. strict is size layout paint style.

Uses: a long list of independent cards, a widget that updates often (a ticker or a chart), or third-party embeds. A card with contain: content means a change in one card does not force the browser to re-check the others.

The cost is behaviour changes. Paint containment clips content that overflows, such as a dropdown, a tooltip or a shadow. Size containment hides content from the size, so a box with no set size becomes empty.

The first box has contain: size and no height, so it ignores its 80px child and measures 0 high plus borders. The second box clips the red square at its own edge, so a click on the part below the box misses it. In the third box the float is contained, so the box is 70px high plus borders instead of collapsing.

Apply contain only where you measured a problem. In DevTools, look at style recalculation and layout times before and after.

html
<style>
  body { margin: 0; font-family: sans-serif; }
  .case { margin: 8px; border: 1px solid #999; }
  .size { contain: size; }
  .size p { margin: 0; height: 80px; background: #cde; }
  .paint { contain: paint; position: relative; width: 120px; height: 60px; background: #eee; }
  .paint .abs { position: absolute; top: 40px; left: 0; width: 80px; height: 80px; background: tomato; }
  .layout { contain: layout; }
  .layout .float { float: left; width: 50px; height: 70px; background: #9c9; }
</style>
<div class="case size" id="size"><p>Content with its own height</p></div>
<div class="case paint" id="paint"><div class="abs" id="abs"></div></div>
<div class="case layout" id="layout"><div class="float"></div></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • contain is a promise that limits how much of the page needs recalculation.
  • layout makes an independent formatting context; paint clips to the box.
  • size and inline-size stop the size from depending on content.
  • content is layout paint style; strict adds size.
  • Side effects include clipping and zero size, so measure first.

Common mistakes

  • Using contain: size without giving the element a size, which collapses it to zero.
  • Using contain: paint on a component with dropdowns or tooltips that must overflow, so they get clipped.
  • Adding contain: strict everywhere as a habit, without measuring if it helps.

Follow-ups

  • Which containment type do container queries apply behind the scenes?
  • Why can contain: paint cut off a dropdown?

Tips

  • Define it as a promise to the browser, then name the layout, paint and size types.
  • Link it to container queries, which rely on inline-size containment.