Positioning and Sticky

Intermediate · 28 min

The five position values, how an absolutely positioned box finds its containing block, and why position sticky stops working when an ancestor has overflow set.

Before thisMargin Collapsing and Formatting Contexts

Kevin Powell · 28 min

Positioning and Sticky

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `position: relative` keeps the element's space in the flow and shifts only its painted position. Its main job is to become the reference box for absolute children.
  • `position: absolute` removes the element from the flow and places it against its containing block, which is the nearest ancestor that is positioned (or has a `transform`, `filter` and similar), not simply the parent.
  • `position: fixed` is placed against the viewport, unless an ancestor has a `transform`, in which case that ancestor becomes the reference.
  • `position: sticky` behaves as relative until the element would cross an offset such as `top: 0` inside its scroll container, then it sticks. It can never leave its parent's box.
  • Sticky stops working if an ancestor between it and the scrolling viewport has `overflow: hidden`, `auto` or `scroll`. `overflow: clip` does not break it.

Leaving the flow on purpose

Normal flow puts every box where the content before it leaves room. That is right until you want something to sit on top of another box (a badge, a tooltip), stay on screen while the page scrolls (a header), or ignore its neighbours. For those, you take a box out of the flow, or partly out, with position.

The five values

static is the default: normal flow, and the offset properties top, right, bottom, left (and the logical inset) are ignored.

relative keeps the element exactly where flow would put it, with its space reserved, and then paints it shifted by any offsets. Moving things with relative is rare. What matters is its side effect: a relative box becomes a reference for absolute children, as below.

absolute removes the element from the flow, so siblings close the gap as if it did not exist. Offsets are measured from its containing block.

fixed is positioned against the viewport and stays put as the page scrolls.

sticky is a hybrid, covered in its own section.

The containing block

The common wrong answer to “what is an absolute box positioned against?” is “its parent”. It is the nearest ancestor that is positioned, meaning its position is anything but static. If none exists, it is positioned against the initial containing block, which is roughly the first screenful at the top of the page.

.card  { position: relative; }   /* the reference */
.badge { position: absolute; top: 0; right: 0; }

Without position: relative on the card, that badge flies to the corner of the page. Two other properties also create a containing block for absolute and fixed descendants: transform (any value other than none) and filter, and a few more such as perspective. I tested a position: fixed box inside a parent with transform: translateX(0) that sat 100px down the page: the fixed box measured 100px from the viewport top, not 0, because the transformed ancestor was its reference. Without the transform it measured 0. When a “fixed” modal drifts off, look for a transform on an ancestor.

An absolute box with inset: 0 fills its containing block, which is a quick way to build overlays. Its width without any dimensions is shrink-to-fit around its content, not the full container width as a block would be.

Sticky

position: sticky flips between two states. It starts as relative, scrolling with the page. When the box would scroll past the offset you gave it, it holds there:

.table-header {
  position: sticky;
  top: 0;
}

Three rules explain every sticky bug.

It sticks relative to its nearest scrolling ancestor. That is the viewport unless some ancestor has overflow set to hidden, auto or scroll. Those make the ancestor the scroll container, and if that ancestor does not itself scroll, sticky has nothing to react to. I tested a header inside an overflow: hidden wrapper: as the page scrolled 300px, it scrolled away (top at −300). With overflow: clip on the wrapper, it stayed at 0, because clip does not create a scroll container. Details are in overflow and scroll containers.

It needs an offset. top: 0, or bottom, left, right. Without one, sticky has no threshold and does nothing.

It cannot leave its parent. A sticky box only moves within its parent’s content box. When the parent’s bottom edge arrives, the box is pushed up with it. In a 200px-tall parent followed by 2000px of other content, I scrolled to 1000px and the header sat at −820, scrolled out of view along with its parent. To keep a sidebar pinned for a long page, its parent has to be as tall as the page.

Sticky is often better than fixed for headers, since it keeps its space in the flow and avoids content hidden underneath. The runner-up video makes that case.

z-index comes next

Positioned boxes can overlap. Which one paints on top is its own topic: stacking contexts and z-index. Popovers and tooltips attached to other elements get a better mechanism in anchor positioning and popovers.

What the video covers

Kevin Powell’s “CSS position deep dive” is a 28-minute video from November 2023. From its description he set out to look at all the position values with an emphasis on how containing blocks work and the gotchas, using a more realistic page than random floating boxes. I could not read a transcript or the full chapter list, so the sticky rules and the numbers above are my own tests in Chromium.

Where this fits

Positioning is the first tool here that removes elements from flow, and it connects to stacking contexts and later to transforms, which share the containing-block effect you just saw. For layout of a whole page, use Flexbox and Grid rather than absolute positioning.

Check yourself

  1. 01You put `position: absolute; top: 0; left: 0` on a badge inside a card, and the badge lands in the corner of the page instead of the card. What is missing?

    The card needs to be a containing block, usually with `position: relative`. An absolute box measures from the nearest positioned ancestor, and with none it falls back to the initial containing block, the viewport-sized box at the top of the page.

  2. 02A `position: fixed` footer is offset 100px from the top of the screen even though you wrote `top: 0`. What could cause that?

    An ancestor has a `transform` (or `filter`, `perspective` and so on), which makes it the containing block for fixed descendants. In my test the fixed box measured 100px from the viewport top because its transformed parent started 100px down.

  3. 03Your `position: sticky; top: 0` header never sticks. What are the two most common causes?

    An ancestor has `overflow: hidden`, `auto` or `scroll`, making that ancestor the scroll container the header sticks within, and it does not scroll; or the sticky element's parent is only as tall as the element itself, leaving no room to stick. Using `overflow: clip` on the ancestor fixes the first without breaking sticky.

  4. 04When does a sticky element stop sticking at the bottom of the page?

    When its parent's bottom edge arrives. A sticky box is confined to its parent, so it scrolls away with the end of that container. I measured this in Chromium: past the end of a 200px parent the header scrolled out of view instead of staying pinned.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark