Containing block
The size and position of an element are often impacted by its containing block. Most often, the containing block is the content area (opens in a new tab) of an element's nearest block-level (opens in a new tab) ancestor, but this is not always the case.
Identifying the containing block
The process for identifying the containing block depends entirely on the value of the element's position
property:
- If the
position
property isstatic
,relative
, orsticky
, the containing block is formed by the edge of the content box of the nearest ancestor element that is either a block container (such as an inline-block, block, or list-item element) or establishes a formatting context (such as a table container, flex container, grid container, or the block container itself). - If the
position
property isabsolute
, the containing block is formed by the edge of the padding box of the nearest ancestor element that has aposition
value other thanstatic
(fixed
,absolute
,relative
, orsticky
). - If the
position
property isfixed
, the containing block is established by the viewport (opens in a new tab) (in the case of continuous media) or the page area (in the case of paged media). - If the
position
property isabsolute
orfixed
, the containing block may also be formed by the edge of the padding box of the nearest ancestor element that has the following:- A
transform
orperspective
value other thannone
- A
will-change
value oftransform
orperspective
- A
filter
value other thannone
or awill-change
value offilter
(only works on Firefox). - A
contain
value ofpaint
(e.g.contain: paint;
) - A
backdrop-filter
other thannone
(e.g.backdrop-filter: blur(10px);
)
- A
Note: The containing block in which the root element (
<html>
) resides is a rectangle called the initial containing block. It has the dimensions of the viewport (for continuous media) or the page area (for paged media).
Positioning contexts
If no ancestor elements have their position property explicitly defined, then by default all ancestor elements will have a static position. The result of this is the absolutely positioned element will be contained in the initial containing block. The initial containing block has the dimensions of the viewport and is also the block that contains the <html>
element. In other words, the absolutely positioned element will be displayed outside of the <html>
element and be positioned relative to the initial viewport.
See also
- Positioning - Learn web development | MDN (opens in a new tab)
- position - CSS: Cascading Style Sheets | MDN (opens in a new tab)