Paweł Krystkiewicz
All articles

4 min read

The nuclear option: debugging broken layouts with CSS outlines

When a layout breaks, I outline every element to see its boundaries. One CSS rule shows the problem in seconds.

When Layouts Break

You know the situation: a layout that worked perfectly yesterday is now completely broken. Elements are overlapping, margins have gone rogue, or something is mysteriously pushing everything 200px to the right. You've tried inspecting elements, toggling CSS properties, checking z-indexes, but you still can't figure out what's causing the issue.

Time to bring out the nuclear option.

The Solution

Add this CSS as a new rule in DevTools (the Styles tab in Chrome, the Rules view in Firefox), put it in a temporary stylesheet, or use the bookmarklet further down. The console won't take it: CSS is not JavaScript, so it's a syntax error there.

css
* {
  background: #000 !important;
  color: #0f0 !important;
  outline: solid #f00 1px !important;
}

Boom. Every single element on the page now has:

  • Black background
  • Green text
  • Red 1px outline

Why This Works

Instant visualization: The red outlines make every element's boundaries visible. That mysterious invisible div pushing everything? Now you can see it.

Outline, not border: An outline takes no space in the layout. A 1px border on every element changes box sizes and moves the very thing you are debugging. An outline is painted on top and shifts nothing.

Background reveals overlap: For positioned or z-indexed elements, the opaque background of the upper element covers the outline of the one below, so you see which one is on top. In-flow overlap (negative margins, say) doesn't work like this: both outlines stay visible either way.

Text stays readable: Green on black ensures you can still read content while debugging.

!important beats normal rules: It cuts through specificity wars and inline styles that might be hiding the problem. It still loses to an !important that is more specific, comes later in the cascade, or sits in a cascade layer (which is where Tailwind 4 puts its ! utilities).

What You'll Discover

This technique instantly reveals:

  • Ghost elements: Empty divs or containers you forgot about
  • Unexpected dimensions: Elements that are way bigger than their visible content
  • Margin/padding issues: Gaps between outlines. The outline follows the border box, so margin and padding both show up as empty space and look the same. The box model view in DevTools tells you which is which
  • Positioning problems: Absolutely positioned elements breaking out of their containers
  • Overflow issues: Content escaping its bounds
  • Z-index stacking: Which positioned elements are actually on top

Practical Example

Say you have this broken layout where content is mysteriously shifted:

html
<div class="container">
  <div class="sidebar">Sidebar</div>
  <div class="content">Main content</div>
</div>

Apply the outline CSS and you might discover:

  • The sidebar has 200px of right margin you didn't know about
  • There's an empty <div> with width: 100px between sidebar and content
  • The container has unexpected padding
  • A child element has position: absolute with weird offsets

Without the outlines, these issues are invisible. With them, they're obvious.

Browser DevTools Alternative

Most browsers have built-in options, but they're less aggressive:

Chrome/Edge: In the Elements panel, click the grid or flex badge next to a container, or tick it in the Layout pane, to draw an overlay on the page.

Firefox: In the Inspector, click the grid or flex marker next to a container, or use the Layout tab, to toggle the same kind of overlay.

These are gentler: they cover the containers you pick and leave existing styles alone. The nuclear CSS option covers every element at once.

Production Warning

Obviously, don't ship this to production. Use it as a temporary debugging tool:

  1. Add the CSS
  2. Find the problem
  3. Remove the CSS
  4. Fix the actual issue

You can also make it a browser bookmark for quick access:

javascript
javascript:(function(){var s=document.createElement('style');s.innerHTML='*{background:#000!important;color:#0f0!important;outline:solid #f00 1px!important}';document.head.appendChild(s);})();

Save that as a bookmark, click it when you need to debug, and refresh to remove.

Variations

Less aggressive version (outlines only):

css
* {
  outline: solid #f00 1px !important;
}

Nesting shows as darker shades:

css
* {
  outline: 1px solid rgba(255, 0, 0, 0.6) !important;
  background: rgba(255, 0, 0, 0.05) !important;
}

Each element adds another 5% red on top of its parent, so the deeper the nesting, the darker the area. (Don't try different colors per depth with * * and * * *: they all have zero specificity, so the last rule wins for everything inside <body>.)

Pseudo-elements too:

css
*, *::before, *::after {
  outline: solid #f00 1px !important;
}

* doesn't match ::before and ::after, and ghost boxes are often pseudo-elements.

Specific element debugging:

css
.problematic-section * {
  outline: solid #f00 1px !important;
}

When to Use This

  • Complex grid/flexbox layouts misbehaving
  • Third-party components with mysterious spacing
  • Email template debugging (the Wild West of CSS)
  • Legacy code with unknown styles
  • Mobile responsive issues where something's causing horizontal scroll

Conclusion

The outline debugging technique is crude, aggressive, and absolutely effective. When you're stuck on a layout issue and traditional debugging isn't helping, this nuclear option can reveal the problem in seconds.

It's not elegant, but it works. And sometimes that's all that matters when you're on a deadline and the layout is inexplicably broken.

Keep this trick in your toolkit for those "I have no idea what's happening" moments. Your future self will thank you.