Sub topic 3
Debugging CSS: styles, computed values and the box model
CSS bugs are rarely about a property being missing. Usually a rule you wrote loses to another rule, never matches the element, or produces a size you did not expect. DevTools shows all three situations directly. This chapter covers the Styles pane, the Computed pane and the box model diagram, and ends with a repeatable way to use them.
Prerequisites: you can read and write basic CSS (selectors, properties, units), you know how to select an element in the Elements panel (covered in the previous chapter), and you know that the browser resolves conflicting rules through the cascade. Panel names follow Chromium-based browsers. Firefox and Safari have equivalent panes with similar names.
The Styles pane
Select an element in the Elements panel. The Styles pane on the right lists every rule that applies to it, with a link to the stylesheet file and line number on each rule. Clicking the link opens the source in the Sources panel.
The order of the list matters. It runs roughly from strongest to weakest:
element.style, which holds inline styles and anything you type directly into the pane.- Matching rules, with the winner of any conflict near the top.
- Sections titled "Inherited from ..." for properties such as
colorandfont-familythat pass down from ancestors. - The browser's own default stylesheet, labeled "user agent stylesheet".
Reading the markers
| What you see | What it means |
|---|---|
| A declaration with a line through it | Another rule won for that property. The declaration is valid but overridden. |
| A yellow warning icon next to a declaration | The property name or value is invalid, so the browser dropped it. |
| A dimmed declaration | The property is valid but has no effect on this element, for example `width` on an inline element. Recent versions explain why in a tooltip. |
| An unchecked box | You turned the declaration off in DevTools. |
An overridden declaration and an invalid one look similar at first glance. Check for the warning icon. An invalid value is a typo to fix. An overridden value is a cascade problem to resolve.
Editing live
Every part of a rule is editable. Click a value to change it, click a selector to rename it, or click in the empty space at the end of a rule to add a property. Hover over a declaration and a checkbox appears to its left. Unchecking it is the fastest way to ask whether this one line is the cause.
Some editing shortcuts save a lot of time:
- Select a numeric value and press Up or Down to change it by 1. Shift moves by 10 and Alt by 0.1.
- Click the small color square beside a color to open a picker. Shift+click on the square cycles between hex, RGB and HSL notation.
- Type in the autocomplete list. It suggests only valid property names and values, which helps you spot typos.
- Click the
+button in the pane toolbar to create a new rule for the selected element.
Forcing states and classes
Many styles apply only while an element is hovered, focused or active. You cannot keep the mouse on an element while you work in DevTools, so use the :hov button in the Styles toolbar. It forces :hover, :focus, :active, :visited or :focus-within on the selected element. The .cls button lets you add or remove class names without editing the HTML. Together they let you test a hover style or a "selected" class without writing any JavaScript.
The cascade, as DevTools shows it
When two rules set the same property on the same element, the browser picks a winner using three checks in this order. First it compares origin and !important, then specificity, then source order.
| Check | Winner |
|---|---|
| Importance | A declaration with `!important` beats a normal one. |
| Specificity | An ID selector beats a class selector. A class selector beats a type selector. |
| Order | When specificity is equal, the rule that appears later in the stylesheet wins. |
For example, #featured .title has one ID and one class, so it beats .card .title, which has two classes. The order of the two rules in the file does not matter.
The Styles pane applies this logic for you. The strikethrough tells you which declaration lost, and the rule above it tells you who won. To fix the problem, you can raise the specificity of your rule, lower the specificity of the winner, or reorder the rules. Avoid !important as a fix. It wins now, but it makes the next conflict harder to resolve.
The Computed pane
The Styles pane shows what you wrote. The Computed pane shows what the browser decided. It lists the final value of each property after the cascade, inheritance and unit resolution. Relative units become absolute ones, so width: 50% appears as a pixel value, 2rem as pixels, and red as rgb(255, 0, 0).
Use it for these tasks:
- Confirm the real value when several rules are involved. Click the arrow beside a property to see every rule that tried to set it. The winner is listed first and the losers are crossed out.
- Search by name with the filter box. Tick "Show all" to include properties you never set, which shows the defaults.
- Check an inherited value. If a text color is wrong and no rule on the element sets it, Computed shows the final color, and the arrow leads to the ancestor rule that supplied it.
- Compare resolved sizes. A computed
widthof0pxorautoon an element you expected to be visible is a strong clue.
A good habit is to treat Styles as the place to change things and Computed as the place to verify them.
The box model
Every element is a rectangle made of four layers, from the inside out: content, padding, border and margin. The box model diagram sits at the bottom of the Styles pane (and in the Computed pane). It shows the numeric value of each layer, and you can edit any number by clicking it.
Hover over an element in the DOM tree and the page highlights it using colors that match the diagram. Blue is the content, green is the padding, yellow is the border and orange is the margin. A tooltip beside the element also shows its rendered size. If the highlighted rectangle is not where you expected, the diagram usually explains why.
box-sizing changes what `width` means
With the default box-sizing: content-box, the width property sets only the content area. Padding and border are added on top. With box-sizing: border-box, width includes padding and border. A card with width: 300px; padding: 20px; border: 5px solid has these totals:
| box-sizing | Content width | Total rendered width |
|---|---|---|
| content-box | 300px | 350px (300 + 40 + 10) |
| border-box | 250px | 300px |
The box model diagram makes this visible. Compare the number you typed in width with the total size shown in the hover tooltip. If they differ, padding or border is the reason.
Margins collapse
Vertical margins between adjacent block elements collapse into a single margin equal to the larger of the two. An h2 with margin-bottom: 24px followed by a p with margin-top: 16px is separated by 24px, not 40px. DevTools shows both margins in the diagram, so the gap looks like it should be larger than it is. Remember this rule when the layout seems to ignore a margin. Horizontal margins and margins inside flex or grid containers do not collapse.
Layout badges
When an element uses display: flex or display: grid, the Elements panel shows a flex or grid badge beside it. Clicking the badge draws an overlay on the page showing the tracks, gaps and free space. The Layout tab in the sidebar lists every flex and grid container on the page and lets you turn overlays on and off. For alignment problems, such as an item that will not center, the overlay shows the container's axes so you can see which one justify-content or align-items is acting on.
A worked example
Save this file as card.html and open it in a Chromium-based browser. It contains three deliberate bugs.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Card debugging</title>
<style>
body { font-family: sans-serif; }
.wrap { width: 320px; border: 1px dashed #999; }
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
background: #eef;
}
.card .title { color: navy; font-size: 1.5rem; line-height: 1.5x; }
#featured .title { color: crimson; }
.badge { width: 80px; height: 24px; background: gold; }
</style>
</head>
<body>
<div class="wrap">
<div class="card" id="featured">
<h2 class="title">Featured <span class="badge">New</span></h2>
<p>Card body text.</p>
</div>
</div>
</body>
</html>Work through the bugs in order.
- The card sticks out of its wrapper. Select
.card. The hover tooltip reports 350 by some height, while the wrapper is 320 wide. The box model shows content 300, padding 20 and border 5. In the Styles pane, addbox-sizing: border-boxto.cardand the card shrinks to 300px and fits. - The title is crimson, not navy. Select the
h2. In Styles,color: navyin.card .titleis struck through. The winner is#featured .title, which has an ID in its selector. Navy was never going to win. Delete the crimson rule in your source, or lower the selector's specificity. - The badge ignores its size. Select the
span. Thewidthandheightdeclarations appear dimmed because an inline element does not accept them. Adddisplay: inline-blockto.badgeand the 80 by 24 gold box appears. In the same pane,line-height: 1.5xcarries a warning icon. It is an invalid value, so the browser ignored it. Change it to1.5.
A repeatable workflow
Common mistakes
- Losing your edits. Changes made in DevTools exist only in the open page. A reload erases them. Open the Changes tab in the drawer (Esc toggles the drawer) to see a diff of everything you edited, then copy those changes into your source files.
- Editing the wrong rule. If several rules set the same property, edit the winner (the one without a strikethrough), or your change will have no visible effect.
- Trusting a stale stylesheet. If the Styles pane shows old values after you saved a file, the browser may have cached the stylesheet. Reload with the cache bypassed, or tick "Disable cache" in the Network panel while DevTools is open.
- Expecting
widthon inline elements. Check the element's computeddisplayvalue before assuming the size is wrong. - Forgetting states. A rule that shows no match may belong to
:hoveror:focus. Use:hovto force the state. - Reaching for
!important. It hides the real cause, which is usually a specificity mismatch you can see in the Styles pane.
Summary
The Styles pane tells you which rules apply and which ones lost. The Computed pane tells you the final values. The box model diagram tells you why an element has the size and spacing it has. When a style looks wrong, work through those three in order: is the rule listed, who won, and what is the final result.
Exercise
A .button has width: 200px; padding: 10px 20px; border: 2px solid, with the default box-sizing. In DevTools the hover tooltip shows the button as 244px wide. Explain the number, and name one change that makes the rendered width 200px.
Show answer
With content-box, width sets only the content area. The total is 200 (content) + 40 (left and right padding, 20 each) + 4 (left and right border, 2 each) = 244px. Adding box-sizing: border-box makes the 200px include padding and border, so the rendered width becomes 200px.
Exercise
You set .nav a { color: white; } but the links stay blue. In the Styles pane your declaration is listed with a line through it. What does that mean, and what are two ways to find and fix the cause?
Show answer
The strikethrough means the declaration is valid but another rule with higher importance, higher specificity or a later position won for the color property. Look at the declarations above it in the Styles pane, or expand color in the Computed pane, to see the winning rule and its stylesheet link. Then either raise the specificity of your selector, or change or remove the winning rule. Avoid !important unless nothing else is possible.
Exercise
An h2 has margin-bottom: 30px and the next paragraph has margin-top: 20px. The box model diagram shows both values, but the gap on the page is only 30px. Why?
Show answer
Adjacent vertical margins between block elements collapse into one margin equal to the larger value. The 20px and 30px margins combine into a 30px gap, not 50px.