Skip to content
Codewilds

Lesson 27 of 29 · Polish & Interaction

CSS Position: Relative, Absolute, Fixed and Sticky

medium20 XP3 min read

Normal flow places elements one after another, from top to bottom. The position property lets you break out of that flow: nudge an element a little, pin a badge to the corner of a card, keep a button in view while the page scrolls, or make a header stick to the top. Use it for these small details, and keep flexbox and grid for the overall layout.

static and relative

Every element starts out as position: static. It sits in the normal flow, and top, right, bottom and left have no effect on it.

position: relative keeps the element in the flow, with its original space still reserved, and lets you shift it away from where it would normally be:

.nudge {
  position: relative;
  top: 4px;  /* 4px lower than normal */
  left: 8px; /* 8px further right */
}

Its most important job, though, is to be the frame of reference for absolutely positioned children.

absolute

position: absolute takes the element out of the flow completely: the other elements act as if it weren't there. It's placed with top, right, bottom and left, measured from the nearest ancestor that is positioned, meaning any position except static. If there is no such ancestor, the offsets are measured from the page itself.

That's the recipe for a badge in the corner of a card:

.card {
  position: relative; /* the frame for the badge */
}
.badge {
  position: absolute;
  top: 12px;
  right: 12px;
}

fixed and sticky

position: fixed measures from the browser window instead, and the element stays put while the page scrolls. It's used for chat buttons, cookie banners and "back to top" links.

position: sticky is a mix of relative and fixed. The element scrolls along normally until it reaches the offset you set, then it sticks there for as long as its parent is on screen. It only works together with an offset such as top: 0:

.topbar {
  position: sticky;
  top: 0;
}

z-index

When positioned elements overlap, z-index decides which one is drawn on top: higher numbers cover lower ones. It only works on positioned elements (and on flex and grid items). A sticky header usually gets a z-index, so the content scrolling underneath never covers it.

Common mistakes

  • Forgetting position: relative on the parent. The absolutely positioned child then flies off to the corner of the whole page.
  • Sticky without an offset. position: sticky does nothing until you add top, bottom, left or right.
  • Building whole layouts with absolute. Absolutely positioned elements don't make room for each other, so text overlaps as soon as the content changes. Use flexbox and grid for layout, and positioning for the details.
  • Setting z-index on a static element. It has no effect until the element is positioned.

Assignment

Polish the realm news page with positioning:

  1. Make .topbar sticky at the very top (position: sticky and top: 0), and keep it above the content with z-index: 10.
  2. Make .card the frame for its badge with position: relative.
  3. Pin .badge to the top-right corner of the card: position: absolute, top: 12px and right: 12px.
  4. Turn the .help link into a floating button that is fixed 24px from the right and 24px from the bottom of the window.

Scroll the Preview to watch the top bar and the help button stay in view.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Keep learning

Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.

Lesson last updated on .

Write code ↓
HTML & CSS+20 XP

Lesson 27 is part of the full course. The first 7 lessons are free.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.