Web design tutorials and tools since 2014Privacy
Search 143 articles

Coding

CSS Position Explained: Static, Relative, Absolute, Fixed and Sticky

The CSS position property decides two things about an element: whether it stays in the normal flow of the page, and what top, right, bottom and left are measured from. It has five values, static, relative, absolute, fixed and sticky, and most position bugs come from getting the second part wrong.

We built a live playground where you can switch one box between all five values and scroll to see what happens, plus demos of the bugs people hit most: sticky that doesn't stick, absolute that escapes its parent, fixed that scrolls away and z-index: 9999 that still ends up underneath. We measured every demo in Chrome 158, Firefox 160 and WebKit 27.2 (Safari's engine), and all three gave the same results.

The Short Answer

ValueIn the flow?Offsets measured fromScrolls with the page?Typical use
staticYesIgnoredYesThe default for every element
relativeYes, its space staysIts own normal positionYesNudging an element, and making it the anchor for absolute children
absoluteNoThe nearest positioned ancestor, or the pageYesBadges, tooltips, dropdowns, overlays inside a card
fixedNoThe browser window (viewport)NoChat buttons, cookie bars, full-screen overlays
stickyYesThe nearest scrolling box, within its parentUntil it reaches its offset, then it sticksHeaders, table headings, sidebars

