Sub topic 12
Pitfalls and debugging checklist
This chapter assumes you know the five position values, offsets, containing blocks, and the basics of z-index from the earlier chapters. It collects the failures that come up most often and gives a fixed order for checking them.
Sticky that never sticks
position: sticky needs three things, and a missing one fails silently.
- An offset such as
top: 0. Without one, the element behaves likerelative. - A parent tall enough to scroll through. A sticky element stays inside its parent and leaves with it.
- No ancestor with
overflow: hidden,auto, orscroll. That ancestor becomes the scroll container, and the element sticks to it instead of the viewport.
<div class="wrapper">
<header class="site-header">Header</header>
<main style="height: 2000px">Long content</main>
</div>
<style>
.wrapper {
overflow: hidden; /* this breaks sticky */
}
.site-header {
position: sticky;
top: 0;
background: white;
}
</style>Remove overflow: hidden from .wrapper and the header sticks to the viewport. If you need clipping, use overflow: clip, which does not create a scroll container.
z-index that seems ignored
Two causes cover most cases.
- The element is
position: static.z-indexapplies only to positioned elements and to flex or grid items. Addposition: relativeif you do not want to move the element. - A parent stacking context limits it. A child cannot rise above an element outside its own stacking context, whatever its number. Raising
z-index: 9999on the child changes nothing. Raise the parent's value, or move the element out of that parent.
.sidebar { position: relative; z-index: 1; }
.sidebar .tooltip { position: absolute; z-index: 9999; }
.content { position: relative; z-index: 2; }Here the tooltip is trapped inside .sidebar, which sits below .content. The tooltip is painted under the content. Giving .sidebar a z-index above 2 fixes it.
Absolute elements in the wrong place
An absolute element looks for the nearest positioned ancestor. If none exists, it uses the initial containing block, which is roughly the first screen of the page. A badge that jumps to the page corner usually means its card lacks position: relative.
An absolute element also leaves the flow. A parent that contains only absolute children collapses to zero height. Give the parent an explicit height or reconsider whether absolute is the right tool.
Fixed that behaves like absolute
position: fixed is normally relative to the viewport. If any ancestor has transform, perspective, or filter set to a value other than none, that ancestor becomes the containing block instead. A modal inside an animated, transformed wrapper then scrolls with the page.
A debugging order
Work through the checks in this order. It moves from the cheapest to the most subtle.
In the browser's developer tools:
- Select the element and read the computed
positionvalue, not the one you intended. - Walk up the DOM tree and look for positioned ancestors,
overflow,transform,filter, andopacitybelow 1. - Toggle properties off one at a time, and change only one per test.
- Temporarily add
outline: 2px solid redto the element and its suspects to see their real boxes.
Exercise
A sticky table header has position: sticky and top: 0, and its parent is taller than the viewport. It still scrolls away. Name the most likely cause and one way to find it.
Show answer
An ancestor probably has overflow: hidden, auto, or scroll, which makes it the scroll container the header sticks to. In developer tools, walk up the ancestors and check each one's computed overflow. Remove or replace it, for example with overflow: clip, and the header sticks to the viewport.