Sticky headers and why z-index fails in CSS
30 Sticky headings, and z-index that does nothing
hardMake a heading stick to the top of a scrolling panel. Then stack two overlapping boxes, and include one that asks for a z-index and is ignored.
Stuck? Three hints, smallest first
- 1.
position: stickyneeds a threshold —top: 0— or it never sticks. - 2. It sticks within its own scrolling ancestor, so that ancestor needs to be the thing that scrolls.
- 3.
z-indexapplies only to a positioned element or a flex or grid child. On a plain static block it is ignored.
<div class="scroller">
<h2 class="stuck">Departures</h2>
<p>06:30 Abuja</p>
<p>08:00 Kano</p>
<p>11:15 Enugu</p>
<p>14:45 Ibadan</p>
<p>17:00 Benin</p>
<p>19:30 Warri</p>
</div>
<div class="stack">
<div class="layer under">z-index: 1</div>
<div class="layer over">z-index: 2</div>
<div class="layer inert">z-index: 5, and ignored</div>
</div>
.scroller {
height: 130px;
overflow-y: auto; /* this is the scrolling ancestor sticky works within */
border: 1px solid #c9cad1;
border-radius: 8px;
padding: 0 10px 10px;
}
.stuck {
position: sticky;
top: 0; /* the threshold; without it, nothing sticks */
margin: 0 -10px 8px;
padding: 8px 10px;
background: #17171a;
color: #ffffff;
font-size: 0.9rem;
}
.scroller p { margin: 0 0 6px; font-size: 0.85rem; }
.stack {
position: relative;
height: 90px;
margin-top: 12px;
}
.layer {
padding: 8px 10px;
border-radius: 6px;
font-size: 0.8rem;
color: #ffffff;
}
.under {
position: absolute;
top: 0; left: 0;
z-index: 1;
width: 150px;
background: #0b3a8f;
}
.over {
position: absolute;
top: 22px; left: 40px;
z-index: 2;
width: 150px;
background: #a3341f;
}
/* No position, so the z-index below is thrown away. Left in on purpose. */
.inert {
z-index: 5;
width: 200px;
margin-top: 56px;
background: #17734b;
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
const inertRule = (/\.inert\s*\{([^}]*)\}/.exec(css) || [, ''])[1];
test('the heading is sticky', /position:\s*sticky/.test(css), true);
test('and has a threshold to stick at', /top:\s*0/.test(css), true);
test('its ancestor is the thing that scrolls', /overflow-y:\s*auto/.test(css), true);
test('the ancestor has a height, or there is nothing to scroll', /height:\s*130px/.test(css), true);
test('the two stacked boxes are positioned, so z-index applies', (css.match(/position:\s*absolute/g) || []).length, 2);
test('the third asks for a z-index of 5', /z-index:\s*5/.test(inertRule), true);
test('but declares no position, so it is ignored', /position:/.test(inertRule), false);
test('overflow: hidden is avoided, since it would break sticky', /overflow[^;]*hidden/.test(css), false);
Output
Three ways to make sticky silently do nothing: forget the threshold, put it in a container that does not scroll, or give an ancestor overflow: hidden, which creates a scroll container with no scrollbar and pins the element to a box it can never leave. The last two checks are the other lesson: z-index: 5 on a static element is discarded, so the box painted last wins regardless. z-index only has meaning for a positioned element or a flex or grid child, which is why so many “my z-index is not working” bugs are really missing position: relative.