Skip to content

CSS Position and z-index: static, relative, absolute, fixed and sticky

The position property decides where an element’s top, right, bottom and left are measured from. In everyday CSS that comes down to three jobs: a badge in the corner of a card (a position: relative card with a position: absolute badge inside), something pinned to the screen (position: fixed), and a header that sticks while you scroll (position: sticky; top: 0). z-index then picks what sits on top when things overlap.

A badge on a card

The card is relative, so the badge’s top: -10px; right: -10px is measured from the card’s corner, not the page’s. Change -10px to 10px and the badge moves inside the card. The rest of this page goes through each value and the traps that come with it.

The five position values

ValueKeeps its space?top / left measured fromTypical use
staticYesIgnoredEverything, by default
relativeYesIts own normal spotSmall nudges, and a parent for absolute children
absoluteNoThe nearest positioned ancestorBadges, close buttons, overlays, dropdowns
fixedNoThe browser windowBack-to-top buttons, cookie bars, chat buttons
stickyYesThe scrolling area, inside its parentHeaders and table headings that stay in view

“Positioned” in CSS means any value except static. You’ll see that word a lot below, because absolute elements and z-index both care about it.

static, the default

Every element starts as position: static. It sits wherever the normal flow of the page puts it, one block under another, and it ignores top, left and z-index completely:

top and left on a static element

The note doesn’t budge. Add position: relative; to the rule and it jumps 40px down and right. You rarely write static yourself; it’s mostly useful to undo a position set somewhere else, for example inside a media query.

relative: nudge without moving out

A relative element moves from where it would normally be, but the page keeps its old space for it. Nothing else shifts to fill the gap:

Cappuccino moved 8px down, 40px right

Cappuccino slides over and overlaps Mocha a little, but Mocha stays exactly where it was. That’s the whole point of relative: a visual nudge that doesn’t push anything around. In practice it’s used far more often with no offsets at all, to make the element the anchor for absolute children, which is next.

absolute: a badge on a card

An absolute element leaves the normal flow (the content around it closes up as if it weren’t there) and is placed against its nearest positioned ancestor: the closest parent, grandparent and so on that has position set to anything but static. That’s why the badge at the top of this page works. Here’s the same card without position: relative:

The badge with no relative parent

With no positioned ancestor, the badge measures from the page itself, so it lands in the top-right corner of the box. On a real page it would sit at the top of the whole document and scroll away with it. Put position: relative; back on .card and it snaps to the card’s corner.

The containing block

The box that top, right, bottom and left are measured from has a name in the CSS specs: the containing block. For the badge it’s the card, or more exactly the inside edge of the card’s border. That’s why top: -10px puts the badge 10px above the inside of the border, which is 9px above the 1px border line itself. Which box it is depends on the value:

  • absolute: the nearest positioned ancestor, or a page-sized box at the top of the document when there’s none.
  • fixed: the browser window (except in the transform case below).
  • relative and sticky: the offsets move the element from its own normal spot instead, so the containing block matters less to them.

Percentages use it too. On an absolute element, top: 50% is half the containing block’s height and width: 50% half its width, which is what the centering trick relies on.

  • No offsets at all? An absolute element with no top/left stays where it would have been, but it no longer takes up space.
  • Both left and right? It stretches between them. left: 10px; right: 10px in a 300px parent makes it 280px wide.
  • A <span> turns into a block. Absolute elements are always block-level, so width and height start working on inline tags.
  • The parent can collapse. If every child is absolute, the parent has nothing in its flow and its height drops to 0. Give it a height or padding.

The tooltip tutorial uses this same relative-parent, absolute-child pair to place a tooltip above a word.

fixed: pinned to the screen

A fixed element is measured from the browser window and stays there when the page scrolls. In these examples the “window” is the little preview box, so scroll inside it:

A back-to-top button (scroll the box)

Like absolute, fixed takes the element out of the flow, so it can cover content. Leave some padding at the bottom of the page so the last line isn’t hidden under the button.

