Positioning and stacking order
W3C CSS Snapshot 2026
CSS positioning changes where a box is drawn. Stacking order determines which box appears on top when two boxes overlap.
On this page
Place a badge inside its card
Normal flow uses position: static. With position: relative, the card keeps its normal place and becomes a positioning reference. The badge uses position: absolute, so it leaves normal flow. top and right measure from the nearest positioned ancestor in this example.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Place a badge inside its card</title>
<style>
.card { position: relative; width: 220px; padding: 24px; border: 2px solid teal; }
.badge { position: absolute; top: 4px; right: 4px; background: gold; }
</style>
</head>
<body>
<div class="card">Pen <span class="badge">Sale</span></div>
</body>
</html>
What to expect Sale sits near the top-right of the card. Remove position: relative to see the reference change.
How do fixed and sticky positions work?
Fixed typically uses the viewport as its reference and stays in place during scrolling. Sticky starts in flow and sticks to a scroll container edge after reaching its offset, until the box reaches the boundary of its containing block.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How do fixed and sticky positions work?</title>
<style>
body { min-height: 150vh; }
header { position: sticky; top: 0; background: #d6efea; padding: 8px; }
aside { position: fixed; right: 12px; bottom: 12px; }
</style>
</head>
<body>
<h1>Example Store</h1><header>Catalog</header><p>Pens and notebooks</p><aside>Help</aside>
</body>
</html>
What to expect Scroll the page. The Catalog heading stays at the top and “Help” stays at the bottom right.
What does z-index compare?
A stacking context is a group of elements stacked together. z-index changes the order of applicable elements within their context. A child cannot move above a higher sibling context by increasing only its own z-index.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>What does z-index compare?</title>
<style>
.board { position: relative; height: 100px; }
.card { position: absolute; width: 100px; padding: 12px; }
.pen { z-index: 2; background: gold; }
.notebook { z-index: 1; top: 20px; left: 24px; background: lightblue; }
</style>
</head>
<body>
<div class="board"><div class="card pen">Pen</div><div class="card notebook">Notebook</div></div>
</body>
</html>
What to expect Pen covers part of Notebook. Give Notebook z-index: 3 and reload to change the order.