Sub topic 3
Debugging CSS: styles, computed values and the box model
This chapter assumes you know basic HTML and CSS, including selectors, the cascade, and properties such as margin and padding. It uses Chromium panel names. Firefox and Safari have equivalent panes under similar names.
Where CSS debugging happens
Select an element in the Elements panel, and the right-hand sidebar shows several tabs. Three matter most for layout problems:
- Styles lists every rule that matches the element, ordered by priority, with the source file and line number.
- Computed shows the final value the browser resolved for each property.
- Layout lists flexbox and grid containers on the page and lets you overlay them.
The usual question is "why does this element look wrong?" The Styles pane tells you which rules applied. The Computed pane tells you which value won.
Reading the Styles pane
Rules appear from highest priority at the top to lowest at the bottom. Three visual cues explain most surprises:
- A struck-through declaration lost to another rule. It was overridden by higher specificity, a later rule, or
!important. - A declaration with a yellow warning icon is valid but has no effect. For example,
topon an element withposition: static. - A declaration with a red underline has an invalid property name or value, which the browser ignored.
You can edit directly in this pane. Click a value to change it, use the arrow keys to step through numbers, and tick or untick the checkbox beside a declaration to toggle it. To add a property, click on empty space inside a rule. Edits apply instantly and are lost on reload, so copy the final values into your source files.
Computed values
The Computed pane resolves relative units and shorthand into final values. A width: 50% becomes pixels, and font-size: 1.2rem becomes a pixel size. Use the filter box to find a property, and tick "Show all" to include defaults.
Expand a property in this pane to see every rule that set it. The topmost entry is the winner. This is the fastest way to answer "which rule is actually setting this color?" when the Styles pane is crowded.
The box model diagram
Every element is a box with four layers, from the inside out: content, padding, border, and margin. The Computed pane draws these as nested rectangles with the numbers for each side. Hover over a layer, and the page highlights that area. Double-click a number to edit it.
Two points commonly cause confusion:
- With
box-sizing: content-box(the default),widthsets the content only. Padding and border add to the total. Withborder-box,widthincludes padding and border. - Vertical margins between siblings can collapse into one. The diagram shows each element's own margin, so a gap that looks smaller than the sum of two margins is collapsing, not a bug in the numbers.
You can check the sizing mode in the Computed pane by searching for box-sizing.
Worked example: a box that overflows its container
Suppose this page has a card that sticks out past its parent.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Overflow demo</title>
<style>
.container { width: 300px; border: 1px solid #333; }
.card { width: 100%; padding: 20px; border: 4px solid teal; background: #e0f7f7; }
</style>
</head>
<body>
<div class="container">
<div class="card">Card text</div>
</div>
</body>
</html>Follow these steps:
- Right-click the card and choose Inspect.
- In the box model diagram, note that the content is 300 pixels wide, and padding (20 on each side) and border (4 on each side) are added around it. The total is 348 pixels, which is wider than the 300-pixel container.
- In the Computed pane, find
box-sizing. It sayscontent-box. - In the Styles pane, click on empty space in the
.cardrule and addbox-sizing: border-box. The card now fits. - Copy that declaration into your stylesheet.
Common errors
- Editing the wrong rule. If a rule comes from a shared stylesheet, the file link in the Styles pane shows where to make the real change.
- Trusting Styles over Computed. A value in Styles may be struck out or overridden. Confirm the final result in Computed.
- Forgetting states. Use the
:hovbutton in the Styles pane to force:hover,:focus, or:active, so you can inspect rules that only apply in those states. - Ignoring inheritance. If a property is not set on the element, scroll down in Styles to the "Inherited from" sections.
Exercise
A div has width: 200px, padding: 10px, a 5px border, and box-sizing: content-box. What is its total rendered width, and what single declaration would make it exactly 200px wide without changing the other values?
Show answer
The total width is 200 + 10 + 10 + 5 + 5 = 230px. Adding box-sizing: border-box makes the 200px width include the padding and border, so the box renders at 200px.
Exercise
In the Styles pane, a color: red declaration is struck through, but the text is blue. Which pane confirms the winning rule, and how?
Show answer
The Computed pane. Expand the color property there to see every rule that set it, with the winning rule at the top and the blue value as the resolved result.