When fixed stops being fixed

A style on a parent, written far away from the fixed element, can change what it’s measured from. If any ancestor has a transform, that ancestor becomes the containing block instead of the window, and the fixed element scrolls along with it:

A transform breaks fixed (scroll the box)

The chat button sits inside the card and scrolls away. Delete transform: translateY(0); and it goes back to the corner of the box. Even a transform that moves nothing counts. In Chromium the same happens with filter, perspective, backdrop-filter, contain: paint and will-change: transform on an ancestor. Hover effects and animations often add one of those without you thinking about it. The fix is to move the fixed element out, so it’s a direct child of <body>.

sticky: sticks while you scroll

A sticky element acts like a normal one until you scroll it to its top value, then it holds there. It only sticks inside its parent, so when the parent scrolls away, the sticky element goes with it:

Menu headings that stick (scroll the box)

Scroll the box: Coffee sticks to the top, then Tea pushes it off as the Coffee section ends. Each heading lives in its own <section>, and that’s what limits it. If sticky won’t stick, check these three things:

  • A top value. position: sticky alone does nothing. Add top: 0 (or bottom, left, right for other directions).
  • The parent’s size. If the parent is only as tall as the sticky element, there’s no room to stick. A sticky <nav> inside a short <header> is the usual case; make the header sticky instead.
  • overflow on an ancestor. overflow: hidden, auto or scroll on any ancestor (even just overflow-x: hidden) turns that ancestor into the sticky element’s scrolling area, so it stops sticking to the page. overflow: clip doesn’t have this problem, so it can replace hidden when you only wanted to cut off overflowing content.

For a whole sticky navigation bar, see the navbar tutorial.

top, right, bottom, left and inset

The four offsets say how far the element sits from each edge of whatever it’s measured from. inset is a shorthand for all four, in the same order as margin (top, right, bottom, left). inset: 0 is the one you’ll type most: it stretches an absolute element over its whole parent, so it can lay a “Sold out” note over a cake:

A Sold out overlay with inset: 0

inset: 0 means top: 0; right: 0; bottom: 0; left: 0. Two values work like margin too: inset: 10px 20px is 10px from the top and bottom, 20px from the sides. Try it on the overlay.

z-index: what goes on top

When elements overlap, later ones in the HTML paint over earlier ones. z-index changes that: a higher number sits on top. Three overlapping photos, with the blue one raised:

Overlapping photos

Remove z-index: 1 from .two and green covers blue again, because it comes later. Give .one a z-index: 2 and red jumps to the front.

  • It only works on some elements: positioned ones (relative, absolute, fixed, sticky) and items inside a flex or grid container. On a plain static block, z-index does nothing. Delete position: relative from .photo to see it stop working.
  • Whole numbers only. Negative ones work too. z-index: 1.5 is invalid and gets thrown away.
  • Keep the numbers small. 1, 2, 10 for a header, 100 for a modal is plenty. Big numbers don’t help, as the next section shows.

Why z-index: 9999 doesn’t win

Here’s a dropdown that should be on top and isn’t. The yellow menu below has z-index: 9999, yet most of it is hidden behind the blue box, which only has z-index: 2:

A dropdown stuck under the hero box

The reason is the header’s z-index: 1. A positioned element with a z-index forms a stacking context: a sealed layer that its children can’t leave. The menu’s 9999 only counts inside the header. On the outside, the whole header (menu included) is a 1, and 1 is below the hero’s 2. Think of the 9999 as a score inside one team that never gets compared with the other team.

Two ways out: delete z-index: 1; from the header (then the menu is compared with the hero directly, and 9999 wins), or give the header a higher number than the hero, like z-index: 3. Try either one in the box above.

