Sub topic 12
Pitfalls and debugging checklist
This chapter assumes you know the position values (static, relative, absolute, fixed, sticky), the offset properties, containing blocks, and the basics of z-index. Here you will learn the failures that come up most often and a repeatable way to find the cause of each.
Most positioning bugs come down to three questions:
- Which box is this element positioned against (its containing block, or for sticky its scroll container)?
- Is the element still in the flow, or did it leave it?
- Which stacking context is it painted in?
Start with inspection, not guessing
Open your browser's developer tools and select the misbehaving element. Check these things before you change any CSS:
- The computed value of
position. A typo or a more specific selector may have overridden your rule. - The box overlay. Hovering the element in the Elements panel highlights its margin, border, padding, and content boxes.
- The ancestor chain. Walk upward and look for anything with
position,transform,filter,overflow, orz-indexset.
A quick way to see every box on a page is a temporary outline. Outlines take no space, so they do not change the layout you are debugging.
* {
outline: 1px solid rgba(255, 0, 0, 0.4);
}Remove it when you are done.
Pitfall 1: the absolute element lands in the wrong place
An absolutely positioned element is placed against its nearest positioned ancestor, meaning one whose position is anything except static. If no ancestor qualifies, the browser uses the initial containing block, which is roughly the first viewport-sized area of the page. The badge then ends up in the corner of the page instead of the corner of the card.
<div class="card">
<span class="badge">New</span>
<p>Card content</p>
</div>
<style>
.card {
padding: 1rem;
border: 1px solid #ccc;
/* missing: position: relative; */
}
.badge {
position: absolute;
top: 0;
right: 0;
}
</style>The fix is one line: add position: relative; to .card. Relative positioning with no offsets moves nothing, so it is the standard way to make an element an anchor.
When the badge is in the wrong place, walk up the DOM and ask which ancestor you intended as the anchor. Then check whether that ancestor is positioned. Also check whether a different, closer ancestor is positioned by accident.
Pitfall 2: the parent collapses
An absolutely or fixed positioned element is removed from the flow. Its parent no longer counts it when computing height, and siblings behave as if it were not there.
Two symptoms are common:
- A container holding only an absolutely positioned child collapses to zero height.
- Following content slides underneath the positioned element.
If the element should still take up space, it probably should not be absolute. Use relative (which keeps its original space) or a normal-flow layout. If it really must be out of flow, reserve the space with padding or a fixed height on the parent.
Pitfall 3: offsets do not do what you expect
topandbottomboth set on a relatively positioned element:topwins. Forleftandrightin a left-to-right language,leftwins.- On an absolutely positioned element with
topandbottomboth set andheight: auto, the box stretches between them. If you set an explicit height as well, the box is over-constrained andbottomis ignored. - Percentage offsets resolve against the containing block's size, not the element's own size.
top: 50%moves the top edge to the middle of the containing block. That is why centering usestransform: translate(-50%, -50%), where percentages refer to the element itself. - A percentage
tophas nothing to resolve against if the containing block hasheight: autoand no content-driven height yet. The offset may then behave as if it wereauto.
Pitfall 4: sticky does not stick
Sticky positioning is the most common source of "it does nothing" reports. Go through these causes in order.
- No offset is set.
position: stickyalone has no threshold to stick at. You need at least one oftop,bottom,left,right, orinset. - An ancestor creates a scroll container. Sticky elements stick relative to their nearest ancestor with
overflowset tohidden,auto, orscroll. If that ancestor does not itself scroll, the element never moves. - The parent is too short. A sticky element only stays stuck while its parent is in view. If the parent is the same height as the element, there is no room to travel.
- Flex or grid stretching. In a flex row, items stretch to the row height by default. A stretched sticky sidebar fills its container, so there is nothing to scroll past.
Here is the overflow case:
<div class="page">
<header class="bar">Section title</header>
<p>Long content goes here. Repeat this paragraph many times so the page scrolls.</p>
</div>
<style>
.page {
overflow: hidden; /* makes .page the scroll container, and it never scrolls */
}
.bar {
position: sticky;
top: 0;
background: white;
}
</style>Remove overflow: hidden from .page. If you need the clipping, use overflow: clip instead. It clips like hidden but does not create a scroll container, so sticky still works.
The flex case needs one declaration:
.layout {
display: flex;
align-items: flex-start;
}
.sidebar {
position: sticky;
top: 1rem;
align-self: flex-start; /* stops the sidebar stretching to full row height */
}Pitfall 5: z-index is ignored
z-index only applies to elements that take part in stacking. These work:
- Positioned elements (
relative,absolute,fixed,sticky). - Flex and grid items, even when
positionisstatic.
A plain static block ignores z-index. The usual fix is to add position: relative;.
If the element is positioned and z-index still seems to do nothing, you are almost certainly dealing with a stacking context.
Pitfall 6: a huge z-index loses to a small one
A stacking context is a self-contained layer group. Children are ordered against each other inside it, and the whole group is then ordered as a single unit against its siblings. A child can never escape its parent's context, so z-index: 9999 inside a low-ranked context still loses to a higher-ranked sibling context.
<div class="panel">
<div class="tooltip">Tooltip</div>
</div>
<div class="other">Other content</div>
<style>
.panel {
position: relative;
z-index: 1;
height: 60px;
background: #eee;
}
.tooltip {
position: absolute;
top: 40px;
height: 60px;
z-index: 9999;
background: gold;
}
.other {
position: relative;
z-index: 2;
height: 80px;
background: lightblue;
}
</style>The tooltip hangs below .panel and overlaps .other. Because .panel forms a context at level 1 and .other sits at level 2, the entire panel, tooltip included, is painted under .other. The tooltip's own 9999 only matters among the panel's descendants.
The following properties create a new stacking context, and each one can cause this problem without you noticing:
| Trigger | Example |
|---|---|
| Positioned element with a `z-index` other than `auto` | `position: relative; z-index: 1` |
| `position: fixed` or `sticky` | always creates one |
| `opacity` below 1 | `opacity: 0.99` |
| `transform`, `filter`, `perspective`, `clip-path` | `transform: translateZ(0)` |
| `isolation: isolate` | the explicit, intentional option |
| `will-change` naming a property that would create one | `will-change: transform` |
| Flex or grid item with a `z-index` other than `auto` | `z-index: 0` |
To fix the example, either move the tooltip out of .panel, or raise .panel above .other with z-index: 3. For a lasting solution, create contexts on purpose. Put isolation: isolate on components so their internal z-index values cannot leak out, and keep a short, named scale of z-index values for page-level layers such as header, dropdown, and modal.
Pitfall 7: fixed stops being fixed
A position: fixed element is normally positioned against the viewport. If any ancestor has transform, perspective, filter, or will-change naming one of those, that ancestor becomes the containing block instead. The "fixed" element then scrolls with the ancestor and its offsets are measured from the ancestor's edges.
.page-wrapper {
transform: translateZ(0); /* a leftover performance hack */
}
.toolbar {
position: fixed;
top: 0;
left: 0;
right: 0; /* now relative to .page-wrapper, not the viewport */
}If a modal or toolbar is clipped, offset, or scrolls away, look for these properties on its ancestors. Better still, render modals as direct children of <body> so no ancestor can interfere. This also sidesteps most stacking context problems.
Pitfall 8: clipping and hidden content
An element with overflow: hidden, auto, or scroll clips any descendant whose containing block is that element or a descendant of it. A dropdown that is absolutely positioned inside a clipped card gets cut off for this reason. Two fixes:
- Make the dropdown's containing block an ancestor above the clipping element. An absolute element whose containing block is outside the clipping element is not clipped by it.
- Render the dropdown elsewhere in the DOM, such as a direct child of
<body>.
A fixed header also hides the top of anchor-link targets, because the browser scrolls the target to the very top of the viewport. Fix that with scroll margin on the targets:
:target,
h2[id] {
scroll-margin-top: 4rem; /* roughly the header height */
}Debugging checklist
Work through this list top to bottom. Each row names a symptom and the first thing to check.
| Symptom | First check |
|---|---|
| Nothing moves | Is `position` actually `static` in the computed styles? Is another rule overriding it? |
| Absolute element in the wrong corner | Does the intended ancestor have a non-static `position`? Is a closer ancestor positioned by accident? |
| Parent has zero height | Is the only child absolute or fixed? |
| Sticky never sticks | Is an offset set? Does an ancestor have `overflow` other than `visible`? Is the parent tall enough? Is the item stretched in a flex row? |
| `z-index` has no effect | Is the element positioned or a flex or grid item? |
| High `z-index` still loses | Which stacking context is each element in? Walk up looking for the triggers in the table. |
| Fixed element scrolls | Does an ancestor have `transform`, `filter`, or `perspective`? |
| Content is cut off | Does an ancestor between the element and its containing block clip with `overflow`? |
Exercise
A sticky sidebar inside <div class="layout"> (a flex container) has position: sticky; top: 0; and its parent is much taller than the viewport, but the sidebar never moves while you scroll. Name two likely causes and the fix for each.
Show answer
First, flex items stretch by default (align-items: stretch), so the sidebar is as tall as the row and has no room to move. Fix it with align-self: flex-start on the sidebar. Second, an ancestor may have overflow set to hidden, auto, or scroll, which makes that ancestor the scroll container for sticky. Fix it by removing that overflow or replacing it with overflow: clip.
Exercise
A dropdown menu has z-index: 5000 but still appears underneath a card that has z-index: 2. Both are positioned. The dropdown's parent has opacity: 0.95. Explain why and give one fix.
Show answer
opacity below 1 creates a stacking context on the dropdown's parent. The dropdown's 5000 is only compared against other elements inside that context, and the whole parent group is ordered against the card as a single layer. Because the parent has no z-index above the card's 2, it is painted underneath. Fixes: remove the opacity from the parent, move the dropdown out of that parent (for example to <body>), or give the parent a position and a z-index higher than the card's.
Exercise
A fixed footer bar works on most pages, but on one page it scrolls with the content. What is the first thing to look for?
Show answer
Look for an ancestor of the footer with transform, filter, perspective, or a matching will-change value. Such an ancestor becomes the containing block for the fixed element, so the footer is positioned against it instead of the viewport.