Positioning
Normally the browser places elements one after another in the normal flow. The position property lets you break out of that flow: nudge an element, pin a badge to the corner of a card, keep a chat button fixed on screen, or make a header stick to the top as you scroll.
The five position values
Positioned elements are moved with the offset properties top, right, bottom and left (or the shorthand inset, which works like the margin shorthand). The offsets have no effect on static elements.
| Value | In normal flow? | Offsets are measured from… | Typical use |
|---|---|---|---|
static (default) | Yes | Offsets ignored | Everything, by default |
relative | Yes; its original space is kept | Its own normal position | Small nudges; a reference box for absolute children |
absolute | No; others close the gap | The nearest positioned ancestor | Badges, tooltips, overlays |
fixed | No | The viewport (browser window) | Floating buttons, cookie bars |
sticky | Yes, until it reaches its offset | Its scrolling container | Headers and table headings that stick while scrolling |
relative shifts a box but leaves a gap where it was; absolute lifts it out of the flow and places it inside the nearest positioned ancestor; fixed pins it to the screen; sticky scrolls normally until it hits its offset, then sticks.relative and absolute: the classic pair
An absolutely positioned element looks up its ancestors for the first one whose position is anything other than static, and positions itself inside that box. If there is none, it uses the whole page. That's why you'll so often see position: relative on a parent with no offsets at all: it simply becomes the reference box.
.product-card {
position: relative; /* reference box for the badge */
}
.product-card .badge {
position: absolute;
top: 0.5rem;
right: 0.5rem;
}
The badge now sits half a rem from the top-right corner of each card, however big the card is.
fixed and sticky
A fixed element stays in the same place on screen while the page scrolls underneath it, like a WhatsApp "chat with us" button in the bottom corner. Because it's removed from the flow, make sure it doesn't permanently cover important content on small screens.
A sticky element is a hybrid: it scrolls with the page until it reaches the offset you set (for example top: 0), then sticks there until its parent scrolls out of view. You must set at least one offset, or it behaves like relative.
If position: sticky doesn't work, check the ancestors: an ancestor with overflow: hidden or overflow: auto becomes the scrolling container, so the element sticks inside that box instead of the page (or seems not to stick at all). Also, a sticky element can only stick within its own parent.
Stacking with z-index
When positioned elements overlap, later elements in the HTML are drawn on top. z-index changes that order: a higher number is closer to the viewer. It works on positioned elements (anything except static) and on flex and grid items.
.site-header { position: sticky; top: 0; z-index: 10; }
.modal { position: fixed; inset: 0; z-index: 100; }
inset: 0 means top, right, bottom and left are all 0, so the modal backdrop covers the whole viewport. Note that a z-index only competes with elements in the same stacking context: if a parent has its own z-index (or properties such as opacity below 1 or a transform), its children can never rise above elements outside that parent. When a huge z-index "doesn't work", a stacking context is usually the reason.
Try it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Positioning</title>
<style>
body { font-family: system-ui, sans-serif; margin: 0; }
header {
position: sticky;
top: 0;
background: #1e3a8a;
color: white;
padding: 0.75rem 1rem;
z-index: 10;
}
main { padding: 1rem; }
.card {
position: relative;
width: 220px;
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 8px;
margin-bottom: 1rem;
}
.badge {
position: absolute;
top: -10px;
right: -10px;
background: #dc2626;
color: white;
border-radius: 999px;
padding: 0.2rem 0.6rem;
font-size: 0.8rem;
}
.chat {
position: fixed;
right: 1rem;
bottom: 1rem;
background: #0f766e;
color: white;
border-radius: 999px;
padding: 0.6rem 1rem;
}
.tall { height: 900px; background: linear-gradient(#f8fafc, #e2e8f0); }
</style>
</head>
<body>
<header>Soko Fresh: scroll down</header>
<main>
<div class="card">Sukuma wiki, 1 bunch <span class="badge">-20%</span></div>
<div class="tall">Long content…</div>
</main>
<div class="chat">Chat with us</div>
</body>
</html>
A dark blue header sits at the top. Below it is a card with a red "-20%" pill hanging over its top-right corner, because the badge is positioned 10px outside the card's edges. A teal "Chat with us" button floats in the bottom-right of the preview. As you scroll the long grey area, the header stays stuck to the top and the chat button never moves.
Key points
relativenudges an element and keeps its space; it also creates a reference box for absolute children.absoluteremoves an element from the flow and positions it inside the nearest positioned ancestor.fixedpins to the viewport;stickysticks once it reaches its offset.z-indexcontrols overlap order, but only within the same stacking context.
Tip: use the ← and → keys to move between pages.
