CSS

Debug CSS Layout Problems Fast with Outlines and Overlays

Outline every block, find the element causing horizontal scroll, see stacking order, and inspect flex and grid containers. A visual approach to the layout bugs that DevTools makes slow.

9 min read

Most layout bugs are invisible. An element is a few pixels wider than its parent, a wrapper has unexpected padding, a modal sits under a sticky header. You can find them one element at a time in DevTools, or you can make the whole layout visible at once and let the problem jump out.

This guide is about the second approach: outlines and overlays that turn an invisible structure into something you can see.

The one-line trick, and why it's not enough

Every front-end developer has pasted this into the Styles pane at some point:

css
* { outline: 1px solid rgb(255 0 0 / 0.4); }
outline doesn't take up space, so unlike border it won't change the layout you're debugging.

It works, but every element gets the same color, so nested boxes blur together, and it's gone the moment you reload. Targeted outlines answer specific questions faster.

Ask the layout a specific question

Six layout questions and the outline or overlay that answers each one
Pick the overlay that matches the symptom instead of outlining everything.
SymptomOverlay to use (Web Developer Tools)What to look for
Page scrolls sideways on mobileOverflow Element DetectorThe highlighted element wider than the viewport
Something won't appear on topHighlight High Z-Index, Display Stack LevelsWhich elements create stacking contexts and at what level
Spacing looks unevenOutline Block Elements, Spacing InspectorMargins collapsing or padding you didn't expect
A sticky header covers contentSticky Element Viewer, Fixed Element ViewerWhich elements are taken out of normal flow
Flex items won't line upFlexbox InspectorContainer direction, wrapping, gaps and item sizes
Grid tracks look wrongCSS Grid InspectorTrack lines, areas and gaps

Find the element causing horizontal scroll

The classic mobile bug. The page is 375px wide but something is 380px, so everything wobbles sideways. To find it by hand, run this in the Console:

js
const vw = document.documentElement.clientWidth;
[...document.querySelectorAll("*")]
  .filter((el) => el.getBoundingClientRect().right > vw + 1)
  .forEach((el) => {
    el.style.outline = "2px solid magenta";
    console.log(el);
  });
Outlines every element that extends past the right edge. The deepest one is usually the culprit.

Usual suspects: an image without max-width: 100%, a long URL or word that can't wrap (overflow-wrap: anywhere fixes it), a 100vw width that ignores the scrollbar, or negative margins on a full-bleed section. The Overflow Element Detector does the same check in one click.

Untangle z-index and stacking contexts

z-index: 9999 not working is almost never about the number. A new stacking context is created by position with a z-index, but also by opacity below 1, transform, filter, isolation: isolate and several other properties. A child can never escape its parent's stacking context, however high its z-index.

  1. 1

    Show the stack

    Use Highlight High Z-Index or Display Stack Levels to see which elements have a z-index and where contexts start.

  2. 2

    Find the parent context

    Walk up from the element that's hidden. The first ancestor with a transform, opacity or z-index is the ceiling.

  3. 3

    Fix at the right level

    Raise the ancestor, move the element out (a portal for modals), or remove the property that created the context.

Inspect flex and grid containers

Chrome and Firefox both show a small flex or grid badge next to containers in the Elements panel; click it to draw an overlay with lines, gaps and areas. The Flexbox and CSS Grid Inspectors in Web Developer Tools list every flex and grid container on the page at once, which is useful when you don't yet know which wrapper is responsible.

Strip styles to find the culprit

When you can't tell which stylesheet is responsible, remove them in layers. Disable Linked Stylesheets shows the page without external CSS, Disable Inline Styles removes style attributes, and Disable All Styles shows the raw document. If the bug disappears at one step, you know where to look. CSS Specificity Viewer and Highlight !important Rules then show which rules are winning.

Web Developer Tools CSS category with tools such as Disable All Styles and View CSS
The CSS category: disable styles in layers, view every stylesheet, and inspect specificity and !important rules.

For bugs that come down to the cascade rather than layout, the CSS debugging guide covers specificity and inheritance. To check the fix at every width, follow how to test responsive design, and to check exact sizes, read how to measure elements on a web page.

Related reading

All 25 articles