Relative and absolute positioning
Overview
Normal flow, flexbox and grid place elements in relation to each other. Sometimes you need to break out of that: a badge in a card’s corner, a close button on a dialog, an icon inside an input, a play button over a thumbnail. That’s what positioning is for.
The position property has five values:
| Value | In the flow? | Positioned relative to |
|---|---|---|
static |
yes (default) | — offsets are ignored |
relative |
yes | its own normal position |
absolute |
no | its containing block |
fixed |
no | the viewport |
sticky |
yes | its scroll container, until it hits limits |
This lesson covers relative and absolute; the next covers fixed and sticky.
Offsets: top, right, bottom, left, inset
Positioned elements (anything except static) can be moved with the offset properties top, right, bottom and left — or their logical equivalents inset-block-start, inset-inline-end and so on. The inset shorthand sets all four:
inset: 0; /* top: 0; right: 0; bottom: 0; left: 0 */
inset: 1rem 2rem; /* top & bottom, left & right */
inset-inline: 0; /* left and right */position: relative
A relatively positioned element is laid out normally, then shifted by its offsets — but its original space is kept, so nothing around it moves:
.nudge {
position: relative;
top: 2px; /* 2px down from where it would have been */
}Shifting things this way is rarely needed. The main use of position: relative is creating a containing block for absolutely positioned children, with no offsets at all.
position: absolute
An absolutely positioned element is removed from the flow — the elements around it behave as if it weren’t there — and placed using offsets measured from its containing block:
.card { position: relative; }
.badge {
position: absolute;
top: 10px;
right: 10px;
}The containing block
For an absolutely positioned element, the containing block is the padding box of the nearest ancestor whose position isn’t static. If no ancestor is positioned, it’s the initial containing block — the size of the viewport, at the top of the document.
That’s why you so often see position: relative on a parent without any offsets: it says “position my absolute children relative to me”.
Gotcha: ancestors with
transform,filter,perspective,contain: layout(and a few others) also become containing blocks for positioned descendants — evenfixedones. If an absolutely positioned element ends up in the wrong place, check its ancestors for those properties.
Sizing absolute elements
An absolutely positioned element shrinks to fit its content (like fit-content) unless you size it. Setting both opposing offsets stretches it:
.overlay {
position: absolute;
inset: 0; /* fills the containing block completely */
background: rgb(0 0 0 / 0.5);
}Centring on a point
To centre an element on a point (like the middle of its container), place its top-left corner there and pull it back by half its own size:
.play {
position: absolute;
top: 50%;
left: 50%;
translate: -50% -50%; /* half of the element's own width and height */
}Percentages in translate refer to the element’s own size, while percentages in top/left refer to the containing block — which is why this works for any size. For an element of known size, inset: 0; margin: auto; also centres it.
The full-card link pattern
A common real-world use: making a whole card clickable without wrapping everything in a link (which would make screen readers read all the card’s text as the link name):
<article class="card">
<img src="thumb.jpg" alt="" />
<h3><a class="card-link" href="/courses/css">CSS layout</a></h3>
<p>Learn flexbox and grid.</p>
</article>.card { position: relative; }
.card-link::after {
content: "";
position: absolute;
inset: 0; /* the link's pseudo-element covers the whole card */
}The link text stays short and meaningful, while the click target covers the card. Give other interactive elements in the card (buttons, secondary links) position: relative; z-index: 1 so they stay clickable above the overlay.
When not to use absolute
Absolute positioning takes elements out of the flow, so their neighbours no longer make room for them. That makes it fragile for anything except small decorative or overlay elements:
- ✗ Don’t build page or component layouts with it — content of different lengths will overlap.
- ✗ Don’t use it to align things side by side — use flexbox or grid.
- ✓ Do use it for overlays, badges, icons inside inputs, decorative shapes and close buttons.
Grid can often replace absolute positioning for overlays too: placing two items in the same grid cell stacks them, while still letting the cell grow to fit the larger one (see Placing grid items).
Rule of thumb: if the element should affect the size or position of anything around it, it shouldn’t be absolutely positioned.
A course card with a badge and an overlay
Build a .card containing a .media area (an image placeholder plus a .play button), a title and a “New” .badge. Position the badge 0.75rem from the card’s top-right corner. Centre the .play button exactly over the image placeholder, whatever the card’s width. Then add a full-card link (.card-link::after) that makes the whole card clickable, while the badge and play button stay on top and remain clickable themselves.
<article class="card">
<div class="media">
<div class="thumb"></div>
<button class="play" aria-label="Play preview">▶</button>
</div>
<span class="badge">New</span>
<h3><a class="card-link" href="/courses/css">CSS layout</a></h3>
</article>
<style>
* { box-sizing: border-box; }
.card { width: 18rem; border: 1px solid #d6d6d2; border-radius: 12px; padding: 0.75rem; }
.thumb { aspect-ratio: 16 / 9; background: #c9d8f8; border-radius: 8px; }
.badge { background: #c0392b; color: white; padding: 0.125rem 0.5rem; border-radius: 4px; }
.play { width: 3rem; height: 3rem; border-radius: 50%; border: none; }
/* your code here */
</style><article class="card">
<div class="media">
<div class="thumb"></div>
<button class="play" aria-label="Play preview">▶</button>
</div>
<span class="badge">New</span>
<h3><a class="card-link" href="/courses/css">CSS layout</a></h3>
</article>
<style>
* { box-sizing: border-box; }
.card { width: 18rem; border: 1px solid #d6d6d2; border-radius: 12px; padding: 0.75rem; }
.thumb { aspect-ratio: 16 / 9; background: #c9d8f8; border-radius: 8px; }
.badge { background: #c0392b; color: white; padding: 0.125rem 0.5rem; border-radius: 4px; }
.play { width: 3rem; height: 3rem; border-radius: 50%; border: none; }
.card { position: relative; } /* containing block for the badge and the link */
.media { position: relative; } /* containing block for the play button */
.badge {
position: absolute;
inset-block-start: 0.75rem;
inset-inline-end: 0.75rem;
z-index: 1; /* above the full-card link */
}
.play {
position: absolute;
top: 50%;
left: 50%;
translate: -50% -50%; /* centre on the middle of .media */
z-index: 1; /* above the full-card link */
}
.card-link::after {
content: "";
position: absolute;
inset: 0; /* covers the whole card */
border-radius: inherit;
}
</style>Summary
position: relativeoffsets an element visually but keeps its original space in the flow.position: absoluteremoves an element from the flow and places it relative to its containing block.- The containing block is the nearest ancestor with a position other than
static— or the initial containing block if there isn’t one. inset: 0stretches a positioned element to all four edges;translate: -50% -50%centres it on a point.- Ancestors with
transform,filterorcontainalso become containing blocks for positioned descendants.