question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Selector specificity not working for !important rules

See original GitHub issue

When two selectors match the same element, and both have !important, then the selector specificity is ignored and the last rule wins:

inlineCss('<style>p a {color: red!important} a{color: black!important}</style>  <p><a>test', {url: '/'}).then(s=>console.log(s))
// '<p><a style="color: black;">test</a></p>'
inlineCss('<style>a{color: black!important} p a {color: red!important}</style>  <p><a>test', {url: '/'}).then(s=>console.log(s))
// '<p><a style="color: red;">test</a></p>'

However for the browsers it’s different. No matter which rule goes first, if both have !important in them, the more specific wins.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
arty-namecommented, Apr 6, 2016

If they are equal in specificity, then the last rule indeed wins.

0reactions
bandrzejczakcommented, Jul 19, 2016

One more thing. Today I’ve found out, that source of style is also ignored with regards to important. For the following HTML:

<style>
p {
  color: red !important;
}
</style>
<p style="color: blue !important">test</p>

the output will be:

<p style="color: red !important">test</p>

while it should be:

<p style="color: blue !important">test</p>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to tackle CSS specificity issues and when to use the ...
If the CSS selectors with conflicting CSS rules have equal specificity, then the browser will use the source order rule and apply the...
Read more >
Specificity - CSS: Cascading Style Sheets - MDN Web Docs
The specificity algorithm calculates the weight of a CSS selector to determine which rule from competing CSS declarations gets applied to an ...
Read more >
More specific CSS rule is not working - Stack Overflow
In case two rules of equal specificity apply, the winner is the last one declared in the CSS. Save this answer. Show activity...
Read more >
Why doesn't my CSS rules apply?, CSS specificity - Medium
The specificity is one of the things that generates more confusion in the web development. The specificity defines the weight in CSS classes...
Read more >
Importance of CSS Specificity and its best practices
Tips to remember when dealing with specificity issues · The universal selector * has low or no specificity. · If we are writing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found