Selector specificity not working for !important rules
See original GitHub issueWhen 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:
- Created 8 years ago
- Comments:8 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If they are equal in specificity, then the last rule indeed wins.
One more thing. Today I’ve found out, that source of style is also ignored with regards to important. For the following HTML:
the output will be:
while it should be: