Trace stylesheet loading and source order in DevTools to find why a declaration is missing or overridden.
When more than one CSS declaration tries to set the same property on an element, the browser must decide which one to apply. The order of the declarations can decide the result, but only after other priority rules have been considered.
Browser developer tools help you investigate these conflicts and find other reasons why a style may not work as expected.
Suppose a stylesheet contains two rules for paragraphs:
p {
color: blue;
}
p {
color: red;
}Both rules target paragraphs and set the color property. Because these declarations have equal priority under the other CSS rules, the later declaration wins. The paragraph text appears red.
This is called source order: the order in which CSS declarations appear. Source order helps resolve a conflict when the declarations are otherwise equal in priority.
This page links to two stylesheets. Save both CSS files beside the HTML file, then read the two links in document order.
p {
color: red;
}The browser uses the stylesheets in document order. Here, style.css comes after base.css. Because the two declarations have equal priority under the other cascade rules, the later color: red declaration wins.
If you swap the two <link> elements, the blue declaration comes later and the paragraph appears blue.
An internal <style> element and an external stylesheet can also contain conflicting rules. Their position matters when the declarations are otherwise equal in priority.
For example, this internal rule comes after the linked stylesheet:
<link rel="stylesheet" href="base.css">
<style>
p {
color: red;
}
</style>If base.css contains the earlier p { color: blue; } rule, the paragraph appears red. If the <style> element comes before the link instead, the blue declaration appears later and wins in this example.
Internal CSS does not automatically have priority over external CSS. Similarly, a later rule does not always win just because it appears later: other cascade rules may give a declaration higher priority first. Inline styles can also conflict with other styles, so placing a style directly on an element does not guarantee that there will be no conflicts.
In the previous lesson you used comments to disable one declaration. When investigating a larger stylesheet, distinguish a comment that labels a rule from one that disables it.
/* Card appearance */
.card {
background-color: white;
padding: 20px;
}
/*
These styles control
the card's appearance.
*/Comments help explain what a rule is for. They can also temporarily disable CSS while you investigate a problem:
/*
.card {
margin: 40px;
}
*/The browser does not apply the commented-out rule. You can remove the comment markers when you want to use the rule again.
To inspect an element, right-click it on the page and select Inspect. In the developer tools, look for the Styles panel. It shows declarations associated with the selected element, including declarations that have been overridden.
When a style does not appear as expected, use the panel to work through the problem:
Changes made in DevTools let you test an idea in the browser. Make the corresponding change in your CSS file when you have found the fix.
Try it yourself
HTML + CSS
/* ... */ to explain CSS or temporarily disable it.