A "positioned" element is anything with a position value other than static. That word matters, because absolute looks for the nearest positioned ancestor, and z-index only works on positioned elements (and on flex and grid items, as we'll show).

The Five Values in the Playground

The playground is a small page inside a frame, so fixed and sticky behave as they would on a real site. The orange box has top: 20px; left: 40px in every mode. Its parent has position: relative, which you can switch off.

static

This is the default. The element sits where the normal flow puts it, and top, left and z-index do nothing. In our test the box stayed at the same spot, 16 pixels in and 70 pixels down from the top of its parent, whatever offsets we gave it. You almost never write position: static yourself, except to reset an element that a stylesheet positioned at a wider screen size.

relative

CSS position playground with relative selected: the orange box is shifted 40 pixels right and 20 pixels down, and the text after it hasn't moved
relative moves the box but keeps its original space, so the text after it stays put.

relative moves the element from where it would normally be. With top: 20px; left: 40px the box moved exactly 40 pixels right and 20 down from its static spot, from (16, 70) to (56, 90). The space it left behind stays reserved, so nothing around it moves. That's why the box can overlap the text below it.

In practice relative is used far more often without offsets than with them. Its main job is to make an element positioned, so it becomes the reference box for absolutely positioned children.

absolute

Playground with absolute selected: the box sits 20 pixels from the top and 40 from the left of its relatively positioned parent, and the text after it has moved up into its old space
absolute takes the box out of the flow. The grey line of text moves up to fill the gap.

absolute takes the element out of the flow completely. Other elements behave as if it isn't there, and the offsets are measured from its containing block: the nearest ancestor that is positioned. Our box landed 42 pixels from the left and 22 from the top of the parent's outer edge. The extra 2 pixels are the parent's border. Offsets start at the inside of the border, at the edge of the padding box.

Percentages work the same way. In a separate test, top: 50%; left: 50% inside a 400 × 200 box with 20 pixels of padding put the element 220 and 120 pixels in, half of the 440 × 240 padding box, not half of the content area.

When we switched off the parent's position: relative, the box jumped to 20 pixels from the top of the whole page. It isn't fixed, though: after we scrolled the frame 400 pixels, it had scrolled away with the content, to −380.

fixed

fixed also leaves the flow, but its offsets are measured from the viewport, the visible area of the browser window. The box sat 20 pixels from the top of the frame before we scrolled and was still at exactly 20 pixels after we scrolled 400. The parent's position made no difference. One exception breaks this, and it causes some of the most confusing bugs. We cover it below.

sticky

sticky is a hybrid. The element stays in the flow and behaves like relative until scrolling would push it past its offset. Then it sticks there, like fixed, but only until the end of its parent. In the playground, with top: 20px, the box held at 20 pixels from the top of the frame while its parent was on screen. Once we scrolled past the parent, the box left with it. That limit is by design, and it's one of the reasons sticky "doesn't work".

Relative vs Absolute: The Containing Block

Containing block demo: an orange box with absolute, top 0 and right 0 sits in the top-right corner of the parent, which has position relative
The box is always a child of .parent. Only the ancestors' position decides which corner it goes to.

This demo keeps the HTML the same and changes only which ancestor is positioned. The box has position: absolute; top: 0; right: 0.

  • Parent is relative: the box goes to the parent's top-right corner.
  • Only the grandparent is relative: the box skips its parent and goes to the grandparent's corner.
  • Nothing is positioned: the box escapes entirely and lands in the top-right corner of the page, 0 pixels from the top of the document.

So when an absolute element ends up somewhere unexpected, don't look at the element. Look up the tree for the first ancestor with position set, and add position: relative to the one you meant. Elements with transform, filter or contain: paint also act as the containing block for absolute children, even without a position value.

A useful shorthand: inset: 0 sets all four offsets at once, so position: absolute; inset: 0 makes an element cover its containing block. Per MDN's compatibility data, inset works in Chrome 87, Firefox 66 and Safari 14.1 and later.

Fixed and the Viewport

A Chat button with position fixed stuck in the bottom-right corner of a card instead of the browser window, because the card has a transform
A fixed button inside a card with transform: translateZ(0). It's pinned to the card, not the window.

A fixed element is supposed to stay put relative to the browser window. In our demo, a "Chat" button with position: fixed; right: 16px; bottom: 16px sits inside a card in the HTML. On a plain card it went to exactly 16 pixels from the right and bottom of the window, as expected.

Then we gave the card each of these properties in turn:

  • transform: translateZ(0), or any other transform
  • filter, even a filter that changes nothing, such as saturate(1)
  • will-change: transform
  • contain: paint

Every one of them trapped the button inside the card, in all three browsers. The card becomes the button's containing block, so the button scrolls with the card. perspective, backdrop-filter and container-type have the same effect according to the spec, but we didn't test those.

This bites most often with animation libraries and page-transition wrappers that put a transform on a wrapper around the whole page. The fix is to move the fixed element out of the transformed ancestor, usually to the end of <body>. In a framework, render it through a portal.

Sticky, and Why It Doesn't Stick

A scroll box scrolled down 150 pixels with a dark section header still pinned to its top
Working sticky: after 150 pixels of scrolling, the header is still at the top of the box.

We scrolled a box 150 pixels and measured where its sticky header ended up. When it works, the header sits 0 pixels from the top of the box. In every broken case, it had scrolled away to −150. These are the causes we reproduced, all with the same result in all three browsers:

  1. No offset. position: sticky without top (or bottom, left or right) behaves exactly like relative. You need at least one offset, usually top: 0.
  2. An ancestor has overflow: hidden. Any overflow value other than visible or clip turns that ancestor into a scroll container. Sticky then sticks inside that ancestor, which never scrolls, so it never sticks. overflow: auto and overflow: scroll do the same.
  3. The parent is too short. A sticky element can't leave its parent. If the header is wrapped in its own <div>, that div is only as tall as the header, so there's no room to stick.
  4. It's stretched in a flex or grid row. A sticky sidebar in display: flex or display: grid stretches to the height of the row by default. In our test the sidebar grew to the full 3,000 pixels of the main column and scrolled away. Adding align-self: start (or align-items: start on the container) shrank it to its content and it stuck at 0.

For cause 2, if you used overflow: hidden only to clip something, replace it with overflow: clip. It clips the same way but doesn't create a scroll container, and in our demo it brought the sticky header back. MDN lists support from Chrome 90, Firefox 81 and Safari 16. To find the guilty ancestor, select the sticky element in dev tools and walk up the tree checking the computed overflow.

position: sticky works without a prefix in Chrome 56, Firefox 32 and Safari 13. You no longer need -webkit-sticky. For a full header that shrinks as you scroll, see our responsive fixed and resizing header tutorial.

z-index and Stacking Contexts

A dropdown menu with z-index 9999 hidden behind card B, which has z-index 1, because card A has position relative and z-index 1
The menu has z-index: 9999, card B has z-index: 1, and the menu still goes underneath.

z-index controls which element is on top when they overlap, but a higher number doesn't always win. Numbers are only compared within the same stacking context. When an element creates a new stacking context, everything inside it is stacked as one layer at the parent's level.

Our demo has a dropdown menu with z-index: 9999 inside card A, hanging over card B, which has position: relative; z-index: 1. We checked which element was on top at a point where the two overlap:

Card A hasOn topWhy
NothingMenuNo new context, so 9999 is compared directly with 1
position: relative; z-index: 1Card BCard A is a context at level 1. It ties with card B, and B comes later in the HTML
opacity: .99Card BOpacity below 1 creates a context at level 0
transform: translateZ(0)Card BSame: a context at level 0
position: relative; z-index: 2MenuCard A's whole layer now beats card B

The fix is never a bigger number on the menu. Find the ancestor that creates the stacking context and raise that, or remove whatever creates the context if you don't need it. Common causes are opacity below 1, any transform or filter, position: fixed or sticky, isolation: isolate, will-change, and position: relative or absolute combined with a z-index other than auto.

Two more rules we confirmed in all three browsers:

  • z-index is ignored on static elements, so a static block with z-index: 2 went under a later relative sibling.
  • Flex and grid items don't need position, so a flex item with z-index: 2 and no position did go on top of its overlapping sibling.

For modals, there's a way out of the z-index race altogether. A native <dialog> opened with showModal() goes into the browser's top layer, above everything else on the page. In our test it appeared above an element with z-index: 2147483647, the highest value most browsers accept.

Real Patterns: Badge, Tooltip, Sticky Header, Modal

Four patterns: a New badge on a card corner, a tooltip above a button, a sticky site header in a scroll box and a button that opens a modal overlay
Four everyday uses of position. All four are on the demo page, with the modal working.

Badge on a card

.card  { position: relative; }
.badge { position: absolute; top: -10px; right: -10px; }

The card is the containing block, and negative offsets push the badge over its corner. Because offsets start inside the border, our badge stuck out 9 pixels past the card's 1-pixel border, not 10. If .card has overflow: hidden for rounded images, it will clip the badge. Use overflow: clip on the image wrapper instead, or round the image itself. For more card layouts, see our card UI design resources.

Tooltip

.tip { position: relative; }
.tip [role=tooltip] {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
}

bottom: 100% puts the tooltip directly above the trigger, and the extra 8 pixels make a gap. left: 50% moves its left edge to the middle, and translateX(-50%) pulls it back by half its own width. We measured a centre offset of 0 pixels and a gap of exactly 8. Show it on :hover and :focus-within so keyboard users get it too. Our older pure CSS tooltips tutorial builds the same idea from a data attribute.

Sticky header

header { position: sticky; top: 0; z-index: 10; }

Unlike a fixed header, a sticky header keeps its space in the flow, so you don't need to add top padding to the page to stop content hiding behind it. Add a z-index so positioned content that scrolls underneath doesn't paint over it. If it doesn't stick, go through the four causes above.

Modal overlay

.overlay { position: fixed; inset: 0; background: rgb(0 0 0 / .55); }
.modal {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

The overlay covers the viewport with inset: 0, and the box is centred with the same 50% and translate trick as the tooltip. Put the overlay at the end of <body> so no transformed ancestor traps it. For new projects, consider <dialog> instead. It handles the top layer, focus and the Escape key for you. For a CSS-only version with animation, see how to create a pure CSS modal window effect.

How We Tested

Every demo is on one page, using plain CSS and a few lines of JavaScript to switch classes. We loaded it with Playwright in Chrome 158, Firefox 160 and WebKit 27.2, set the viewport to 1280 × 900 and read element positions with getBoundingClientRect(). For the z-index cases we used elementFromPoint() at the overlap. We built the flex and grid sticky sidebar, the static z-index, the percentage offsets and the dialog cases as separate small test pages.

All the results in this article matched across the three engines, except for differences of 1 pixel in text height. Support versions come from MDN's browser-compat-data (version 8.1.10, November 2026). If you're new to stylesheets, start with how to link CSS to HTML. For layout rather than positioning, see Flexbox vs Grid.

FAQ

What is the default value of position in CSS?

static. Every element starts in the normal flow, and top, right, bottom, left and z-index have no effect until you change it.

What is the difference between relative and absolute?

A relative element stays in the flow and moves from its own normal position, leaving its space behind. An absolute element leaves the flow and is placed against its nearest positioned ancestor, so other elements close the gap.

Does position: absolute need a relative parent?

No, but without a positioned ancestor it is placed against the whole page. Add position: relative to the element you want it placed against.

Why is position: sticky not working?

Usually one of four things: no top value, an ancestor with overflow: hidden, auto or scroll, a parent only as tall as the sticky element, or a flex or grid item stretched to the full row height. Add top: 0, change the overflow to clip, or set align-self: start.

Why is my position: fixed element scrolling with the page?

An ancestor has a transform, filter, will-change: transform or contain: paint, which makes it the containing block. Move the fixed element outside that ancestor.

Why doesn't z-index work?

Either the element is static (z-index needs a position other than static, or a flex or grid item), or it sits inside a stacking context that is itself below the other element. Raise the ancestor that creates the context, not the element.

What is the difference between fixed and sticky?

A fixed element is always pinned to the viewport and takes no space in the layout. A sticky element scrolls normally, takes up space, and pins only once it reaches its offset, and only until the end of its parent.

How do I centre an absolutely positioned element?

Use top: 50%; left: 50%; transform: translate(-50%, -50%). If it has a fixed width and height, inset: 0; margin: auto also works.