CSS Debugging & DevTools
Master the browser DevTools layout overlays and learn to fix the three most common CSS bugs instantly.
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.
Try it
Explore the three classic CSS bugs. Toggle the one-line fixes to see how they instantly resolve the layout issues.
The 3 Classic CSS Bugs
Child refuses to shrink (Bug)
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
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-rootto the parent container.
Done with this concept?
Mark it complete to track your progress. No login needed.