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
| Value | What it does | Common use |
|---|---|---|
static | Normal flow (default) | Everything |
relative | Moves a little from its normal spot; also becomes the "anchor" for absolute children | Parent of a badge |
absolute | Taken out of the flow; placed inside the nearest positioned parent | Badges, icons, tooltips |
fixed | Stays in one place on the screen when you scroll | Chat button, cookie bar |
sticky | Normal, until you scroll to it, then it sticks | Headers, table headings |
Use top, right, bottom and left to say where.
relative: move a little from the normal spot
.nudge {
position: relative;
top: 6px; /* move 6px down */
left: 10px; /* move 10px right */
}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
relative parent..card {
position: relative; /* anchor */
}
.badge {
position: absolute;
top: -10px;
right: -10px;
}position: relative (or any position except static). If they find none, they use the whole page.Center something on top of something else
.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
<div class="hero">
<img src="images/city.jpg" alt="City at night">
<div class="hero-text">
<h1>Welcome to Mumbai</h1>
</div>
</div>.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));
}The dark gradient at the bottom keeps the white text readable, whatever the photo looks like.
Example: sticky header
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.
Fixed headers can hide content
A fixed header floats above the page, so the first lines of the page slide under it. Two fixes:
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).
z-index is on top..popup { position: fixed; z-index: 100; }
.overlay { position: fixed; z-index: 90; }Mini project: a "back to top" button
<a href="#top" class="to-top" aria-label="Back to top">↑</a>.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
| Problem | Fix |
|---|---|
| An absolute element flies to the corner of the page | Its parent needs position: relative |
z-index does nothing | The element needs a position other than static |
| Sticky does not stick | Add top: 0, and check that no parent has overflow: hidden |
| Layout breaks on phones | Too much absolute positioning. Use flexbox or grid for layout instead. |
Practice time
- Put a "SALE" badge on the corner of a card.
- Make your site header sticky.
- Make a round "Help" button fixed at the bottom-right of the screen.
- Put a play button in the exact center of an image using
inset: 0andmargin: auto. - Add white text on a dark gradient over a photo.
Quick summary
relativeparent +absolutechild = exact placement.fixedstays on screen;stickysticks when you scroll to it.z-indexcontrols which box is on top.