- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- Debugging CSS
CSS Output & Performance
Debugging CSS
Trace computed values, layout constraints, stacking contexts, and overflow with browser developer tools. It provides a repeatable workflow for matched rules, computed values, box dimensions, Grid and Flex overlays, stacking contexts, overflow, and reduced test cases.
When CSS does not do what you expect, do not start changing things at random. Open the browser's DevTools, click the element, and look: it shows exactly which rule won, which lost, and why. Most CSS bugs are solved in that panel.
Two lessons explain most of what you will see there: Cascade & Inheritance for why one rule beats another, and CSS Specificity for how selector strength is counted.
Start with the element
Right-click the element and choose Inspect. The Styles panel lists every rule that matches, with the losing declarations crossed out. A declaration with a yellow warning icon is invalid, often a typo. The Computed panel shows the final value of each property, and clicking it jumps to the rule responsible. You can also force states like :hover or :focus there to check pseudo-classes.
Make the invisible visible
Example
css
/* Temporary diagnostic rule */
* {
outline: 1px solid rgb(255 0 0 / 0.18);
}This temporary rule draws a faint red line around every element, so you can see their real sizes and spot the one that is too wide. Remove it when you are done.
- Use the Grid and Flex overlays to see tracks, gaps, free space, and alignment.
- Inspect the box model for unexpected padding, borders, or collapsed margins.
- Look for
min-width: autowhen flex or grid content will not shrink. - Inspect stacking-context badges before raising z-index.
- Use the Accessibility panel to check the computed name and contrast.
- Emulate media features such as reduced motion and forced colors.
Make the problem small
Copy the smallest failing markup and styles into an isolated page. Remove declarations until the failure disappears, then restore the last relevant rule. A reduced case exposes interactions faster than random property changes.
Debugging Checklist
| Check | What it reveals | Browser tool |
|---|---|---|
| Matched rules | Which declarations matched and which were overridden. | Styles panel |
| Computed value | The final value after the cascade. | Computed panel |
| Box dimensions | Content, padding, border, and margin sizes. | Box model |
| Layout tracks | Flex and Grid alignment and track boundaries. | Layout overlay |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Debugging CSS example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
/* Temporary diagnostic rule */
* {
outline: 1px solid rgb(255 0 0 / 0.18);
}
</style>
</head>
<body>
<main><section class="layout"><article><h1>Inspect this layout</h1><p>Open developer tools and inspect the box model, matched rules, and computed values.</p></article><aside>Sidebar</aside></section></main>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| Core CSS inspection | 1 | 12 | 1 | 1 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
Common causes, quickly
- The rule does not apply at all: a typo in the selector or property, a missing semicolon, or the CSS file is not loaded. Check the Network panel.
- It applies but loses: another rule is more specific or comes later.
- Width or height is ignored: the element is inline. Change its display.
- The page scrolls sideways: something is wider than the screen. The outline rule above finds it fast.
- A flex or grid item will not shrink: add
min-width: 0.
Once you know the cause, fix it at its source with the smallest change, instead of adding another override on top.
Debugging CSS exercises
Show a box
Outline the box with 1px solid red, which does not change its size.
The HTML your code works on
<div class="wrap">
<div class="box">Box</div>
<p class="wide">Long text</p>
</div>Result
