Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 36 ยท Elements and Styles

The Box Model and Layout Debugging

Debug spacing and layout with the box model diagram, Flexbox and Grid overlays, and size tools, with runnable stand-ins for box-sizing and flex space calculations.

Every element on a web page is a rectangular box, and most layout bugs are arithmetic bugs about that box: padding adds to the width, a margin collapses, a flex child shrinks. DevTools draws the box for you and, for modern layout, offers overlays for Flexbox and Grid. This lesson shows how to read them.

The box model diagram

Select an element and scroll the right-hand pane to the box model diagram (it is at the bottom of Styles, and also at the top of the Computed tab). It shows concentric layers:

  • Content: the innermost area. The numbers show its current width and height.
  • Padding: space between the content and the border.
  • Border: the line around the padding.
  • Margin: outside space between this box and its neighbors. It is drawn in the outermost layer.

Click a number in the diagram to edit it, or hover a layer to highlight that area on the page: orange for margin, yellow for border, green for padding, blue for content. This makes "where does this unexpected gap come from?" a matter of hovering.

The diagram shows computed numbers, so percentages and auto have already been turned into pixels. If a gap exists but you cannot find a margin or padding responsible, check the neighbors: gaps often come from a sibling's margin, a parent's gap, or an inline element's line-height.

content-box versus border-box

box-sizing controls what width means:

  • content-box (the default): width is the content width. Padding and border are added on top.
  • border-box: width includes padding and border; the content shrinks to fit.

So a width: 300px box with padding: 20px and a 2px border is 344px wide with content-box and exactly 300px wide with border-box. Many projects set box-sizing: border-box on everything because it makes arithmetic predictable. If your layout overflows by exactly "padding plus border", check box-sizing.

JS Stand-in: box-sizing arithmetic

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

No output.

This stand-in is the arithmetic you would otherwise do by reading the box diagram twice. In the output, two content-box children overflow a 600-pixel parent while border-box children fit exactly.

Margin collapsing and other gotchas

  • Margin collapsing: vertical margins of adjacent block siblings (and between a parent and its first child) combine into the larger of the two, not the sum. Flexbox and Grid items do not collapse margins.
  • Percent padding is relative to the container's width, even for top and bottom padding.
  • Inline elements ignore vertical margin and width; use inline-block or flex to size them.
  • Overflow appears as scrollbars or clipped content; the box model diagram's content size helps you tell which.

Flexbox and Grid overlays

In the Elements panel, an element whose display is flex or grid gets a small badge next to it in the DOM tree. Clicking the flex badge draws an overlay on the page: dashed lines for the container and shaded areas for item spacing. Clicking the grid badge draws track lines with numbers (line numbers and optionally track sizes and area names). Several overlays can be shown at once; the Layout tab (next to Styles) lists them with checkboxes and colors.

The Styles pane also adds small editor icons next to layout properties. Click the icon next to display: flex or flex-direction to open a visual picker for justify-content, align-items and similar values, so you can try alternatives with one click.

What to look for in a Flexbox overlay:

  • Is the main axis the direction you think? flex-direction: column swaps it.
  • Does an item shrink below its content size? Items have min-width: auto, so long words can force overflow; min-width: 0 often fixes that.
  • Is free space going where you expect? justify-content distributes along the main axis, align-items across it.

What to look for in a Grid overlay:

  • Do the line numbers match your grid-column and grid-row values?
  • Are named areas in the right cells?
  • Is an item overflowing because a track is 1fr with long content? Use minmax(0, 1fr).
flowchart LR
  A["Layout looks wrong"] --> B{"display value?"}
  B -->|"flex"| C["Check axis, wrap, shrink"]
  B -->|"grid"| D["Check lines, tracks, areas"]
  B -->|"block / inline"| E["Check box model and margins"]

Start from the container's display value to choose your debugging approach.

Stand-in: how flex distributes space

Flexbox gives free space to items by flex-grow and takes space from them by flex-shrink. This calculator models the grow case for one row, ignoring edge cases like min-content limits.

JS Stand-in: flex-grow space distribution

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

No output.

With 300 pixels of free space shared 2:1, content gets 200 extra and aside 100 extra. When you click the flex overlay in DevTools, the gaps and sizes you see should match this arithmetic.

Tools for measuring

  • Hold Alt while hovering the page (with an element selected) to see distances from the selected element to the hovered one.
  • Press Ctrl+Shift+C and hover: the tooltip shows the element's size, color contrast and accessibility info such as its role.
  • In Settings, Preferences, Elements, you can turn on Show rulers on hover and toggle Show user agent shadow DOM.
  • The Layout tab includes options to show line numbers, track sizes and area names for grids.

When an element overflows its parent horizontally, select the parent, look at the box model's content width and compare it to the child's total width including margins. The difference is your bug.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A box has width 200px, padding 10px and a 5px border. With box-sizing: content-box, how wide is its border box?

    Content 200 plus padding 20 plus border 10 equals 230px.
  2. 2. Which layer of the box model diagram is orange when you hover?

    DevTools highlights margin in orange, border in yellow, padding in green and content in blue.
  3. 3. What does a 'flex' badge next to an element in the DOM tree do when clicked?

    The badge toggles a visual overlay showing the flex container and item spacing.
  4. 4. Which statement about vertical margins of two adjacent block siblings is true?

    Adjoining vertical margins collapse to the larger value, except inside flex and grid layouts.

Summary

  • The box model diagram shows content, padding, border and margin with editable numbers.
  • border-box makes width include padding and border and avoids many overflow bugs.
  • Flex and Grid badges draw overlays; the Layout tab manages them.
  • Debug layout by reading numbers: container width, child widths with margins, free space and axis direction.

Next: Computed Styles, Inheritance and Style Sources