Sub topic 7
Inspecting boxes in browser developer tools
This chapter assumes you can write CSS rules and know the four box layers: content, padding, border and margin. It also assumes you know the box-sizing property. You will use the browser's developer tools to see those layers on a real page. The steps are the same in Chrome, Edge, Firefox and Safari, apart from a few panel names.
Open the inspector
Right-click an element and choose Inspect, or press F12. The tools open on the DOM tree, which is called Elements in Chrome and Edge and Inspector in Firefox. Clicking a node in the tree selects that element. The styles pane then shows the rules that apply to it.
Read the box model diagram
Chrome shows the box model diagram at the bottom of the Computed tab. Firefox shows it in the Layout panel under Box Model. Both draw nested rectangles, from the outside in: margin, border, padding, content.
Take this rule, with the default content-box sizing:
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
margin: 10px;
}The diagram for .card would show these numbers:
| Layer | Value on each side | Running width |
|---|---|---|
| Content | 300 (width) | 300px |
| Padding | 20 | 340px |
| Border | 5 | 350px |
| Margin | 10 | 370px |
The center of the diagram shows the content size, such as 300 × 120. The border box is 350px wide, so that is the width you see on the page. The margin box is 370px. It reserves space around the element, but it is not part of the element's visible size.
Use the hover overlay
Hover over a node in the DOM tree and the browser highlights that element on the page. In Chrome, the content is blue, the padding is green, the border is yellow and the margin is orange. Firefox uses similar tints. Hovering also shows a tooltip with the rendered size.
This overlay is the fastest way to find out where unexpected space comes from. If a gap looks too large, hover the elements on both sides of it. The overlay shows which margin or padding produces the gap.
Edit values live
Click a number in the box model diagram and type a new one. The page updates immediately. You can also toggle declarations in the styles pane with their checkboxes. This lets you test a fix before you change your source file.
*,
*::before,
*::after {
box-sizing: border-box;
}Try adding this rule to a page in the styles pane. With border-box, the 300px width now includes padding and border. The diagram shows the content shrinking to 250px while the border box stays at 300px. The box model diagram is the quickest way to confirm which box-sizing value applies.
Common problems to check
- Content is smaller than your
width. The element probably usesborder-box. The diagram shows the content area after padding and border are subtracted. - The gap between two blocks is smaller than the sum of their margins. Adjoining vertical margins collapse. The diagram still shows each element's own margin values, so measure the gap with the overlay instead of adding the numbers.
- An element is wider than its parent. Look for
content-boxsizing with padding, a fixedwidth, or content that overflows. - Your edit has no effect. Check the styles pane for a struck-through declaration. A more specific rule has overridden it.
Exercise
You select an element in the inspector. The box model diagram shows content 250 × 80, padding 20 on every side, and border 5 on every side. The styles pane shows width: 300px. Which box-sizing value is in effect, and how wide is the margin box if the margin is 10px on every side?
Show answer
The value is border-box. The border box is 250 + 20 + 20 + 5 + 5 = 300px, which equals the declared width. The margin box is 300 + 10 + 10 = 320px wide.