Sub topic 9
Overlapping elements and z-index
This chapter assumes you know basic HTML and CSS selectors, the box model, and how position values and offsets move an element. Once you move boxes with position, they can overlap. This chapter covers which box is painted on top and how z-index changes that.
Why elements overlap
In normal flow, boxes do not overlap. Each block sits below the previous one, and each inline box sits after the previous one. Overlap happens when you do something that takes a box out of that arrangement:
- Absolute or fixed positioning removes the box from flow, so its siblings ignore it.
- Offsets on a relative or sticky box move the drawn box over its neighbors.
- Negative margins pull a box into its neighbor's space.
- Transforms such as
translate()move the drawn box without changing layout.
When two boxes cover the same pixels, the browser needs a rule to choose the one on top. That rule is the paint order.
The default paint order
Without any z-index, the browser paints in a fixed sequence. Later steps cover earlier steps. Inside one stacking context, the order is:
- The background and borders of the context's root element.
- Descendants with a negative
z-index. - Non-positioned block boxes, in document order.
- Non-positioned floats.
- Non-positioned inline content, such as text.
- Positioned descendants with
z-index: autoor0, in document order. - Positioned descendants with a positive
z-index, from lowest to highest.
The consequence most people notice is step 6. Any positioned element is painted above every non-positioned element, even if the non-positioned element comes later in the HTML. Position alone lifts a box above ordinary content.
Ties are broken by document order
When two positioned elements have the same z-index (including both auto), the one that appears later in the HTML is painted on top. You do not need z-index at all to put one box over another. You only need them to be positioned and in the right order.
<div class="a">A</div>
<div class="b">B</div>
<style>
.a, .b {
position: absolute;
width: 100px;
height: 100px;
}
.a { top: 0; left: 0; background: tomato; }
.b { top: 50px; left: 50px; background: steelblue; }
</style>Here B covers the lower-right quarter of A because B comes later in the source. Moving the .b element before .a in the HTML reverses the result.
Changing the order with z-index
z-index takes an integer, or auto. A higher number is painted above a lower one. Negative numbers are allowed. Add z-index to the earlier example and A moves above B:
<div class="a">A</div>
<div class="b">B</div>
<style>
.a, .b {
position: absolute;
width: 100px;
height: 100px;
}
.a { top: 0; left: 0; background: tomato; z-index: 2; }
.b { top: 50px; left: 50px; background: steelblue; z-index: 1; }
</style>Both boxes still overlap, but A is now on top because 2 is greater than 1. The source order no longer matters between them.
Which elements accept z-index
z-index has an effect on:
- Elements with
positionset torelative,absolute,fixed, orsticky. - Flex items and grid items, even when they are
position: static.
It has no effect on an ordinary static block. This is the most common reason a z-index appears to be ignored.
auto versus 0
z-index: auto and z-index: 0 paint at the same step (step 6 above), so they look the same in most cases. The difference is that 0 creates a new stacking context on a positioned element, while auto does not. Stacking contexts are the subject of the next chapter. For now, treat auto as the default and use 0 only when you want it to behave as a boundary.
Negative z-index and the parent's background
A negative z-index places an element below non-positioned content in the same stacking context. This lets you put a decorative layer behind text without wrapping the text:
<div class="hero">
<h1>Welcome</h1>
</div>
<style>
.hero {
position: relative;
isolation: isolate;
padding: 3rem;
}
.hero::before {
content: "";
position: absolute;
inset: 0;
background: linear-gradient(#cde, #fff);
z-index: -1;
}
</style>The isolation: isolate line on .hero matters. It makes .hero a stacking context, so the -1 layer goes behind the heading but stays in front of .hero's own background and of anything outside .hero. Without it, the pseudo-element could drop behind an ancestor's background further up the page and disappear.
Comparing values only works between siblings in a context
z-index numbers are not global. Two numbers are only compared when both elements belong to the same stacking context. Inside one context, the numbers rank its direct painted layers. Between different contexts, the whole context is ranked as one unit.
Here is a short example of the effect:
<div class="left">
<div class="tooltip">Tooltip</div>
</div>
<div class="right">Panel</div>
<style>
.left, .right {
position: relative;
width: 200px;
height: 100px;
}
.left {
z-index: 1;
background: #fdd;
}
.right {
z-index: 2;
margin-top: -50px;
background: #ddf;
}
.tooltip {
position: absolute;
top: 60px;
z-index: 9999;
}
</style>The tooltip has z-index: 9999, but it is inside .left, which has z-index: 1 and so forms a stacking context. The whole of .left ranks below .right, which has 2. The tooltip is painted behind the panel even though 9999 is larger than 2. The next chapter explains how contexts are created and how to debug this. For now, remember that a big number cannot escape its parent's context.
Keeping z-index values manageable
Projects that pick numbers ad hoc end up with values like 99999, because each new overlay needs to beat the last one. Two habits prevent this.
- Define a small named scale in one place, using custom properties, and refer to the names.
- Keep overlapping elements as close together in the DOM as you can, so that small numbers are enough.
:root {
--z-dropdown: 10;
--z-sticky: 20;
--z-backdrop: 100;
--z-modal: 110;
}
.dropdown { position: absolute; z-index: var(--z-dropdown); }
.site-header { position: sticky; top: 0; z-index: var(--z-sticky); }
.backdrop { position: fixed; inset: 0; z-index: var(--z-backdrop); }
.modal { position: fixed; z-index: var(--z-modal); }Leave gaps between the numbers so you can insert a new layer later without renumbering. The modal is one above the backdrop, which is the same relationship as in a typical modal pattern with values 100 and 101.
Checking the order in the browser
When an overlap looks wrong, work through these steps:
- Confirm that both elements are positioned, or are flex or grid items. If not,
z-indexis ignored. - Compare their
z-indexvalues. If they are equal or bothauto, the later one in the HTML is on top. - Find the nearest ancestor of each that creates a stacking context. If the ancestors differ, compare the ancestors' values instead.
- Use the browser's developer tools. Most have a layers or 3D view, and the Computed pane shows the final
z-indexandposition.
A quick test is to temporarily give the suspect element a bright outline and an extreme z-index. If it still sits behind something, a stacking context boundary is the cause and the number is not.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| `z-index` has no effect | Element is `position: static` and not a flex or grid item | Add `position: relative` |
| Huge value still loses | An ancestor with a lower value forms a stacking context | Raise the ancestor, or move the element out of it |
| Element on top despite lower number | Elements are in different contexts and the contexts rank the other way | Compare the ancestors' values |
| Layer with `z-index: -1` disappears | Ancestor background paints over it | Add `isolation: isolate` to the intended parent |
| Overlap order flips when HTML is reordered | Both have equal `z-index`, so source order decides | Set explicit values |
Exercise
A page has <div class="a"> followed by <div class="b">. Both are position: absolute, overlap, and have no z-index set. Which one is on top, and what is the smallest change that puts the other one on top without editing the HTML?
Show answer
.b is on top because it comes later in the source and both have z-index: auto. Give .a a z-index of 1 (for example .a { z-index: 1; }). A positive value paints above the auto layer, so .a moves above .b.
Exercise
A tooltip with z-index: 9999 is hidden behind a neighboring panel with z-index: 2. The tooltip's parent has position: relative; z-index: 1. Why does the tooltip lose, and how can you fix it?
Show answer
The parent has a z-index on a positioned element, so it forms a stacking context. The tooltip's 9999 is only compared with layers inside that parent. The parent as a whole ranks 1, which is below the panel's 2. To fix it, raise the parent's z-index above the panel's, remove z-index from the parent so it no longer creates a context, or move the tooltip outside the parent.
Exercise
You add z-index: 5 to a <p> that has no position value and sits in normal block flow. The paragraph still appears under a positioned <div> that overlaps it. Why?
Show answer
A static element that is not a flex or grid item ignores z-index. The paragraph is painted in the non-positioned block layer, which is below all positioned elements. Setting position: relative on the paragraph makes the z-index count.