Chapter 9 of 15

CSS Position: Relative, Absolute, Fixed and Sticky

Sometimes you need to put a box in an exact spot, like a badge on a card or a header that stays at the top. That is what position does.

Five position values

ValueWhat it doesCommon use
staticNormal flow (default)Everything
relativeMoves a little from its normal spot; also becomes the "anchor" for absolute childrenParent of a badge
absoluteTaken out of the flow; placed inside the nearest positioned parentBadges, icons, tooltips
fixedStays in one place on the screen when you scrollChat button, cookie bar
stickyNormal, until you scroll to it, then it sticksHeaders, table headings

Use top, right, bottom and left to say where.

relative: move a little from the normal spot

CSS
.nudge {
  position: relative;
  top: 6px;    /* move 6px down */
  left: 10px;  /* move 10px right */
}
Result in the browser
1
2 (moved)
3

Notice the other boxes do not move. The space where box 2 used to be stays empty. top pushes it down from the top, which feels backwards at first.

Example: a badge on a card

A product card with a NEW badge on its top right corner
The absolute badge is placed using the corner of its relative parent.
CSS
.card {
  position: relative;   /* anchor */
}
.badge {
  position: absolute;
  top: -10px;
  right: -10px;
}
Result in the browser
Product cardNEW
The golden ruleAbsolute children look for the nearest parent with position: relative (or any position except static). If they find none, they use the whole page.

Center something on top of something else

CSS
.photo {
  position: relative;
}
.play-button {
  position: absolute;
  inset: 0;           /* top, right, bottom and left all 0 */
  margin: auto;       /* center in the middle */
  width: 64px;
  height: 64px;
}

inset: 0 is a short way to write top: 0; right: 0; bottom: 0; left: 0;. With a fixed size and margin: auto, the element sits in the exact middle. This is how play buttons sit on video thumbnails.

Text on top of an image

HTML
<div class="hero">
  <img src="images/city.jpg" alt="City at night">
  <div class="hero-text">
    <h1>Welcome to Mumbai</h1>
  </div>
</div>
CSS
.hero { position: relative; }
.hero img { width: 100%; display: block; }
.hero-text {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  padding: 24px;
  color: white;
  background: linear-gradient(transparent, rgba(0,0,0,0.7));
}
Result in the browser
Welcome to Mumbai

The dark gradient at the bottom keeps the white text readable, whatever the photo looks like.

Example: sticky header

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

The header on this website uses exactly this. Scroll down and watch the top bar.

Two tall pages: a fixed help button stays in the screen corner, a sticky header stays at the top of the screen
The blue frame is the part of the page on screen. Fixed and sticky elements stay visible while you scroll.

Fixed headers can hide content

A fixed header floats above the page, so the first lines of the page slide under it. Two fixes:

CSS
body { padding-top: 64px; }          /* same as header height */
html { scroll-padding-top: 72px; }    /* links like #contact stop below the header */

A sticky header does not have this problem, because it keeps its own space in the page. That is why sticky is often the better choice.

z-index: who is on top?

When boxes overlap, z-index decides which one is in front. A bigger number is closer to you. It only works on positioned elements (not static).

Three overlapping cards with z-index 1, 10 and 100, the 100 card in front
When boxes overlap, the one with the bigger z-index is on top.
CSS
.popup   { position: fixed; z-index: 100; }
.overlay { position: fixed; z-index: 90; }
Do not overuse positionUse flexbox and grid for layout. Use position only for small special things. Too much absolute positioning breaks on phones.

Mini project: a "back to top" button

HTML
<a href="#top" class="to-top" aria-label="Back to top">↑</a>
CSS
.to-top {
  position: fixed;
  right: 20px;
  bottom: 20px;
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #2563eb;
  color: white;
  text-decoration: none;
  font-size: 22px;
  z-index: 50;
}
html { scroll-behavior: smooth; }

Add id="top" to your header. The button stays in the corner while you scroll, and one tap brings you back up smoothly.

Common mistakes

ProblemFix
An absolute element flies to the corner of the pageIts parent needs position: relative
z-index does nothingThe element needs a position other than static
Sticky does not stickAdd top: 0, and check that no parent has overflow: hidden
Layout breaks on phonesToo much absolute positioning. Use flexbox or grid for layout instead.

Practice time

  1. Put a "SALE" badge on the corner of a card.
  2. Make your site header sticky.
  3. Make a round "Help" button fixed at the bottom-right of the screen.
  4. Put a play button in the exact center of an image using inset: 0 and margin: auto.
  5. Add white text on a dark gradient over a photo.

Quick summary

  • relative parent + absolute child = exact placement.
  • fixed stays on screen; sticky sticks when you scroll to it.
  • z-index controls which box is on top.