Sub topic 7
Inspecting boxes in browser developer tools
This chapter assumes you can write CSS rules with selectors and declarations, and that you know the units px, em, rem, and %. It also assumes you have met the four box layers (content, padding, border, margin) and the box-sizing property in earlier chapters. Here you will use the browser's developer tools to see those layers on a real page. You will also learn to find the cause of a size or spacing surprise.
Why inspect instead of calculating
You can compute a box's size by hand. A card with width: 300px, padding: 20px, and a 5px border is 350px wide under content-box. Real pages are harder to calculate. Rules come from many stylesheets. Some values are inherited. Percentages and rem units depend on other elements. Margins may collapse.
Developer tools show the result the browser actually computed. They answer three questions:
- Which rules apply to this element, and which one won?
- What are the final sizes of the content, padding, border, and margin?
- Why is the box larger, smaller, or farther from its neighbour than expected?
The tool names differ between browsers (Chrome and Edge call it DevTools, Firefox calls it Developer Tools, Safari calls it Web Inspector). The ideas are the same. This chapter uses Chrome and Firefox names where they differ.
Opening the tools and selecting an element
There are three common ways to open the tools:
- Press
F12, orCtrl+Shift+I(Cmd+Option+Ion macOS). - Right-click an element on the page and choose Inspect.
- Open the tools, then click the element picker (the arrow-in-a-box icon at the top left of the panel) and click on the page.
The Elements panel (called Inspector in Firefox) shows the live DOM tree. The DOM is the browser's in-memory version of your HTML. Selecting a node highlights its box on the page. Beside the tree you will find the Styles pane, which lists the matching CSS rules, and the Computed pane, which lists final values.
Use a small test page to follow along:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Box inspection</title>
<style>
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
margin: 10px;
}
</style>
</head>
<body>
<div class="card">Inspect me</div>
</body>
</html>Save it as inspect.html, open it in a browser, and inspect the div.
The box model diagram
In the Computed pane (Chrome) or the Layout tab (Firefox), you will find a set of nested rectangles. This is the box model diagram. It shows one number per side for each layer.
| Layer in the diagram | Typical colour in Chrome | What it holds |
|---|---|---|
| Margin | Orange | Space outside the border |
| Border | Yellow | The border widths |
| Padding | Green | Space inside the border |
| Content | Blue | The width and height of the content area |
The colours vary by browser and version, so rely on the labels, not the colours.
For the test card, the diagram should show:
- Content:
300 × 20.4or similar (the height depends on the font) - Padding:
20on every side - Border:
5on every side - Margin:
10on every side
Hover over a layer in the diagram. The browser highlights the same layer on the page. This is the quickest way to see where one layer ends and the next begins.
Reading the totals
The diagram shows the content size, not the total. Add the layers yourself:
- Border-box width: 300 + 20 + 20 + 5 + 5 = 350px
- Space taken horizontally, including margin: 350 + 10 + 10 = 370px
Chrome also shows the border-box size when you hover over an element on the page. The tooltip reads something like 350 × 70.4. Firefox shows similar dimensions in its hover label. Compare this tooltip with your own sum. If they disagree, one of your assumptions about the rules is wrong, and the Styles pane will tell you which.
The Styles pane and the cascade
The Styles pane lists every rule that matches the selected element. Rules appear in order of priority, with the winner at the top. A declaration that loses is shown with a line through it. This strikethrough tells you that the cascade chose another value. The cascade is the set of rules the browser uses to pick one value when several declarations compete.
Each rule shows its source file and line number. Clicking the link takes you to the stylesheet. Inherited properties appear under headings such as "Inherited from body".
Three habits help with box problems:
- Look for
box-sizing. If a global rule setsborder-boxand a component overrides it, the samewidthyields different sizes. - Look for shorthand properties.
margin: 10px 20pxsets all four sides. Expand the arrow beside a shorthand to see the individual sides. - Look for declarations with a warning icon. Chrome flags invalid or unknown values, such as
margin: 10 px, which has a space in the wrong place.
The Computed pane
The Computed pane lists the final value of each property after the cascade and unit resolution. It matters for the box model because it converts relative units to pixels. Suppose you write width: 50% or padding: 2rem. The Computed pane shows the used pixel values for the current window size and font size.
Use the Filter field to find a property quickly. Typing margin shows the four margin values. Expand any property to see which rule set it. This is the fastest way to answer "where does this 16px come from?"
A few values show as auto. For example, margin-left: auto shows its resolved pixel value in the box diagram but auto in the property list in some browsers. The diagram is the more reliable source for the real number.
Editing values live
You can change values without touching your source files:
- Click a value in the Styles pane, such as
300px. - Type a new value and press
Enter. - Use the
UpandDownarrow keys to change a number by 1. HoldShiftfor steps of 10. - Untick the checkbox beside a declaration to disable it.
- Click the
+icon (New Style Rule) to add a rule for the selected element.
You can also edit the numbers in the box diagram directly. Double-click the padding value and type a new number. The page updates at once.
Try this experiment on the test card. Click + to add a rule for .card and enter box-sizing: border-box. The content width in the diagram drops from 300 to 250, and the border-box width stays at 300. Untick the declaration to switch back. You have just reproduced the difference between the two sizing modes without reloading.
Showing the box on the page
Several tools make hidden boxes visible:
- Hover highlighting. Hovering over a node in the Elements panel colours its content, padding, border, and margin areas on the page. This is how you spot a margin that pushes content away.
- Layout overlays. In Firefox, the Layout tab can show margin, border, and padding guides. In Chrome, selecting an element and pressing
Escopens the console drawer, which is useful for checking sizes withgetBoundingClientRect(). - Outline all boxes. Adding a temporary rule that outlines every element reveals the whole structure at once.
The last technique uses outline, which is drawn outside the box model and does not change any element's size (a later chapter covers this). That is why it is safe for debugging:
* {
outline: 1px solid rgba(255, 0, 0, 0.4);
}Paste this into the Styles pane as a new rule, or into a stylesheet while debugging. Do not use border for the same purpose. A border adds to the box size and shifts the layout, so you would debug a different layout than the real one.
Measuring from the console
The console can return exact numbers for the selected element. In Chrome, the most recently selected element is available as $0. In Firefox, use $0 as well while the Inspector has a node selected.
const box = $0.getBoundingClientRect();
console.log(box.width, box.height);
const style = getComputedStyle($0);
console.log(style.boxSizing, style.marginTop, style.paddingLeft);getBoundingClientRect() returns the border-box size. For the test card, box.width is 350. getComputedStyle returns the final CSS values as strings, for example "10px". These calls are useful when you want to compare two elements or log a size while resizing the window.
Common problems to look for
| Symptom | What to check | Likely cause |
|---|---|---|
| A box is wider than its `width` value | Computed `box-sizing` and the padding and border numbers | The element uses `content-box`, so padding and border add to the width |
| A horizontal scrollbar appears | Hover over elements to find one that sticks out past the viewport | A `width: 100%` box with padding or border under `content-box` |
| The gap between two blocks is smaller than the sum of their margins | The margin layers of both elements | Adjoining vertical margins collapsed |
| Space appears above the first child but not inside its parent | The margin of the first child | The child's top margin collapsed through the parent's edge |
| A rule has no effect | Strikethrough in Styles, or a warning icon | A more specific rule won, or the value is invalid |
| Vertical spacing is ignored on text | The element's `display` value | Vertical margins and `width` and `height` do not apply to non-replaced inline boxes |
Chrome's tooltip for a hovered element also shows display, and the Styles pane shows it as a normal declaration. Check it early, because display type changes which box properties apply.
Edge cases and cautions
- Values are rounded in the diagram. The diagram may show
20.4or20.39. Use the console for exact numbers when it matters. - Zoom changes numbers. Browser zoom and device pixel ratio alter what the tools report as CSS pixels in some cases. Reset zoom to 100% before measuring.
- Collapsed margins are not shown as one number. Each element's diagram still shows its own margin. The collapse happens during layout, so you must compare both diagrams and the actual gap on the page.
- Pseudo-elements are separate boxes.
::beforeand::afterappear in the DOM tree as their own nodes. Select them to inspect their boxes. - Media queries and state. Styles may change with window width or with
:hover. Use the :hov toggle in the Styles pane to force a state, and resize the window to trigger breakpoints. - Device mode. Chrome's device toolbar (
Ctrl+Shift+M) and Firefox's Responsive Design Mode (Ctrl+Shift+M) let you test a set viewport width, which changes percentage-based box sizes.
Putting it together
Take the test page and change the CSS to this version, which contains a deliberate fault:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Find the fault</title>
<style>
body { margin: 0; }
.panel {
width: 100%;
padding: 24px;
border: 2px solid #333;
}
</style>
</head>
<body>
<div class="panel">Panel content</div>
</body>
</html>Narrow the window until a horizontal scrollbar appears. Select .panel and read the box diagram. The content width equals the viewport width, and the padding and border add 52px on top of it. Add box-sizing: border-box in the Styles pane and the scrollbar disappears. The fix is a single declaration, and you found it by reading the numbers.
Exercise
You inspect a div with width: 200px; padding: 10px 15px; border: 4px solid; margin: 8px; and box-sizing: content-box. What numbers does the box diagram show for each layer, and what are the border-box width and the total horizontal space the element occupies?
Show answer
The diagram shows content 200 wide, padding 10 top and bottom and 15 left and right, border 4 on every side, and margin 8 on every side. The border-box width is 200 + 15 + 15 + 4 + 4 = 238px. The total horizontal space with margins is 238 + 8 + 8 = 254px.
Exercise
A page shows a horizontal scrollbar. Describe how you would use developer tools to find the element responsible and test a fix without editing your source file.
Show answer
Hover over elements in the Elements panel, or use the element picker, and look for a box whose highlighted area extends past the viewport's right edge. Select it and read its box diagram and Computed values. Check for a width: 100% combined with padding or border under content-box. In the Styles pane, add box-sizing: border-box or tick and untick other declarations, and watch whether the scrollbar disappears. Then copy the working change into your stylesheet.
Exercise
Why is outline better than border for drawing debug lines around every element?
Show answer
An outline is drawn outside the box model and takes no space, so it does not change any element's size or position. A border adds to the box size under content-box and shifts the layout, so you would be inspecting a different layout than the real one.