Lesson 27 of 29 · Polish & Interaction
CSS Position: Relative, Absolute, Fixed and Sticky
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: relativeon the parent. The absolutely positioned child then flies off to the corner of the whole page. - Sticky without an offset.
position: stickydoes nothing until you addtop,bottom,leftorright. - 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-indexon a static element. It has no effect until the element is positioned.
Assignment
Polish the realm news page with positioning:
- Make
.topbarsticky at the very top (position: stickyandtop: 0), and keep it above the content withz-index: 10. - Make
.cardthe frame for its badge withposition: relative. - Pin
.badgeto the top-right corner of the card:position: absolute,top: 12pxandright: 12px. - Turn the
.helplink into a floating button that is fixed24pxfrom the right and24pxfrom 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
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 .