CSS position

Quick reference for CSS position: static, relative, absolute, sticky, and fixed, including sticky inset and overflow, and why absolute is not always relative to the parent.

position and the inset properties (top, right, bottom, left, and the inset shorthand) determine whether a box stays in normal flow and which containing block inset offsets are measured against. The initial position is static. The CSS tutorial does not have a dedicated positioning chapter, so this page is the reference for that topic, alongside the layout lessons.

Two boundaries are easy to miss. position: sticky does nothing useful until at least one inset side is not auto, and an ancestor with overflow hidden, auto, or scroll can become the scrollport that sticky is confined to. position: absolute is not always relative to the parent element: the containing block is the nearest ancestor that is not statically positioned, or that otherwise establishes a containing block, for example with transform.

Examples were parsed for syntax and structure. Sticky sticking, overflow clipping, and containing-block offsets were not measured in a browser here.

Official sources: MDN and the CSS specification. Fragments were checked with PostCSS 8 for syntax and structure. That is not a CSS conformance suite, and this is not a browser render or visual test.

CSS reference catalog → · CSS tutorial overview →

Position values and inset

Inset properties apply when position is not static. For relative, the box stays in flow and the inset offsets it visually from itself. For absolute and fixed, the box is taken out of flow and inset is resolved against its containing block. For sticky, the box stays in flow and uses inset as a stick threshold inside its scrollport.

Syntax or propertyInitial / defaultNotes
positionstaticstatic, relative, absolute, sticky, or fixed.
staticstaticThe default. The box is in normal flow. top, right, bottom, left, and inset have no effect, and z-index is not applied in the usual positioned way.
relative—The box stays in flow. Inset offsets it from where it would have been. It can also become a containing block for absolute descendants.
absolute—The box is out of flow. Inset is resolved against the containing block, which is not necessarily the parent. See the containing-block section.
fixed—Typically positioned against the viewport and stays put when the page scrolls. An ancestor that establishes a containing block (for example with a transform) can fix it against that ancestor instead.
sticky—A hybrid of relative and a stick constraint. It needs a non-auto inset and is limited by its nearest scrollport. See the next section.
top, right, bottom, left / insetautoOffsets or stick thresholds. If all sides stay auto, a sticky box has no threshold and an absolute box uses its static position.
z-indexautoOnly positioned boxes (and some flex/grid items) use z-index to paint in a stacking context. auto does not create a new stacking context by itself.

sticky: inset, containing block, and overflow

A sticky box is laid out as if it were relatively positioned, then constrained so that it stays inside its containing block while honouring the inset as a distance from the edges of the nearest scrollport. The scrollport is often the viewport, but a nearer ancestor that can scroll (overflow: auto or scroll, and in practice overflow: hidden as a scroll container) becomes the scrollport instead.

That is why a sticky header can appear to “fail” inside a widget with overflow: hidden or overflow: auto: it is sticking inside that box, not inside the page. overflow: visible on the ancestors is the usual case when you want the viewport to be the scrollport.

At least one of top, right, bottom, or left must be something other than auto. position: sticky; alone, with all insets auto, does not create a stick threshold.

A sticky heading with a top inset

position: sticky;
top: 0;

Sticky needs a non-auto inset. This heading uses top: 0 and does not place overflow on a wrapping ancestor.

  • No overflow other than the initial visible is set on .page, so the intended scrollport is the viewport. This was not scrolled in a browser here.
html
<div class="page">
  <h2 class="stuck">Weekly tasks</h2>
  <p>The list continues below the heading.</p>
  <p>More content so the page can scroll in a real viewport.</p>
</div>
<style>
.page { overflow: visible; }
.stuck { position: sticky; top: 0; background: #F3EFE4; }
</style>

What this markup means

.stuck is sticky with top: 0, so the stick threshold is the top of the scrollport. .page leaves overflow at visible, which avoids introducing a nearer scroll container. If .page used overflow: hidden or auto, the heading would be constrained to that box instead.

Related: Box model

MDN documentation → · CSS specification →

absolute is not always the parent

For an absolutely positioned box, the containing block is the padding box of the nearest ancestor that has a position other than static, or of the nearest ancestor that otherwise establishes a containing block. CSS Transforms are the usual surprise: a parent (or grandparent) with transform other than none, and similarly filter, perspective, will-change, or contain in some cases, can become the containing block even when its position is still static.

If no such ancestor exists, the containing block is the initial containing block. Do not write “absolute is relative to the parent” as a general rule. A position: relative parent is only one way to establish that containing block, and it has to be an ancestor, not necessarily the element’s immediate parent if a nearer positioned or transformed ancestor exists.

Absolute offsets follow the containing block, not always the parent

position: relative;
position: absolute;
right: 0; top: 0;

The badge is absolutely positioned against the ancestor that establishes a containing block. A static parent alone is not enough.

  • The inner wrap is position: static (the default) and has no transform. The card is position: relative, so it is the containing block in this tree.
html
<article class="card">
  <div class="wrap">
    <p>Garden planner</p>
    <span class="badge">New</span>
  </div>
</article>
<style>
.card { position: relative; padding: 16px; }
.wrap { position: static; }
.badge { position: absolute; top: 0; right: 0; }
</style>

What this markup means

The badge’s parent is .wrap, which is statically positioned and does not establish a containing block. The offsets top: 0 and right: 0 are therefore resolved against .card, the nearest relative ancestor, not against .wrap. A transform on .wrap would have changed that, even without changing .wrap’s position.

Related: Display

MDN documentation → · CSS specification →