Full answer
Third-party code sometimes sets styles with the style attribute or with !important. The cascade has a fixed order for these cases, and knowing it removes guesswork.
For normal (not important) declarations, the order from weakest to strongest is: the browser's styles, then author styles with layers and specificity, then inline style. An inline style beats a normal rule of any specificity.
For !important declarations, the order reverses between sources:
- A stylesheet rule with
!importantbeats a normal inline style. So.widget .title { font-size: 20px !important; }wins overstyle="font-size: 10px". An inline style with!importantbeats a stylesheet rule with!important. - Among important rules, the usual specificity and order apply. A more specific
!importantrule beats a less specific one. - With cascade layers, the order flips for important rules. An important rule in an earlier layer beats an important rule in a later layer, and unlayered important rules are weakest of all. So if the widget's stylesheet is in a later layer, an important rule in an earlier layer of yours wins without a long selector.
Safe approach:
- Check first whether the widget has a documented setting, a custom property or a
::part. That is better than any override. - Use the narrowest selector that targets only the widget, such as
.widget .title. - Use
!importantonly on the properties you must override, with a comment that explains why. - If you can place your CSS in an earlier layer, use
@layerso you can avoid specificity battles. - As a last resort, a script can change the inline style, but widget updates may bring it back.
The first paragraph is blue, because important rules in the earlier layer win. The second is 20px even though it has an inline size, because the stylesheet rule is important. The third is green, because the more specific important rule wins.
Document every override, so the next person can remove it when the widget is fixed.
<style>
@layer ours, widget;
@layer widget { .w .a { color: red !important; } }
@layer ours { .w .a { color: blue !important; } }
.w .b { font-size: 20px !important; }
.w .c { color: red !important; }
.w .c.fix { color: green !important; }
</style>
<div class="w">
<p class="a" id="a">Layered</p>
<p class="b" id="b" style="font-size: 10px">Inline loses to !important</p>
<p class="c fix" id="c">Specific important wins</p>
</div>Checked in Chrome 156 only. The widget is a stand-in written for the example, not a real third-party script. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Normal inline styles beat normal stylesheet rules.
- Important stylesheet rules beat normal inline styles.
- Among important rules, specificity decides.
- Layer order is reversed for important rules.
- Prefer documented settings and comment overrides.
Common mistakes
- Raising specificity again and again instead of using layers or the widget's own settings.
- Using !important on whole blocks of CSS.
- Overriding widget internals that change with each widget update, with no comment.
Follow-ups
- Which wins, an inline style or a stylesheet rule that has !important?
- Why is a documented widget setting better than an override?
Tips
- State the key rule: an important stylesheet rule beats a normal inline style.
- Say that layers reverse the order for important rules.