Skip to content

Intermediate · CSS layout and responsive design · topic 26 of 64 · 25 min

Positioning and stacking

Explanation

The position property lets you move an element out of the normal flow: relative nudges it, absolute places it inside a positioned parent, fixed pins it to the screen, and sticky keeps it in view while scrolling. z-index decides which element is on top when they overlap.

Normal flow first

By default every element is position: static. It sits in the normal flow: blocks stack from top to bottom and inline content flows like text. Use flexbox and grid for layout; use positioning only for special cases like badges, overlays and sticky headers.

Think of it like this: Think of a notice board. Most papers are pinned in neat rows (normal flow). A "SALE" sticker stuck on the corner of one poster is absolute. A clock nailed to the wall that stays put whatever you do is fixed. A label that slides down with you but stops at the end of its section is sticky.
ValueMoves relative toStill takes its space?
staticNot moved (default)Yes
relativeIts own normal placeYes
absoluteThe nearest positioned ancestorNo
fixedThe browser window (viewport)No
stickyNormal, until it reaches an edge while scrollingYes

The badge pattern

css
.card {
  position: relative;   /* becomes the reference for children */
}
.card .badge {
  position: absolute;
  top: 8px;
  right: 8px;
}
A "20% OFF" badge in the top-right corner of a product card

An absolute element looks up the tree for the nearest ancestor whose position is not static. Giving the card position: relative (without moving it) makes the card that reference. Without it, the badge would jump to the corner of the whole page.

Sticky and fixed

css
.site-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: white;
}
.pay-bar {
  position: fixed;
  inset: auto 0 0 0;   /* top right bottom left: stuck to the bottom */
}
A header that sticks while scrolling, and a "Pay now" bar fixed to the bottom

inset is a shorthand for top, right, bottom and left. inset: 0 stretches an absolute or fixed element over its whole reference box, which is useful for overlays.

z-index and stacking

When positioned elements overlap, z-index decides who is on top: higher numbers come forward. z-index only works on positioned elements (and on flex or grid items). Elements are grouped into stacking contexts, so a child can never rise above its parent's group, however big its number.

Tip: Use a small, planned set of z-index values (for example 10 for sticky headers, 100 for dropdowns, 1000 for modals) instead of random huge numbers like 99999.
Careful: A fixed bar covers content. Add padding-bottom to the page equal to the bar height, or the last button on the page may be hidden under it.

Watch the lesson

Positioning and stacking · 1/6

static = normal flow

Normally elements follow the normal flow, one after another. position lets you break out of it.

Prefer a video by a teacher? Find free videos on “css position relative absolute fixed sticky z-index tutorial” on YouTube ↗

In real life: A sticky "Pay now" bar in a train booking app

On the passenger details page, the total fare and the "Pay now" button sit in a bar with position: fixed at the bottom of the screen, so the user can pay from anywhere while scrolling through the passenger list. The page has padding-bottom equal to the bar height, so the last passenger form is never hidden.

Practice (0/2 done)

Task 1 · Code · HTML

Place the .badge in the top-right corner of the .card: give .card position: relative, and .badge position: absolute with top: 8px and right: 8px.

Task 2 · Code · HTML

Make the <header> sticky: position: sticky, top: 0 and z-index: 10.

Quick quiz

Question 1

1.An absolute badge flies to the corner of the whole page. What is missing?

Question 2

2.Which value keeps a header visible while you scroll its section, but in the normal flow before that?

Question 3

3.What does a higher z-index do?

Question 4

4.What is the default position value?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: CSS Grid