CSS !important does not override styles from external stylesheets
This has nothing to do with CSS specificity or !important
. You have a rule in main.css that says:
#legend .label {
color: black;
}
The selector is targeting the .label
elements directly and giving them a color, which prevents them from inheriting the color from the body or some other ancestor. An !important
property applies only to the element that is targeted; it does not force other elements to inherit that property. In other words, a specified rule always takes precedence over an inherited rule, even if that inherited rule is !important
.
See the spec:
User agents must first assign a specified value to each property based on the following mechanisms (in order of precedence):
- If the cascade results in a value, use it.
- Otherwise, if the property is inherited and the element is not the root of the document tree, use the computed value of the parent element.
- Otherwise use the property's initial value. The initial value of each property is indicated in the property's definition.
— http://www.w3.org/TR/CSS21/cascade.html#specified-value
CSS: !important not overriding other rules
Try this:
.navbar .navbar-nav>li>a {
color: red;
}
How to override external css marked as !important?
I'm assuming you can't simply change the external CSS file.
This sort of thing is horrible to deal with and you should write the owner of that CSS file a condescending letter. Once you're done with that, you have to win the specificity battle. CSS selectors apply according to which one is the most specific. When !important is used, it means, "screw the specificity of anything else, use me."
However, when two selectors that target the same element both have a property with !important, the specificity kicks back in again (fun huh). Now this sort of war is best avoided (hence the letter and ideally slashing off important from the offending file), but you can do something like the below in your style sheet, which is a more specific selector than just the body tag AND has !important.
html body { font-size:9px !important;}
or
* body { font-size:9px !important;}
This sort of war is like nuking the body tag from space, so beware the collateral damage of this.
EDIT: Oh by the way inline styles beat out external stylesheets and inline blocks, such as your style attribute, and therefore would work yes, but if you're working on a site with more than one page that technique is obviously painful to maintain. The above approach will allow you to keep the override in an external stylesheet. Cheers.
Does embedded css always override external css?
It doesn't matter if your stylesheet is within <style>
-tags or externally and linked with <link />
. The last one has always precedence, they could even be in the same external file, really just the order of the selectors and their specificities matter.
However, inline CSS using the style=".."
attribute always has precedence, because it's most specific. To override that, you would have to use !important
. Properties in style=".."
using !important
cannot be overridden.
External CSS !important overriding inline !important
You have apparently confused inline style origin that originates from physical HTML attribute (<el style="[css declarations]">
) with in-page style sheet that has "author" level origin specificity and originates either from <style>[css rules]</style>
or <link rel="stylesheet" href="[css file url]">
.
Style and link elements in page are origin-wise equal, attribute is more origin-wise specific.
It is not possible to beat !important
rules from inline origin level (attribute) with !important
rule from author level origin.
Overriding CSS inline with css file using !important does not work
I would erase the [style]
and add the div
to give it even more specifity, like:
div.selector.selectorarrow.selectorarrowleft {
position: relative !important;
top: 10px !important;
}
How to override !important?
Overriding the !important modifier
- Simply add another CSS rule with
!important
, and give the selector a higher specificity (adding an additional tag, id or class to the selector) - add a CSS rule with the same selector at a later point than the existing one (in a tie, the last one defined wins).
Some examples with a higher specificity (first is highest/overrides, third is lowest):
table td {height: 50px !important;}
.myTable td {height: 50px !important;}
#myTable td {height: 50px !important;}
Or add the same selector after the existing one:
td {height: 50px !important;}
Disclaimer:
It's almost never a good idea to use !important
. This is bad engineering by the creators of the WordPress template. In viral fashion, it forces users of the template to add their own !important
modifiers to override it, and it limits the options for overriding it via JavaScript.
But, it's useful to know how to override it, if you sometimes have to.
Related Topics
Dynamically Set a CSS Property Based on a Template Value
Controller Specific Stylesheets in Rails 3: Inheritence
Center a Row Using Bootstrap 3
How to Exclude a Specific Element from Inheriting CSS Rules
Css: Flex Box Not Printing All Pages on Firefox
Changing the Order of Grid Item Stacking in Material-Ui
How to Customize Style of Bootstrap $Modal
Linear Gradient in Chrome and Safari Browsers
Why Aren't Browsers Smart Enough to Hardware Accelerate Without Tricks
Angular Flex-Layout with Ngfor
How to Change Customized Carousel Indicator Background Color
CSS Grid: Content to Use Free Space But Scroll When Bigger
Expand Container Div with Content Width
Change Svg Viewbox Size with CSS
How to Remember in CSS That Margin Is Outside the Border, and Padding Inside
Body Set to Overflow-Y:Hidden But Page Is Still Scrollable in Chrome