Debugging layout problems
Overview
Every developer writes CSS that does something unexpected. The difference between a frustrating hour and a quick fix is having a method — and knowing the handful of causes behind most layout bugs. This lesson gives you both.
A method
1. Find the element
The bug is rarely where it appears. A gap might be a margin on the element above; a page that scrolls sideways is caused by one element somewhere inside. Right-click → Inspect, then hover over elements in the Elements panel to see their boxes highlighted on the page.
To find what’s causing horizontal scrolling, paste this into the console:
[...document.querySelectorAll('*')].filter(el => el.getBoundingClientRect().right > innerWidth)Or temporarily outline every box: * { outline: 1px solid red; }.
2. Check the box
The Computed tab shows the final box model: the actual width, padding, border and margin. Compare them with what you expected. Is box-sizing border-box? Is a margin collapsing (Margins and collapsing)?
3. Check the parent’s layout mode
The same property means different things in different layout modes. width: 50% on a flex item is a flex-basis suggestion that can shrink; margin: auto centres horizontally in block layout but in both directions in flex. Check what display the parent has — DevTools shows flex and grid badges next to containers; click them for an overlay of lines and gaps.
4. Check the rules
In the Styles tab, look for your rule:
- Crossed out: something more specific, later, or in a later cascade layer wins. The winning rule is shown above it.
- Warning icon: the value is invalid (a typo, a missing unit) and the whole declaration is ignored.
- Greyed out with an info icon: the property is valid but has no effect in this context — like
justify-contenton a non-flex element. Hover the icon for the reason. - Not there at all: the selector doesn’t match, or the stylesheet didn’t load.
5. Find the overflow
Most “the layout is broken” reports are overflow: something wider than its container. Suspects: fixed widths, long words and URLs, images without max-inline-size: 100%, white-space: nowrap, and flex or grid items that refuse to shrink.
6. Simplify
Toggle declarations off with their checkboxes in DevTools until the bug disappears — the last one you turned off is involved. For stubborn bugs, rebuild the smallest example that still fails in a blank page. Half the time, you find the cause while doing it.
The ten usual suspects
| Symptom | Likely cause | Fix |
|---|---|---|
Grid column grows to fit a <pre>, table or long word |
1fr has a minimum of auto |
minmax(0, 1fr) |
| Flex item won’t shrink; ellipsis never shows | flex items have min-width: auto |
min-inline-size: 0 on the item |
| Sticky element doesn’t stick | an ancestor has overflow: hidden/auto/scroll, or no top is set, or the item stretches to its container’s height |
overflow: clip; set top; align-self: start |
z-index: 9999 doesn’t bring it to the front |
its parent forms a stacking context that’s below another one | raise the parent’s context, or use isolation/the top layer (popover, dialog) |
z-index does nothing at all |
the element isn’t positioned, a flex item, or a grid item | add position: relative |
height: 100% does nothing |
the parent’s height isn’t definite (auto or only min-height) |
make the parent flex or grid and let the child stretch |
| Unexpected gap above a section | margin collapsing through the parent | display: flow-root on the parent, or use gap / padding |
| Gap under an image | images are inline and sit on the text baseline | display: block on the image |
position: fixed element scrolls with the page |
an ancestor has a transform, filter, or contain: layout/paint, which creates a containing block for it |
move the element out of that ancestor (or use the top layer) |
| Everything is a few pixels off | box-sizing: content-box |
*, *::before, *::after { box-sizing: border-box; } |
You’ve met each of these in earlier lessons — this table is the cheat sheet. Each one in a little more detail:
Grid blowout
1fr is shorthand for minmax(auto, 1fr), and auto as a minimum means “at least as wide as the content’s min-content width”. A code block or a long URL can force the column — and the page — wider than the screen. minmax(0, 1fr) removes the minimum; the content then overflows inside its own box, where you can give it overflow-x: auto.
Flex items that won’t shrink
Flex items have min-width: auto (min-inline-size: auto) by default, which stops them shrinking below their content’s width. For text-overflow ellipsis, long URLs or nested flex rows, set min-inline-size: 0 on the flex item.
One subtlety: a flex item that itself has overflow other than visible already gets a minimum of 0, so an ellipsis set directly on the flex item usually works. The bug appears when the truncated text is inside the flex item — a wrapper holding a label and a URL, say — because the wrapper still has the automatic minimum. Put min-inline-size: 0 on the wrapper, the element that is actually the flex item.
Sticky that won’t stick
A sticky element sticks relative to its nearest scrolling ancestor. Any ancestor with overflow: hidden, auto or scroll becomes that ancestor — even if it never actually scrolls. Use overflow: clip to hide overflow without creating a scroll container. Also check that an inset (top) is set and that the element isn’t stretched to the full height of its parent.
z-index stuck behind
z-index values only compete with siblings inside the same stacking context. If a parent has z-index: 1 (or opacity < 1, transform, filter, isolation: isolate…), everything inside it is painted as one layer at level 1. A child with z-index: 100 can’t escape it. Raise the parent instead, or render the overlay in the top layer with the Popover API or <dialog>.
Percentage heights
height: 100% resolves against the parent’s height — but only if that height is definite. A parent sized by its content (auto) or with only a min-height doesn’t count, so the percentage is ignored. Making the parent a flex or grid container lets the child stretch to the parent’s full height instead.
DevTools features worth knowing
- Flex and grid overlays: click the badge next to a container to show its lines, gaps, track sizes and area names. You can show several at once.
- Computed → Layout: lists every flex and grid container on the page.
- The box model diagram in the Computed tab: click and edit values directly.
- Rendering panel: emulate
prefers-reduced-motion,prefers-color-schemeandforced-colors. - Device mode: test widths, touch and pixel density; but finish on real devices.
When stuck: describe the problem as “this box is this size but should be that size because…”. Saying which box and why usually points straight at the cause.
Five bugs, one page
The starter page has five layout bugs, each marked with a comment describing the symptom. Fix each with the smallest change, and add a comment naming the cause: (1) the code block pushes the main column far wider than the screen; (2) the long URL pushes the Copy button out of the 18rem share box instead of being truncated with an ellipsis; (3) the sticky header doesn’t stick; (4) the dropdown appears behind the next section even with z-index: 100; (5) the .full panel isn’t full height. Don’t change the HTML.
<div class="scroll-area">
<header class="bar">Sticky header</header>
<div class="layout">
<aside>Sidebar</aside>
<main>
<pre>const veryLongLineOfCode = someFunctionWithALongName(argumentNumberOne, argumentNumberTwo, argumentNumberThree);</pre>
<div class="share"><div class="link"><span class="label">Share link</span><span class="url">https://academy.example.com/courses/css-layout/lessons/debugging-layout-problems</span></div><button>Copy</button></div>
</main>
</div>
<section class="menu-wrap"><div class="dropdown">Dropdown</div></section>
<section class="next">Next section</section>
<div class="panel-wrap"><div class="full">Full-height panel</div></div>
<p style="block-size: 150vh">Scroll down…</p>
</div>
<style>
* { box-sizing: border-box; }
body { margin: 0; }
.scroll-area { overflow: hidden; }
/* Bug 3: the header scrolls away instead of sticking */
.bar { position: sticky; top: 0; background: #16181d; color: white; padding: 1rem; }
/* Bug 1: on narrow screens the <pre> pushes the main column far past the right edge */
.layout { display: grid; grid-template-columns: 12rem 1fr; gap: 1rem; }
pre { background: #f7f7f5; padding: 1rem; overflow-x: auto; }
/* Bug 2: the button is pushed out of the card */
.share { display: flex; gap: 0.5rem; inline-size: 18rem; margin-block-start: 1rem; border: 1px solid #ccc; padding: 0.5rem; }
.url { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.share button { flex: none; }
/* Bug 4: the dropdown is hidden behind .next */
.menu-wrap { position: relative; z-index: 1; block-size: 3rem; }
.dropdown { position: absolute; top: 2rem; z-index: 100; background: #f1ecfd; block-size: 6rem; inline-size: 10rem; }
.next { position: relative; z-index: 2; background: #eaf0fc; block-size: 6rem; }
/* Bug 5: the panel should fill the 20rem wrapper's height */
.panel-wrap { min-block-size: 20rem; display: block; }
.full { block-size: 100%; background: #e6f6ec; }
</style><div class="scroll-area">
<header class="bar">Sticky header</header>
<div class="layout">
<aside>Sidebar</aside>
<main>
<pre>const veryLongLineOfCode = someFunctionWithALongName(argumentNumberOne, argumentNumberTwo, argumentNumberThree);</pre>
<div class="share"><div class="link"><span class="label">Share link</span><span class="url">https://academy.example.com/courses/css-layout/lessons/debugging-layout-problems</span></div><button>Copy</button></div>
</main>
</div>
<section class="menu-wrap"><div class="dropdown">Dropdown</div></section>
<section class="next">Next section</section>
<div class="panel-wrap"><div class="full">Full-height panel</div></div>
<p style="block-size: 150vh">Scroll down…</p>
</div>
<style>
* { box-sizing: border-box; }
body { margin: 0; }
/* Bug 3 cause: overflow: hidden makes .scroll-area the sticky header's
scroll container, and it never scrolls. clip hides overflow without
creating a scroll container. */
.scroll-area { overflow: clip; }
.bar { position: sticky; top: 0; background: #16181d; color: white; padding: 1rem; }
/* Bug 1 cause: 1fr has a minimum of auto (the min-content width of the
<pre>), so the track grows to fit the code. minmax(0, 1fr) lets it shrink. */
.layout { display: grid; grid-template-columns: 12rem minmax(0, 1fr); gap: 1rem; }
pre { background: #f7f7f5; padding: 1rem; overflow-x: auto; }
/* Bug 2 cause: .link is a flex item with min-width: auto, so it can't
shrink below the URL's full text width and the ellipsis never kicks in. */
.share { display: flex; gap: 0.5rem; inline-size: 18rem; margin-block-start: 1rem; border: 1px solid #ccc; padding: 0.5rem; }
.link { min-inline-size: 0; }
.url { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.share button { flex: none; }
/* Bug 4 cause: .menu-wrap creates a stacking context with z-index: 1, so
the dropdown's z-index: 100 only counts inside it; .next (z-index: 2)
is above the whole of .menu-wrap. Raise the context, not the child. */
.menu-wrap { position: relative; z-index: 3; block-size: 3rem; }
.dropdown { position: absolute; top: 2rem; z-index: 100; background: #f1ecfd; block-size: 6rem; inline-size: 10rem; }
.next { position: relative; z-index: 2; background: #eaf0fc; block-size: 6rem; }
/* Bug 5 cause: a percentage height needs a parent with a definite height;
min-block-size isn't one. In a grid, the item stretches to fill instead. */
.panel-wrap { min-block-size: 20rem; display: grid; }
.full { background: #e6f6ec; }
</style>Summary
- Debug systematically: find the misbehaving box, check its size, its parent’s layout mode, the rules that apply, and overflow.
- Grid blowouts come from
1fr(minimum auto); useminmax(0, 1fr). Flex overflow comes frommin-width: auto; usemin-inline-size: 0. - Sticky fails when an ancestor has
overflow: hidden/auto/scroll, when there’s no inset liketop, or when the item stretches. - A z-index only competes within its stacking context — raise the context, not the child.
- Percentage heights need a parent with a definite height; flex and grid stretching are usually the better fix.