# CSS Debugging & DevTools

> Master the browser DevTools layout overlays and learn to fix the three most common CSS bugs instantly.

Subject: CSS | Lesson 44 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/debugging-and-devtools
Previous: [Naming, BEM & Design Tokens](https://codewithanimation.in/css/naming-bem-and-tokens.md)

## Visualizing the invisible

CSS layout algorithms (like Grid and Flexbox) happen invisibly. When a layout breaks, guessing which property to change is a recipe for frustration.

Modern browser DevTools include powerful layout overlays. By clicking the small `flex` or `grid` badge next to an element in the DOM tree, the browser draws an overlay on the screen revealing the exact boundaries of every track, gap, and line.

## The three classic CSS bugs

Beyond using DevTools, mastering CSS means recognizing the common 'gotcha' bugs that plague every codebase. The three most common are:

1. **Flex child text overflow**: A child in a flex row refuses to shrink and text-truncate, blowing out the width of the page.

2. **Z-index wars**: An element with `z-index: 9999` is somehow still appearing behind an element with `z-index: 1`.

3. **Margin collapsing**: A child element's top margin pushes its parent down, instead of pushing itself away from the parent.

## The one-line fixes

**Flex Overflow**: Flex items default to `min-width: auto`. Change it to `min-width: 0` on the child. This allows it to shrink below its content size, allowing `text-overflow: ellipsis` to work.

**Z-index Wars**: Z-index only works within a stacking context. To reset the stacking context and trap z-indexes inside a parent, apply `isolation: isolate` to the parent. Now its children can never overlap elements outside of it.

**Margin Collapsing**: To prevent a child's margin from leaking out of the parent, you must establish a new block formatting context. Add `display: flow-root` to the parent.

## Common mistake: The !important trap

When developers encounter these bugs without understanding the root cause, their first instinct is to throw `!important` at the problem, or arbitrarily change `position` to absolute.

This is a massive pain point because it never actually solves the bug (e.g., `!important` does not fix margin collapsing), and it severely damages the maintainability of the CSS architecture.

## Tip: Hover state debugging

Trying to debug a `:hover` state is annoying if your mouse moves off the element to click the DevTools. Use the `:hov` toggle in the DevTools Styles panel to artificially force the element into a hover state so you can inspect it in peace.

## Try it: Try it

Explore the three classic CSS bugs. Toggle the one-line fixes to see how they instantly resolve the layout issues.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/debugging-and-devtools

## Check yourself

### Question 1

A text element inside a Flexbox row is overflowing and refusing to truncate with ellipsis. What is the standard one-line fix?

- A. Apply min-width: 0 to the flex child.
- B. Apply overflow: hidden to the flex container.
- C. Apply width: 100% to the text element.
- D. Use CSS Grid instead.

Correct answer: A

Flex items cannot shrink smaller than the size of their content by default (`min-width: auto`). Setting it to `0` allows the element to shrink, which allows `text-overflow: ellipsis` to kick in.

- Why not B: This hides the overflow, but doesn't allow the child to shrink properly.
- Why not C: Because flex items default to `min-width: auto`, a % width won't force it to shrink below its content.
- Why not D: Grid is great, but Flexbox handles this perfectly once the min-width default is overridden.

### Question 2

You have a tooltip with `z-index: 9999`, but it is still appearing behind a header with `z-index: 10`. What is the most likely cause?

- A. The tooltip is trapped inside a parent that has a lower stacking context than the header.
- B. The header has `!important` on its z-index.
- C. Z-index maxes out at 1000.
- D. The tooltip is missing `position: relative`.

Correct answer: A

Z-index values are not global. If a parent element creates a stacking context (e.g., via `z-index: 1`), all of its children (even `z-index: 9999`) are trapped inside that context and will appear behind any element in a sibling stacking context of `z-index: 2`.

- Why not B: `!important` affects specificity, not stacking order.
- Why not C: Z-index can technically go up to 2147483647.
- Why not D: If it had no position, z-index wouldn't work at all, but the question implies it's a stacking context issue.

### Question 3

What does the `isolation: isolate` property do?

- A. It creates a new stacking context, trapping child z-indexes and mix-blend-modes inside the parent.
- B. It prevents margin collapsing.
- C. It removes the element from the DOM accessibility tree.
- D. It stops JavaScript event bubbling.

Correct answer: A

It is the modern, intended way to create a stacking context without resorting to hacks like `transform: translateZ(0)` or arbitrary `z-index` values.

- Why not B: That is the job of `display: flow-root`.
- Why not C: That is `aria-hidden="true"`.
- Why not D: That is `pointer-events: none` or JS `stopPropagation()`.

## Remember this

- Use the Grid and Flex overlays in browser DevTools to see the invisible layout tracks.
- Fix flex-child text overflow by applying `min-width: 0`.
- Fix z-index battles by creating a new stacking context with `isolation: isolate`.
- Fix margin collapsing by adding `display: flow-root` to the parent container.
