Skip to content
Tutorials Center
My learning

Topics

Lesson 59 of 139 ยท Layout Fundamentals

Positioning

Master CSS positioning with static, relative, absolute, fixed and sticky, the inset properties, and containing blocks that decide where a positioned box lands.

In normal flow, boxes stack one after another: blocks top to bottom, inline content left to right. That covers most of a page. But some things need to break out of the line: a badge pinned to the corner of a card, a dropdown that floats over the content below it, a header that stays visible while you scroll, a "back to top" button glued to the corner of the screen.

The position property handles all of these. It has five values, and each one answers two questions differently: does the element still take up its space in the flow, and what is it positioned relative to? Once you can answer both for each value, positioning stops feeling like guesswork.

The five values at a glance

Value Keeps its space in the flow? Offsets are measured from
static Yes Offsets do not apply
relative Yes Its own normal position
absolute No The nearest positioned ancestor (its containing block)
fixed No The viewport (the browser window)
sticky Yes Its normal position, until it reaches an edge of its scroll container

The offsets are four properties: top, right, bottom and left. The inset shorthand sets all four at once, in the same top-right-bottom-left order as margin:

CSS
.overlay {
  position: absolute;
  inset: 0;              /* top: 0; right: 0; bottom: 0; left: 0 */
}
.corner {
  position: absolute;
  inset: 8px 8px auto auto; /* top and right only */
}

Logical versions such as inset-inline-start and inset-block-end follow the writing direction instead of the physical sides. See Writing Modes and Logical Properties.

static: the default

Every element starts as position: static. It sits in normal flow, and top, left, z-index and the other offsets have no effect. You rarely write static yourself, except to reset an element that was positioned elsewhere, for example inside a media query.

relative: nudge, and become an anchor

position: relative keeps the element in the flow and reserves its original space. Offsets then shift the element visually from that spot, without moving anything around it. top: 10px moves it down by 10 pixels, left: 20px moves it right by 20 pixels.

Nudging is occasionally handy (an icon that sits a pixel too high), but the main reason to write position: relative is its side effect: a positioned element becomes the containing block for absolutely positioned descendants. That leads straight to the next value.

absolute: placed inside an ancestor

An element with position: absolute is taken out of the flow. Its siblings behave as if it were not there, and it no longer stretches to fill its parent; it shrinks to fit its content. Its offsets are measured from the padding edge of its containing block: the nearest ancestor whose position is anything other than static. If there is no such ancestor, the containing block is the initial one, roughly the first screen of the page.

That is why you will see this pair everywhere:

Badges and overlays with absolute positioning

Delete position: relative from .card and reload the preview. The badge and caption jump to the corners of the whole preview, because their containing block is now the page. That is the most common positioning bug.

Notice also that inset: auto 0 0 0 gives the caption both left: 0 and right: 0. When you set both sides, the element stretches between them. inset: 0 therefore makes a box cover its containing block exactly, which is perfect for overlays.

Not only position creates containing blocks. For absolutely positioned elements, any positioned ancestor counts. For fixed elements, an ancestor with transform, filter, perspective, contain: paint or a few other properties becomes the containing block instead of the viewport. That is why a fixed element inside a transformed parent suddenly scrolls with it.

fixed: pinned to the viewport

position: fixed also takes the element out of the flow, but measures its offsets from the viewport. The element stays in the same place on screen while the page scrolls underneath it. Classic uses are cookie banners, chat buttons and toolbars.

CSS
.to-top {
  position: fixed;
  right: 16px;
  bottom: 16px;
}

Two cautions. First, fixed elements cover content, so leave room for them, for example with padding-bottom on the page or scroll-padding-top for a fixed header so that links to #sections do not land underneath it. Second, on small screens a fixed bar steals precious height. A sticky header is often kinder.

sticky: in the flow until it sticks

position: sticky is a hybrid. The element sits in normal flow and keeps its space, like relative. But once scrolling would carry it past the threshold you set, such as top: 0, it sticks at that edge of its scroll container and stays visible. It lets go again when the end of its parent element scrolls past, because a sticky element can never leave its parent.

Here is a sticky header inside a scrollable box. Scroll the list:

Sticky headers inside a scroll container

The app bar is a child of .scroller itself, so it sticks for the whole list. Each letter heading is inside its own <section>, so it sticks only while its section is visible, then gets pushed away by the next one. That push-off comes for free from the "never leave the parent" rule.

When sticky "does nothing," check these four things:

  1. You set a threshold. position: sticky without top, bottom, left or right never sticks.
  2. The parent is taller than the sticky element. If the parent is exactly as tall as its sticky child, there is no room to move. A common culprit is a flex or grid parent that stretches items.
  3. No ancestor between it and the intended scroller has overflow: hidden, auto or scroll. Any of those turns that ancestor into the scroll container, even if it never actually scrolls. Use overflow: clip if you only need to clip.
  4. The scroll container really scrolls. In the example, .scroller has a fixed height and overflow-y: auto. Without a height there is nothing to scroll, and the page becomes the scroll container instead.

Sticky table headers work the same way: put position: sticky; top: 0 on the <th> elements.

Stacking positioned elements

Positioned elements (anything except static) are painted above non-positioned ones, and later ones above earlier ones. To change that order, use z-index, as the sticky app bar does above. z-index comes with its own set of rules, called stacking contexts, which explain most "my z-index of 9999 doesn't work" problems. They are the topic of the next lesson.

Prefer flow layout when you can. Absolute positioning is perfect for small decorations on top of a component, but building whole layouts with it is brittle: positioned boxes do not push each other around, so longer text or a narrower screen leads to overlaps. Use flexbox and grid for structure, and position only the extras. Centering is another job with better tools; see Centering Anything.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. An absolutely positioned badge jumps to the corner of the page instead of its card. What is the most likely fix?

    Absolute elements are placed relative to their nearest positioned ancestor. Without one, they use the initial containing block.
  2. 2. Which position values keep the element's original space in the flow?

    Relative and sticky elements still occupy their normal place; absolute and fixed are removed from the flow.
  3. 3. What does inset: 0 do on an absolutely positioned element?

    Setting all four offsets to 0 pins every edge to the containing block, so the element covers it exactly.
  4. 4. A position: sticky; top: 0 header never sticks. Which of these could cause it?

    An ancestor with overflow: hidden, auto or scroll becomes the scroll container, so the header sticks inside it instead, often with no visible effect.
  5. 5. When does a sticky element stop sticking?

    A sticky element can never leave its parent, so it is carried away once the parent's end reaches it.

Summary

  • static is the default; offsets do not apply.
  • relative nudges an element from its normal spot and makes it a containing block for absolute children.
  • absolute leaves the flow and is placed against its nearest positioned ancestor; inset: 0 covers it.
  • fixed leaves the flow and is placed against the viewport, unless a transformed or filtered ancestor captures it.
  • sticky stays in the flow and sticks at a threshold within its scroll container, but never leaves its parent. It needs an offset and an ancestor chain without stray overflow values.

Next: z-index and Stacking Contexts โ€” control which element appears on top, and understand why z-index sometimes seems to be ignored.