Sub topic 5
Containing Blocks and Positioned Ancestors
This chapter assumes you know basic HTML and CSS selectors, the box model, and units such as px, %, and rem. It also assumes you know what position: relative does and that offsets such as top and left move a positioned box.
What a containing block is
A containing block is the rectangle an element uses as its reference. Percentage widths, heights, and offsets are calculated against it. For an absolutely positioned element, top, right, bottom, and left measure from the edges of this rectangle.
Every element has a containing block. Which element supplies it depends on the element's position value.
| Position value | Containing block |
|---|---|
| `static` or `relative` | The content box of the nearest block-level ancestor |
| `absolute` | The padding box of the nearest positioned ancestor |
| `fixed` | The viewport, unless an ancestor changes this (see the pitfalls below) |
The padding box is the content plus the padding, without the border. An absolutely positioned child with top: 0; left: 0 therefore sits just inside its ancestor's border.
Finding the positioned ancestor
A positioned element is any element whose position is not static. Browsers find the containing block of an absolute element by walking up the tree from its parent:
- Check the parent. If its
positionisrelative,absolute,fixed, orsticky, it is the containing block. - If not, check the grandparent, and keep going up.
- If no ancestor is positioned, use the initial containing block, which has the size of the viewport and sits at the top of the page.
Worked example: a corner badge
The badge below is absolutely positioned. Its parent .card has position: relative, so the card is the containing block.
<div class="card">
<span class="badge">New</span>
<p>Card content</p>
</div>
<style>
.card {
position: relative;
padding: 1rem;
border: 1px solid #ccc;
}
.badge {
position: absolute;
top: 0;
right: 0;
}
</style>The badge lands in the card's top-right corner. It is also removed from the flow, so the paragraph does not leave a gap for it.
Remove position: relative from .card and run it again. No ancestor is positioned now, so the badge jumps to the top-right corner of the page. This is the most common containing block mistake.
How relative positioning fits in
A relatively positioned box is shifted from its normal place, but the space it occupied stays reserved.
.box {
position: relative;
top: 10px;
left: 20px;
}This box is drawn 10px lower and 20px further right. The surrounding content does not move. Relative positioning is useful for small nudges, and more often as an anchor for absolute children.
Percentages and the containing block
Offsets and sizes in percent resolve against the containing block. In an absolute element, top: 50% means half the height of the containing block's padding box, and width: 50% means half its width. Because the reference is the padding box, padding on the ancestor counts toward that size.
.card {
position: relative;
width: 300px;
height: 200px;
}
.card .overlay {
position: absolute;
top: 0;
left: 0;
width: 50%; /* 150px */
height: 100%; /* 200px */
}Common errors
- The element lands in the wrong corner. No ancestor is positioned, so the element used the page. Add
position: relativeto the intended parent. - The element is anchored to the wrong ancestor. A closer ancestor has a non-static position. Check each ancestor in the browser's developer tools.
- Offsets seem off by a few pixels. The containing block is the padding box, so the ancestor's border is outside the reference rectangle.
Exercise
A .panel contains a .close button with position: absolute; top: 8px; right: 8px;. The button appears in the top-right corner of the browser window instead of the panel. What is the cause, and what is the smallest fix?
Show answer
No ancestor of .close is positioned, so its containing block is the initial containing block (the viewport-sized area at the top of the page). Add position: relative to .panel. The panel then becomes the containing block, and the button is placed 8px from the panel's top and right padding edges.
Exercise
A .card has position: relative, a 4px border, and 20px padding. A child has position: absolute; top: 0; left: 0;. Does the child touch the outer edge of the border, or does it sit just inside the border?
Show answer
It sits just inside the border. The containing block for an absolute child is the ancestor's padding box, which excludes the border. The child therefore starts at the inner edge of the 4px border, and it overlaps the padding area rather than sitting inside it.