Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 36 ยท Debugging Workflows

Debugging Layout and CSS Bugs

A repeatable method for layout bugs: find the winning rule, read the box model, check specificity and cascade, inspect flex and grid, and test fixes live.

"Why is this box not where I want it?" is the most common question in front-end work, and guessing wastes hours. DevTools lets you ask the browser itself. This lesson gives you a method for layout and CSS bugs: a fixed sequence of questions that finds the cause of nearly every case, with a stand-in for the part that is plain arithmetic, CSS specificity.

The method assumes you know where the Elements panel, Styles pane and Computed pane live; if not, review the earlier chapters first.

The method

  1. Select the element (Ctrl+Shift+C, then click it). Confirm you picked the one you mean, and not a wrapper.
  2. Is the rule applied at all? In the Styles pane, find your declaration. If it is crossed out, another rule wins. If it has a yellow warning triangle, hover it: DevTools tells you the property has no effect here, for example width on an inline element or align-items on a non-flex container.
  3. Who wins? Rules higher in the Styles pane win for equal specificity; the ones lower are overridden. Compare the selectors.
  4. What is the computed value? The Computed pane shows the final value and, when you expand a property, which rule produced it.
  5. What is the box doing? Look at the box model diagram: content, padding, border, margin, with the actual numbers. Hover the element in the DOM tree to see the overlay on the page.
  6. What is the parent doing? Many layout bugs live in the container: its display, position, overflow, height, flex or grid settings.
  7. Test a fix live by editing values in the Styles pane, then copy the change to your source file.

Specificity: who wins?

When two rules set the same property on the same element, the browser picks by cascade layers and importance first, then specificity, then source order. Specificity is written as a triple (IDs, classes/attributes/pseudo-classes, elements/pseudo-elements). Compare left to right: (1,0,0) beats (0,5,0). Inline styles beat all selectors; !important beats normal declarations. The stand-in below computes specificity for simple selectors, helping you read the Styles pane.

JS Stand-in: CSS specificity calculator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The result shows why #main h2 (1,0,1) overrides .card .title (0,2,0) even though the latter looks more specific. The lesson for fixing bugs: avoid fighting specificity with !important; reduce the winning selector or use a cascade layer.

Reading the box model

Under the Computed tab (or at the bottom of the Styles pane) the box diagram shows margin (orange), border (yellow), padding (green) and content (blue). Common surprises:

  • box-sizing: with content-box (the default), width: 300px plus 20px padding makes the box 340px wide. With border-box, the width includes padding and border. Most resets set border-box globally.
  • Margin collapsing: vertical margins of adjacent blocks merge into the larger one instead of adding; flex and grid items do not collapse.
  • Percentage heights do nothing unless the parent has a definite height.

The next stand-in computes the outer size of a box in both models, a useful sanity check when numbers on the diagram look odd.

JS Stand-in: box model size calculator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

With content-box the box takes 364 px of the container (344 px box plus margins); with border-box it takes 320 px. A row of three such boxes in a 1000 px container wraps in one model (1092 px) and fits in the other (960 px).

Flexbox and Grid overlays

Elements that use flexbox or grid show a small flex or grid badge next to them in the Elements panel. Click it to draw an overlay on the page:

  • Grid overlay shows track lines, numbers and names, gaps and areas. The Layout pane (next to Styles) lists all grids on the page with checkboxes and lets you display line numbers or area names.
  • Flex overlay highlights the main and cross axes and free space. Clicking the flex icon in the Styles pane opens an editor for justify-content, align-items and flex-direction with visual buttons.

Typical bugs these find: items shrinking because flex-shrink defaults to 1 (set min-width: 0 for long text), a grid column wider than expected because of auto sizing, or align-items: stretch making cards equal height when you wanted them not to.

Finding why something overflows or is clipped

  • Horizontal scrollbars: select html and press the Delete key on suspects until the scrollbar disappears (this only changes the DOM in the browser), or use the Console: [...document.querySelectorAll("*")].filter(e => e.scrollWidth > innerWidth).
  • Clipped content: check ancestors for overflow: hidden, a fixed height, or contain.
  • Element behind another: look at z-index, but remember a z-index only works on positioned elements or flex/grid items, and stacking contexts (created by opacity < 1, transform, position: fixed...) trap z-indexes inside them.

You can force states in the Styles pane via the :hov button to debug :hover, :focus and :active rules, and the .cls button to toggle classes. Add outline: 1px solid red to everything temporarily with * { outline: 1px solid red } in a new style rule to see all boxes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which selector has the highest specificity?

    #menu a is (1,0,1), higher than three classes (0,3,0) because the ID column is compared first.
  2. 2. A declaration in the Styles pane shows a yellow warning triangle. What does it mean?

    Overridden declarations are crossed out. The triangle means the declaration is valid but does nothing here.
  3. 3. Width 300px, padding 20px, border 2px each side, content-box. What is the border-box width?

    300 + 2 x 20 + 2 x 2 = 344 px.
  4. 4. Which DevTools feature lets you test hover styles without holding the mouse?

    The :hov button forces element states like hover, focus and active.

Summary

  • Follow the method: select, is it applied, who wins, computed value, box model, parent, test fix.
  • Specificity (IDs, classes, elements) decides conflicts before source order; avoid !important.
  • The box model diagram and box-sizing explain most sizing surprises.
  • Flex and grid badges draw overlays; the Layout pane lists them.
  • Force states with :hov and toggle classes with .cls.

Next: Debugging Slow Page Loads