Sub topic 5
Containing blocks and positioned ancestors
This chapter assumes you know basic HTML and CSS selectors, the box model (content, padding, border, margin), and units such as px, %, and rem. It also assumes you have met position, the offset properties (top, right, bottom, left, inset), and position: relative.
Every offset and every percentage on a positioned element is measured against one specific box, called the containing block. Most "my element ended up in the wrong place" bugs come from misidentifying that box.
What a containing block is
The containing block is the rectangle an element uses as its frame of reference. It affects three things:
- Percentage widths and heights resolve against its size.
- Percentage margins and padding resolve against its width. This applies to all four sides, including top and bottom.
- For positioned elements,
top,right,bottom,left, andinsetmeasure the distance from its edges.
A containing block is not always an element's parent. Which box it is depends on the element's position value.
| `position` value | Containing block |
|---|---|
| `static` or `relative` | The content box of the nearest block container ancestor |
| `absolute` | The padding box of the nearest ancestor that is a containing block for absolute descendants (see below) |
| `fixed` | The viewport, unless an ancestor changes that (see below) |
| `sticky` | The nearest block container ancestor, plus the scrollport used for the sticking behavior |
The padding box is the content plus the padding, and it excludes the border. This is a detail that surprises people. An absolutely positioned child with top: 0; left: 0 sits just inside the parent's border, not on top of it.
The initial containing block
If no ancestor qualifies, the browser falls back to the initial containing block. It is a rectangle with the dimensions of the viewport, anchored at the top-left of the page canvas, and it is the containing block of the root <html> element.
An absolutely positioned element with no positioned ancestor is placed against this rectangle. It scrolls away with the page, because it is part of the document, not the viewport. A fixed element is also measured against the viewport size, but it stays on screen when you scroll.
Finding the containing block of an absolute element
The browser walks up the ancestor chain from the element's parent. It stops at the first ancestor that meets any one of these conditions:
- Its
positionis anything other thanstatic(relative,absolute,fixed, orsticky). - Its
transform,translate,scale,rotate, orperspectiveis anything other thannone. - Its
filterorbackdrop-filteris anything other thannone. - Its
containproperty includeslayoutorpaint. - Its
will-changenames one of the properties above.
If it reaches the top without a match, it uses the initial containing block.
The word "positioned ancestor" in the chapter title usually means the first condition. The other conditions matter because they quietly create containing blocks. A transform on a wrapper can change the layout of everything inside it, even though transform is not a positioning property.
Example: anchoring a badge
The usual way to anchor an absolute child to its parent is to give the parent position: relative and no offsets. The parent stays in normal flow and becomes the reference box.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Containing block demo</title>
<style>
.card {
position: relative;
width: 240px;
padding: 1rem;
border: 4px solid #333;
margin: 3rem;
}
.badge {
position: absolute;
top: 0;
right: 0;
background: tomato;
color: white;
padding: 0.25rem 0.5rem;
}
</style>
</head>
<body>
<div class="card">
<span class="badge">New</span>
<p>Card content</p>
</div>
</body>
</html>The badge's top-right corner touches the inner edge of the card's 4px border, because the containing block is the padding box. To place the badge so it overlaps the border, use negative offsets such as top: -4px; right: -4px. To push it halfway out of the card, use top: 0; right: 0; transform: translate(50%, -50%);. Percentages inside translate() refer to the badge's own size, not the card's.
Now remove position: relative from .card. The card becomes static, so the browser keeps walking up. Nothing above it is positioned, so the badge moves to the top-right corner of the initial containing block. It sits at the top-right of the page, far from the card, and the card closes up the gap it left, because the badge no longer takes up space in the flow.
Percentages and the padding box
Because an absolute element's containing block is a padding box, percentage values follow the padding box, not the content box.
.panel {
position: relative;
width: 400px;
height: 200px;
padding: 20px;
}
.panel .overlay {
position: absolute;
top: 0;
left: 0;
width: 50%; /* 50% of the panel's padding box: 440px, not 400px */
height: 100%; /* 100% of the padding box height: 240px */
}With box-sizing: content-box, the panel's padding box is 440px by 240px, so the overlay is 220px by 240px. With box-sizing: border-box, the declared width and height include the padding, so the padding box is slightly smaller than the declared size. This is also why inset: 0 reliably fills the padding box without you doing any arithmetic. It sets all four offsets to 0, and the browser works out the size.
Percent offsets work the same way. left: 50% means half of the containing block's width. top: 50% means half of its height. This is the basis of the centering technique in the modal pattern: move the top-left corner to the middle, then pull the element back by half of its own size.
Fixed elements and the viewport exception
For position: fixed, the containing block is normally the viewport, which is why a fixed element ignores scrolling. The exception catches many developers: if any ancestor has a transform, perspective, filter, or contain: layout or paint, that ancestor becomes the containing block instead. The fixed element then scrolls and clips with that ancestor.
<div class="page-wrapper">
<div class="toolbar">Toolbar</div>
</div>
<style>
.page-wrapper {
transform: translateZ(0); /* creates a containing block for fixed descendants */
}
.toolbar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
}
</style>Here the toolbar is attached to the bottom of .page-wrapper, not the bottom of the screen. A common cause of this bug is a CSS rule added for animation or GPU hints (transform: translateZ(0), will-change: transform) on a layout wrapper. The fix is to remove that property from the ancestor, or to move the fixed element so it is not inside that ancestor. In a framework, rendering modals into a node placed directly under <body> avoids the problem.
Clipping and the containing block
Containing blocks interact with overflow in a way that is easy to get wrong. An overflow value other than visible clips descendants only if the clipping element is in the containing block chain of those descendants.
- If an absolute element's containing block is inside the
overflow: hiddenelement (or is that element), the absolute element is clipped. - If an
overflow: hiddenelement sits between the absolute element and its containing block, but is not positioned itself, the absolute element is not clipped by it. It escapes.
<div class="outer">
<div class="clip">
<div class="child">I am not clipped</div>
</div>
</div>
<style>
.outer { position: relative; }
.clip { overflow: hidden; height: 50px; }
.child { position: absolute; top: 100px; left: 0; }
</style>The .child containing block is .outer. .clip is static, so its clipping does not apply. Add position: relative to .clip and the child is now clipped, because .clip has become its containing block.
Common errors
- Forgetting the anchor. You add
position: absoluteto a child and it jumps to the page corner. Addposition: relative(without offsets) to the intended parent. - Anchoring to the wrong ancestor. An intermediate wrapper has
position: relativefor an unrelated reason, so the child anchors there instead. Check each ancestor in the browser's computed styles. - Expecting the content box. Offsets are measured from the padding edge, so an element with
left: 0ignores the parent's padding. - Hidden containing blocks. A
transform,filter, orwill-changeon an ancestor captures fixed and absolute descendants. - Percentage heights collapsing.
height: 100%on an absolute element works against the containing block's height, which is fine. On a non-positioned child it needs the parent to have a definite height, so it may resolve toauto.
Debugging approach
- Open the browser's developer tools and select the misplaced element.
- Walk up the DOM tree one ancestor at a time. For each, check computed
position,transform,filter,perspective,contain, andwill-change. - The first ancestor that has any of these is the containing block. Compare its padding box to where the element landed.
- If the box is wrong, either change which ancestor holds the property, or add
position: relativeto the ancestor you want.
Exercise
A .tooltip with position: absolute; top: 0; left: 0; sits inside .wrapper (static) inside .card (position: relative; padding: 20px; border: 2px solid). The .card is inside <main style="transform: scale(1)">. Which element is the tooltip's containing block, and where is the tooltip drawn relative to the card?
Show answer
The containing block is .card, because it is the nearest ancestor with a non-static position. The transform on <main> is further up and never reached. The tooltip is drawn at the top-left corner of the card's padding box. That is just inside the 2px border and ignores the 20px of padding, so the tooltip touches the border edge.
Exercise
A fixed footer works on most pages, but on one page it scrolls with the content. The only difference is that this page wraps its body content in a div with filter: drop-shadow(0 0 4px #000). Explain why, and name two fixes.
Show answer
A non-none filter on an ancestor makes that ancestor the containing block for fixed descendants. The footer is therefore positioned relative to the wrapper and scrolls with it. Fixes: remove the filter from the wrapper (apply it to a smaller element that does not contain the footer), or move the footer out of the filtered wrapper so it is a direct child of <body>.