Sub topic 9
Overlapping elements and z-index
This chapter assumes you know basic HTML and CSS selectors, the box model, and how position and the offset properties (top, left, and so on) place an element. Here you will learn how the browser decides which of several overlapping elements is drawn on top, and how z-index changes that.
Default paint order
When elements overlap, the browser paints them in a fixed order. For elements in the same stacking context (the next chapter covers what that means), the order from back to front is:
- The background and borders of the context's root element.
- Positioned descendants with a negative
z-index. - Non-positioned block elements, 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 value to highest.
Two points follow from this list. First, any positioned element is painted above any non-positioned element, even if the non-positioned one comes later in the HTML. Second, when two positioned elements have the same z-index (or both use auto), the one that appears later in the HTML is painted on top.
The z-index property
z-index takes an integer, which can be negative, or the keyword auto. A higher number is painted in front of a lower one. The default is auto, which behaves like 0 for ordering purposes.
z-index applies only to certain elements. On a normal block with position: static, it has no effect. It works on elements that are positioned (any value other than static). It also works on flex and grid items, even when they are not positioned.
<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 overlap in a 50px by 50px square. Box A has the higher z-index, so it is painted on top of box B. If you removed both z-index lines, box B would win, because it comes later in the HTML.
Choosing z-index values
A z-index value only matters relative to other elements in the same stacking context. The number itself carries no meaning, so 2 over 1 gives the same result as 9999 over 1. Large numbers give you no extra power and make later changes harder.
A practical habit is to keep values small and, in larger projects, to name them in one place:
:root {
--z-header: 10;
--z-dropdown: 20;
--z-modal: 30;
}
.site-header { position: sticky; top: 0; z-index: var(--z-header); }
.modal { position: fixed; z-index: var(--z-modal); }Negative values place an element behind its non-positioned siblings. This is useful for decorative backgrounds, but the element can then disappear behind a parent's background if the parent creates no stacking context of its own.
Common mistakes
- Setting
z-indexon aposition: staticelement. It is ignored. - Giving two elements the same value and expecting a particular winner. HTML order decides.
- Escalating values (
999, then9999, then99999) instead of finding why the first value failed. - Expecting a child to appear above an element outside its parent when the parent has a lower
z-indexthan that element.
Exercise
Two absolutely positioned boxes overlap. Box X comes first in the HTML with z-index: 5. Box Y comes second with z-index: auto. Which box is painted on top, and what changes if you remove X's z-index?
Show answer
Box X is on top, because 5 is higher than auto (which counts as 0). If you remove X's z-index, both boxes tie, so Y is painted on top because it appears later in the HTML.
Exercise
A paragraph has z-index: 10 and position: static, and it overlaps a positioned box with no z-index. Which one is on top and why?
Show answer
The positioned box is on top. z-index has no effect on a static, non-flex, non-grid element, so the paragraph is painted in the non-positioned layer, which sits below positioned elements.