These also create a stacking context, even without a z-index. That’s why adding a fade or a hover effect to a parent can suddenly push its menu behind something else:

  • opacity below 1, for example a fade-in that ends at 0.99
  • transform and filter, even ones that do nothing visible
  • position: fixed and position: sticky
  • flex and grid items with a z-index
  • isolation: isolate, which exists only to create one on purpose

Quick check: when a high z-index loses, walk up from the element through its parents in your browser’s dev tools and look for the first one with any of the properties above. That parent’s z-index is the number that really competes.

Center with absolute position

Absolute positioning can put something in the exact middle of its parent, even when you don’t know its size:

Centered with top, left and translate

top: 50%; left: 50% puts the stamp’s top-left corner in the middle. translate(-50%, -50%) then pulls it back by half its own width and height, so its center lands there. If the element has a fixed size, there’s another way:

Centered with inset: 0 and margin: auto
.stamp {
  position: absolute;
  inset: 0;
  width: 120px;
  height: 40px;
  margin: auto;
}

For centering things in normal layout, flexbox and grid are usually simpler. The center text tutorial and the flexbox tutorial show how.

Things that go wrong with position

  • Forgetting the relative parent. The absolute child flies off to the corner of the page. Put position: relative on the element it should stick to.
  • Building a whole layout with absolute. It looks right at one screen size and overlaps at every other, because absolute elements don’t push each other. Use flexbox or grid for layout and keep position for small overlays.
  • Sticky with no top, or inside a parent with overflow: hidden. See the checklist above.
  • A fixed bar covering the top of the page. Fixed elements take no space, so the first heading slides under the bar. Add padding-top to the body, or use sticky instead.
  • Fighting with bigger and bigger z-index numbers. Find the stacking context instead (see above).
  • Typos. positon: absolute or z-index: 10px are silently ignored. The CSS Validator flags both.

Questions people ask

What is the difference between position relative and absolute?

A relative element moves from its normal spot but keeps its space, so nothing around it shifts. An absolute element leaves the normal flow (its space closes up) and is placed against its nearest positioned ancestor. They are often used together: .card { position: relative; } and .badge { position: absolute; top: 0; right: 0; } puts the badge in the card's top-right corner.

Why is my absolute element in the wrong place?

It is measured from its nearest ancestor with position set to relative, absolute, fixed or sticky. If there isn't one, it is measured from the page itself and ends up in a corner of the document. Add position: relative to the element it should sit on.

Why is position: sticky not working?

Check three things. It needs a top value (position: sticky; top: 0). It only sticks inside its parent, so a parent that is no taller than the sticky element leaves no room. And an ancestor with overflow: hidden, auto or scroll (even overflow-x: hidden) stops it sticking to the page; overflow: clip doesn't.

What is the difference between fixed and sticky?

A fixed element is pinned to the browser window and takes no space, so content slides under it. A sticky element scrolls normally until it reaches its top value, then holds there while its parent is in view, and it keeps its space in the page. For a header at the top, sticky is usually easier.

Why does my position: fixed element scroll with the page?

An ancestor probably has a transform, filter, perspective, backdrop-filter, contain: paint or will-change: transform. Any of those makes the fixed element measure from that ancestor instead of the window. Move the fixed element so it is a direct child of body, or remove the property from the ancestor.

Why is z-index not working?

z-index only works on positioned elements (relative, absolute, fixed, sticky) and on flex or grid items. On a plain static block it does nothing. If it still loses, a parent is creating a stacking context (a z-index of its own, opacity below 1, a transform, a filter), and the child can never rise above that parent's level.

What does inset: 0 mean in CSS?

It is short for top: 0; right: 0; bottom: 0; left: 0. On an absolute element inside a relative parent it covers the whole parent, which is how you make overlays like a "Sold out" layer over a product card. Like margin, it also takes 2, 3 or 4 values.

How do I center an element with position absolute?

Give the parent position: relative, then on the child use position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%). The translate pulls it back by half its own size, so it works without knowing the width. For a fixed-size box, inset: 0; margin: auto; does the same.

Tools that help