Sub topic 8
Seeing which styles apply with developer tools
The previous chapters covered the cascade, source order, and specificity. This chapter shows how to check those rules on a live page. Browser developer tools (DevTools) show which declarations apply to an element, which ones lost, and which file each one came from.
This chapter assumes you can write a page with all three methods (style attribute, style element, linked file). It also assumes you know that equal specificity is settled by source order and that inline declarations usually beat selector rules. You need a current Chrome, Edge, Firefox, or Safari. The panel names differ slightly between browsers. The ideas are the same.
A practice page
Create a folder with two files. Open index.html directly from disk, or serve it with any local server.
/* css/site.css */
p {
color: darkgreen;
font-size: 1.25rem;
}
.note {
border-left: 4px solid gray;
padding-left: 1rem;
}<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>DevTools practice</title>
<link rel="stylesheet" href="css/site.css">
<style>
p { color: blue; }
.note { color: purple; }
</style>
</head>
<body>
<p>Plain paragraph.</p>
<p class="note">Paragraph with a class.</p>
<p class="note" style="color: red;">Paragraph with a class and an inline style.</p>
</body>
</html>Before you inspect anything, predict the color of each paragraph. The p rule in the style element comes after the linked file, so it beats p { color: darkgreen; }. The .note selector is more specific than p, so purple beats blue. The inline declaration beats both. The expected result is blue, purple, and red.
Opening DevTools and selecting an element
You can open DevTools in several ways:
- Press
F12. - Press
Ctrl+Shift+Ion Windows and Linux, orCmd+Option+Ion macOS. Safari needs the Develop menu enabled in its settings first. - Right-click the element on the page and choose Inspect.
The right-click route is the most useful because it selects the element you care about. You can also use the element picker, the arrow icon at the top-left of the DevTools window. It is also bound to Ctrl+Shift+C or Cmd+Shift+C. Hover over the page, then click the element to select it.
Selecting an element shows its HTML in the Elements panel (called Inspector in Firefox). Beside or below it is the styles pane, called Styles in Chrome and Safari and Rules in Firefox. This pane is where you will do most of your debugging.
Reading the Styles pane
The styles pane lists every rule that matches the selected element. Rules are ordered from highest priority at the top to lowest at the bottom. Select the third paragraph in the practice page and you will see something like this:
| Block in the pane | Where it comes from | What to notice |
|---|---|---|
| `element.style { color: red; }` | The `style` attribute on the element | Always at the top. The label is `element.style` in Chrome and Safari, and `element` in Firefox. |
| `.note { color: purple; }` | The `style` element, with a link such as `index.html:8` | The line number tells you where the rule is in the file. |
| `p { color: blue; }` | The `style` element | `color` is struck through because the inline declaration beat it. |
| `.note { border-left: ...; padding-left: ... }` | `site.css:6` | The link opens the external file at that line. |
| `p { color: darkgreen; font-size: 1.25rem; }` | `site.css:1` | `color` is struck through. `font-size` is not, because nothing beat it. |
| `p { display: block; margin-block: 1em; }` | `user agent stylesheet` | The browser's built-in defaults. They lose to any author rule that sets the same property. |
Three things in this table are worth remembering:
- Struck-through declarations lost. The rule matched the element, but another declaration for the same property won. Only one declaration per property is not struck through, and that is the winner.
- The source link names the method. A name like
site.css:6means the rule is in an external file. The HTML file name with a line number means astyleelement.element.stylemeans an inline attribute. - Ordering in the pane is not a complete explanation. It reflects the cascade's result for each property, but a rule can be high in the list and still lose one declaration, as
.notedoes forcoloragainst inline red.
Hover over a selector in the pane and the matching elements are highlighted on the page. That helps when a rule applies to more elements than you expected.
A warning icon next to a declaration usually means the browser did not understand it. A typo such as colour: red or a value like margin: 10 px is the usual cause. The browser throws away invalid declarations. Without DevTools you would see no effect and no error.
Using the Computed pane
The styles pane shows rules. The Computed pane shows results: the final value of every property on the selected element after the cascade is done. For the second paragraph, it lists color: rgb(128, 0, 128), because DevTools reports the resolved value rather than the keyword purple.
Use Computed when you ask, "What is this property actually set to?" Expand a property in the list and it shows the rules that set it. The winning rule is listed first, and the losing rules are listed below it with strikethroughs. Chrome and Firefox also let you click the source link beside a property to jump to the rule in the styles pane.
Computed also resolves relative units. font-size: 1.25rem becomes pixels, and margin: 1em becomes a length. This explains many "why is this 20px?" surprises.
The pane has a filter box. Type color to see only matching properties. In the styles pane, the filter box matches both property names and values, so you can search for blue and find every rule that sets it.
Editing styles live
Every value in the styles pane can be edited. This lets you test a fix before touching your files.
- Click a value, such as
purple, and type a new one. The page updates as you type. Arrow keys step through numbers, and Chrome also lets you cycle through some keywords. - Click the checkbox beside a declaration to turn it off and on. This is the quickest way to find which declaration is responsible for a visual effect.
- Click empty space inside a rule to add a new declaration.
- Click the
+button to add a new rule that targets the selected element.
DevTools also has a Force element state option. In Chrome it is the :hov button. It lets you set :hover, :focus, or :active on the element so you can inspect those rules without holding the mouse in place.
When you edit a value that came from site.css, the browser applies the change to its parsed copy of that file. Your file on disk is unchanged.
A debugging workflow
The following steps work for almost any "why doesn't my style apply?" question.
The first branch deserves a closer look, because "rule not listed at all" has several distinct causes.
The selector does not match
You wrote .notes and the class is note. Compare the selector against the element's attributes in the Elements panel. In the Console, document.querySelectorAll(".notes") returns an empty list when the selector matches nothing.
The stylesheet did not load
Open the Network tab, reload, and filter by CSS. Each stylesheet should show status 200 (or 304 for a cached response). A 404 means the href path is wrong. Remember that a relative href is resolved against the HTML page's location, not against the CSS file. Check that css/site.css really exists next to index.html. A stylesheet that loaded but contains a syntax error part-way through can also lose rules. The browser skips the broken rule and carries on with the next one.
The Network tab has a Disable cache checkbox that applies while DevTools is open. Turn it on when you edit external CSS and the page seems not to change. It rules out a stale cached copy.
The browser blocked the style
If the site sends a Content-Security-Policy header that does not allow inline styles, the browser refuses style elements and style attributes. In that case the rule is absent from the styles pane, and the Console shows an error that names the violated directive, such as style-src. A page with style-src 'self' would behave this way for all inline styles in the practice page, while still loading site.css. The Network tab will show the stylesheet as loaded, so the Console is the place to look.
The declaration lost the cascade
The rule is listed, and the declaration is struck through. Look further down or up the pane for the rule that is not struck through. Then compare:
- Does it have
!important? DevTools shows that explicitly. - Does it have higher specificity, for example
.noteagainstp? - At equal specificity, which rule comes later in source order? Remember that the order is the order of the stylesheets in the document. The
<link>and<style>elements are ranked by their position in thehead.
For the practice page, move the <link> below the <style> element and reload. Select the first paragraph. Now site.css comes later, so darkgreen wins and the style element's blue is struck through. The only thing you changed was source order.
Finding the source of a rule
When a stylesheet is large, the link next to each rule is the fastest way to find the source. Click it and DevTools opens the Sources panel (Chrome) or Style Editor (Firefox) at that line. From there you can search all loaded files. In Chrome, Ctrl+Shift+F or Cmd+Option+F searches across every loaded file, including CSS.
The same information is available for inline styles through the HTML. Selecting an element with style="color: red;" shows that attribute in the Elements panel. You can edit it there as plain text, or change it from the element.style block.
Common mistakes when reading DevTools
- Assuming the top rule always wins. Ordering is by priority, but the right test is whether the specific declaration is struck through.
- Trusting a live edit as a fix. It disappears on reload and does not change your files.
- Debugging a cached file. Use Disable cache or a hard reload before concluding that your edit did nothing.
- Reading Computed values as authored values.
rgb(...)and pixel lengths are resolved forms of what you wrote. - Forgetting the user agent stylesheet. Default margins and heading sizes come from the browser. They are overridden by any author rule, even a low-specificity one.
Exercise
In the practice page, you select the paragraph <p>Plain paragraph.</p>. You see p { color: blue; } from the style element and p { color: darkgreen; } from site.css. Which one is struck through, why, and what would you change in the HTML to reverse the result?
Show answer
The darkgreen declaration from site.css is struck through. Both selectors are p, so specificity is equal, and the style element comes after the <link> in the head, so its declaration is later in source order and wins. Moving the <link> below the <style> element reverses the result. darkgreen then wins and blue is struck through.
Exercise
A colleague says that their site.css rules have no effect, and the styles pane shows no rule from site.css for any element. Name two checks you would do in DevTools and what each one would tell you.
Show answer
First, open the Network tab, reload, and filter by CSS. A 404 status for site.css means the href path is wrong. Second, open the Console. A Content Security Policy error would mean the browser refused the stylesheet, which can happen if style-src does not allow its origin. If the file shows 200 and the Console is clean, check the selectors against the element's tag and class names in the Elements panel.