CSS Module 1 – How CSS works
Inspecting CSS with browser DevTools
Open DevTools in Chrome, Firefox and Safari, see which CSS rule wins and why, read computed values and the box model, and edit styles live.
What you will learn
- Find which rules match an element, which declaration wins and which are overridden
- Read the computed panel and box-model diagram to explain an element's final size
- Edit and toggle declarations live, then copy the fix back into the stylesheet
- Test dark mode, reduced motion and print styles with the browser's emulation tools
Before you start
What you’ll learn · Before you start
What you will learn
- Find which rules match an element, which declaration wins and which are overridden
- Read the computed panel and box-model diagram to explain an element's final size
- Edit and toggle declarations live, then copy the fix back into the stylesheet
- Test dark mode, reduced motion and print styles with the browser's emulation tools
Before you start
On this page
Every desktop browser has DevTools: A browser's tools for inspecting the current document, styles, resources and behaviour., DevTools for short, that show exactly how CSS applies to any element on a page. Right-click an element and choose Inspect or Inspect Element, depending on the browser (Safari shows the item once its developer features are switched on), and the Elements panel opens with that element selected. Its Styles pane lists every rule that matches the element, the strongest first, with the declarations that lost crossed out and the ones the browser could not use flagged. Its Computed pane shows resolved property values beside a diagram of the element’s box. You can change any value, or switch a declaration off, and the page updates at once.
This lesson works through a small card with three planted bugs, one A valid declaration that loses a cascade comparison to another declaration for the same property. declaration, one invalid and one inactive declaration: A valid CSS declaration whose value does not affect the selected kind of box in the current layout., in Chrome, Firefox and Safari.
Selecting an element separates four common failures
Here is a small order card. Paste the two files into the HTML, CSS & JS Playground and inspect its preview as you read; the steps below find each bug.
HTML · card.html
<div class="panel">
<article class="card">
<h2 class="card-title">Order 4821</h2>
<p>Your parcel has left the Pune warehouse.</p>
<a class="button" href="#track">Track it</a>
</article>
</div> CSS · card.css
.panel {
font-size: 16px;
width: 640px;
}
.card {
font-size: 1.25em;
width: 50%;
padding: 0.75em 1em;
border: 0.1em solid #c8d3e0;
margin: 0 auto;
line-height: 1.5;
color: #243b53;
justify-content: center;
}
.card-title {
font-weight: 600;
colour: #102a43;
}
.card a {
background: #0b6e4f;
color: #ffffff;
padding: 0.25em 0.75em;
}
.button {
background: #1d4ed8;
} The design asked for three things that did not happen: a title darker than the text (#102a43), a blue “Track it”
button (#1d4ed8), and the card’s contents centred. None of the three shows an error anywhere on the page.
The first useful split is whether a rule appears for the selected element. A missing rule suggests that its sheet did not load or its selector does not match. If the rule appears, ask a different question: is the declaration invalid, overridden, or valid but inactive on this kind of box? Those are different causes, so making a selector stronger cannot repair every failure.
For normal declarations of the same importance, an element’s style attribute ranks above a selector rule. Between selector rules, specificity comes before source order: a class plus an element can beat a later single class. The Styles pane exposes that contest. A valid winning alignment value may still be inactive on an ordinary block, which is why the final colour or position alone does not explain the cause.
The current view can be edited, but a DevTools edit is not a saved stylesheet. Copy a working fix into the file and reload before treating it as persistent.
Follow the selected rule’s presence and validity before choosing a fix:
The drawing shows the whole scene; its 4 steps are listed below.
Every step as text
- Is the rule present?. If it is absent, inspect the resource load and selector match. A stronger selector is not the first fix for a file that never loaded.
- Is the declaration valid?. A misspelt property or invalid value is a parser problem. Other valid declarations of the rule can remain.
- Did the declaration win?. A crossed-out valid declaration lost a cascade comparison. Find the declaration that won before altering specificity.
- Does it affect this box?. A valid winning value may be inactive on this box. The original card's justify-content on a block demonstrates that separate case.
Checkpoint
Check: Finding a declaration's problem
Computed values answer a different question from rules
The Styles pane answers which rule supplied a value; the Computed pane answers what resolved value: The value a CSSOM inspection reports for a property, including used layout values for specified properties. the browser reports. Relative lengths can become pixel lengths, inherited values are included, and a shorthand is represented through its longhands. A reported value is still a value rather than proof of an effect: an alignment property can read center while inactive on an ordinary block.
A The content, padding, border and margin regions used to account for an element's layout dimensions. sum makes the distinction concrete. With A sizing mode in which the declared width describes content; padding and border add to the total box. sizing, width 200 px means 200 px of content. Padding of 0.5em at a 20 px font adds 10 px on each side; a 1 px border adds another pixel per side. The border box is therefore 222 px. Counting one side only gives 211 px, and assuming declared width already includes everything gives 200 px; both miss the sizing rule. Account for both sides of the same 200 px example before answering its size question.
The drawing shows the whole scene; its 3 steps are listed below.
Every step as text
- Read the sizing mode. Under content-box sizing the declared 200 px width is the content, not the padding and borders.
- Resolve the padding. The self-check gives a 20 px font and 0.5em padding. That is 10 px on each side, taking the total to 220 px.
- Include both borders. The 1 px border on each side adds two more pixels. The complete border box is 222 px; the separate full card record remains in the depth part.
Checkpoint
Check: Written values and box size
Inspecting the complete card and resolving its box Pro: opens with a Pro, Premium or Ultimate pass
The three original card bugs each fail for a different reason. The complete stylesheet, schematic and recorded box calculation let you follow each cause to the final result.
In this part:
- The complete card exposes overridden, invalid and inactive CSS
- The card measurements explain every box edge
3 diagrams · 1 runnable example (JavaScript) · about 9 minutes
Opening this part with your pass…
This part opens with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so this part can’t be opened at the moment. The free part above stands on its own. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
Persisting a fix and testing preference branches Pro: opens with a Pro, Premium or Ultimate pass
A correct current view must survive a reload, and each preference branch needs its own check. The fixed stylesheet and mode stylesheet keep those two jobs separate.
In this part:
- Live edits need a file change to survive reload
- Preference and print emulation exercise separate branches
2 diagrams · 2 runnable examples (CSS) · about 6 minutes
Opening this part with your pass…
This part opens with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so this part can’t be opened at the moment. The free part above stands on its own. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
Practise: classify the cause before changing a rule
Practice that adapts to you
Practise this lesson
10 questions about 6 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.
Interview questions
What does the Computed pane in DevTools show that the Styles pane does not?
The Styles pane shows the rules and declarations as they are written in your stylesheets, in cascade order, including
the ones that lost, so it explains why a value won. The Computed pane shows the result: one final value for every
property of the selected element, as resolved values, the same ones getComputedStyle() returns. The card’s relative
lengths become pixels and its colour becomes rgb(). Resolution depends on the property and its box; other properties
can keep computed values. Shorthands split into longhands, inherited values are included, and the card’s margins, width
and padding report layout sizes, so its margin: auto reads as 138px. Expanding a property shows which
declaration set it. Use Computed to see what the browser actually used; use Styles to see why.
-
Fresher to mid
You add .price { color: #b00020; } to a stylesheet, reload, and the price is still black. Describe, step by step, how you would use DevTools to find out why, covering every reason a declaration can fail to take effect.
-
Fresher to mid
An element has width: 50%, padding: 0.75em 1em, border: 0.1em solid and margin: 0 auto, with font-size: 20px, inside a 640px container. DevTools shows its box 364px wide and 138px from the left. Explain every number, then make the box exactly half the container.
-
Mid to senior
A card looks correct in the ordinary page but fails in dark mode and print preview. A live DevTools edit repairs it, then the problem returns after reload. Describe how you would locate the failing branches, preserve the fix and check the modes the change could affect.
Key takeaways
- Right-click and Inspect opens the Elements panel on that element; Safari first needs “Show features for web developers” in its settings.
- The Styles pane lists matching rules strongest first: crossed out means overridden, a warning means invalid, pale with an information icon means valid but inactive.
- The Computed pane shows resolved values, the same as
getComputedStyle(): the card’s pixel sizes andrgb()colour, longhands and inherited values. Other properties can keep computed values; a reported value does not prove an effect. - Edit, toggle and add declarations live, then copy them back from the Changes panel, because a reload discards them.
Terms in this lesson
- DevTools
- A browser's tools for inspecting the current document, styles, resources and behaviour.
- inactive declaration
- A valid CSS declaration whose value does not affect the selected kind of box in the current layout.
- resolved value
- The value a CSSOM inspection reports for a property, including used layout values for specified properties.
References
- View and change CSS (Chrome DevTools) (Google (Chrome for Developers))
- Find invalid, overridden, inactive, and other CSS (Chrome DevTools) (Google (Chrome for Developers))
- CSS features reference (Chrome DevTools) (Google (Chrome for Developers))
- Emulate CSS media features (Chrome DevTools) (Google (Chrome for Developers))
- Remote debug Android devices (Chrome DevTools) (Google (Chrome for Developers))
- Examine and edit CSS (Firefox DevTools) (Mozilla)
- Keyboard shortcuts (Firefox DevTools) (Mozilla)
- Enabling Web Inspector (WebKit)
- Elements Tab (Web Inspector Reference) (WebKit)
- CSS Object Model (CSSOM), resolved values (W3C CSS Working Group)
Related tools
Report a problem with this lesson
Kept only in this browser. Your Learn progress