Sub topic 8
Seeing Which Styles Apply with Developer Tools
This chapter assumes you can write the three kinds of CSS from earlier chapters: the style attribute, the style element, and a linked stylesheet. It also assumes you know the cascade basics: when two rules have equal specificity, the later one wins. Here you will use the browser's developer tools to check which rule actually applied, and where it came from.
Why inspect instead of guess
When a style does not show up, the cause is usually one of a few things. The file may not have loaded. A later rule may override it. A selector with higher specificity may win. An inline declaration may beat everything. Reading the source code tells you what you wrote. Developer tools tell you what the browser did with it.
Every major desktop browser has them. Open them by pressing F12, or Ctrl+Shift+I (Cmd+Option+I on macOS), or by right-clicking an element and choosing Inspect.
A page to inspect
Save this as index.html and site.css in the same folder, then open the HTML file in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Inspect me</title>
<link rel="stylesheet" href="site.css">
<style>
p { color: blue; }
</style>
</head>
<body>
<p>Blue, from the style element.</p>
<p style="color: red;">Red, from the style attribute.</p>
</body>
</html>/* site.css */
p {
color: darkgreen;
font-size: 1.25rem;
}Both p rules have the same specificity, so the style element wins over site.css because it comes later. The inline declaration beats both.
The Elements panel and the Styles pane
In the Elements panel (called Inspector in Firefox), you see the live DOM tree. Click a <p> element. The Styles pane (Rules in Firefox) lists every rule that matches it. The list is ordered from the strongest rule at the top to the weakest at the bottom.
For the second paragraph you should see:
element.style { color: red; }. This is the inline declaration.p { color: blue; }, labeled withindex.htmland a line number. This is thestyleelement.p { color: darkgreen; font-size: 1.25rem; }, labeled withsite.cssand a line number. This is the external file.
The label on the right of each rule is how you find the origin. Clicking it opens the file at that line. This tells you which of the three methods produced the rule.
Reading overridden declarations
Declarations that lost are shown with a line through them. For the second paragraph, color: darkgreen and color: blue are both struck out. Only color: red is active. The font-size: 1.25rem declaration is not struck out, because nothing overrides it. The paragraph still gets that size from site.css.
A struck-out declaration is not an error. It means the cascade picked another value for that property.
Experimenting live
You can edit values in the Styles pane and see the page change immediately.
- Click a value, such as
red, and type a new one. - Click the checkbox next to a declaration to turn it off. Turning off
color: redshould make the paragraph turn blue. - Click inside a rule and type a new property to add it.
These edits change only the page in memory. Reloading discards them. To keep a change, copy it into your source file.
The Computed pane
The Computed pane shows the final value of each property after the cascade. Expand a property such as color to see the list of rules that set it, in winning order. Use this pane when the Styles pane has many rules and you only care about one property.
Computed values are resolved. For example, 1.25rem appears as a pixel value such as 20px.
Checking that the stylesheet loaded
If an external style has no effect, open the Network panel and reload the page. Filter by CSS. Look for site.css and check the status code.
| Status | Meaning |
|---|---|
| 200 | The file loaded. |
| 304 | The browser reused its cached copy. |
| 404 | The path is wrong or the file is missing. |
A 404 on a stylesheet is one of the most common reasons external styles seem to do nothing. Check that the href path is relative to the HTML file, not to the CSS file.
Common errors
- Editing the wrong place. Changes made in the Styles pane vanish on reload. Copy them to your file.
- Stale cache. If your edit to
site.cssdoes not appear, the browser may be using an old copy. Reload with Ctrl+Shift+R, or tick "Disable cache" in the Network panel while the tools are open. - Inspecting the wrong element. A child can inherit or override a parent's style. Select the exact element that looks wrong.
Exercise
In the Styles pane, an element shows color: darkgreen from site.css with a line through it, and color: red under element.style without one. Which method set the winning value, and what does the line through darkgreen mean?
Show answer
The style attribute set the winning value, because element.style represents the inline declaration. The line means the declaration matched the element but lost in the cascade, so it is not applied.
Exercise
You added a link element for css/site.css, but nothing changes. Which panel do you open first, and what status code would confirm a wrong path?
Show answer
Open the Network panel, reload, and filter by CSS. A 404 status for site.css confirms the path is wrong or the file is missing.