CSS Z-index
When positioned elements overlap, which one appears on top? By default, elements later in the HTML are painted over earlier ones. The z-index property lets you control the stacking order explicitly — think of it as the element's height above the page, along a "z axis" pointing toward you.
Basic usage
z-index takes an integer (negative, zero or positive). Higher values are drawn on top. It works on positioned elements (relative, absolute, fixed, sticky) and on flex and grid items.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.box { position: absolute; width: 120px; height: 120px; color: white; font-weight: bold; padding: 8px; box-sizing: border-box; }
.red { background: #e53935; top: 20px; left: 20px; z-index: 3; }
.green { background: #43a047; top: 60px; left: 80px; z-index: 1; }
.blue { background: #1e88e5; top: 100px; left: 140px; z-index: 2; }
</style>
</head>
<body>
<div class="box red">red z 3</div>
<div class="box green">green z 1</div>
<div class="box blue">blue z 2</div>
</body>
</html>Even though red comes first in the HTML, it's on top because its z-index is highest. Try changing the numbers.
Default stacking order
Without z-index, the browser paints in this order (back to front):
| Layer | What |
|---|---|
| 1 | Background and borders of the root / stacking context |
| 2 | Positioned elements with negative z-index |
| 3 | Normal block elements in source order |
| 4 | Floats |
| 5 | Inline content (text) |
| 6 | Positioned elements with z-index: auto or 0, in source order |
| 7 | Positioned elements with positive z-index |
Tip: Use a small, documented scale like 1 (raised), 10 (dropdown), 100 (sticky header), 1000 (modal). Escalating to
z-index: 99999is a sign of a stacking-context problem.
Stacking contexts
This is the part that confuses people. Some elements create a stacking context: a self-contained layer. Children's z-index values only compete inside that context. A child with z-index: 9999 can never escape a parent context that sits below a sibling.
Things that create a stacking context include:
- A positioned element with a
z-indexother thanauto opacityless than 1transform,filter,perspectivenotnoneposition: fixedorstickyisolation: isolate
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; position: relative; }
.parent { position: relative; z-index: 1; width: 200px; height: 130px; background: #ffe082; padding: 6px; }
.child { position: absolute; z-index: 9999; top: 60px; left: 120px; width: 150px; height: 70px; background: #8e24aa; color: white; padding: 6px; }
.other { position: absolute; z-index: 2; top: 80px; left: 180px; width: 160px; height: 110px; background: #26a69a; color: white; padding: 6px; }
</style>
</head>
<body>
<div class="parent">parent z 1
<div class="child">child z 9999</div>
</div>
<div class="other">sibling z 2 — I'm above the 9999 child!</div>
</body>
</html>The purple child is trapped in its parent's context (z 1), which is below the teal sibling (z 2). Remove z-index: 1 from .parent and the child jumps on top.
Negative z-index
A negative value puts an element behind its parent's content — handy for decorative backgrounds. Use isolation: isolate on the parent so the element doesn't slip behind the whole page.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; padding: 30px; }
.title { position: relative; isolation: isolate; display: inline-block; font-size: 32px; font-weight: 800; padding: 10px 20px; }
.title::before { content: ""; position: absolute; inset: 0; z-index: -1; background: #ffeb3b; transform: rotate(-4deg); border-radius: 8px; }
</style>
</head>
<body>
<h1 class="title">Highlighted</h1>
</body>
</html>Test yourself
When does z-index take effect on a normal block element?
- Always
- When it is positioned (or a flex/grid item)
- Only with display: inline
- Only with negative values
A child has z-index 9999 but appears below another element with z-index 2. Why?
- 9999 is too large
- The child is inside a stacking context whose parent is below the other element
- z-index only accepts 1-10
- Browsers ignore large z-index
Which of these creates a new stacking context?
- color: red
- opacity: 0.9
- margin: 10px
- display: block