Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS !important does not override styles from external stylesheets

Tags:

css

In a stylesheet, I have this:

body {
  color: white !important;
}

Notice how it doesn't work on the text on the right side of this page:

https://www.graf.ly/graph_templates/56/

You can inspect the text, and see that the style was applied, but then overridden. How is this possible?

I thought !important ignores CSS specificity, and acts as a directive to always use that style. I've never seen this behavior before.

Note:

Don't be fooled by the white text by the graph's axis, that is a text svg element and was colored with fill: white !important.

Also, I'm well aware of the correct usage of !important. So please no comments or answers saying "you should never use !important". That is not the question :)

like image 635
Don P Avatar asked Jun 06 '14 05:06

Don P


People also ask

Can Important be overridden in CSS?

important is if you have to override a style that cannot be overridden in any other way. This could be if you are working on a Content Management System (CMS) and cannot edit the CSS code. Then you can set some custom styles to override some of the CMS styles.

Does internal CSS override external?

Therefore, when it is rendered in the browser, internal css will override external css . div-color class because of specificity rules, and inline css will override internal css . div-color class because inline css has a higher priority than internal and internal has more than external.

How do I override CSS class in external CSS file?

html worked fine, overrides the external css. Show activity on this post. Either apply the style="padding:0px;" on the content div inline (not recommended), or load your style after you load your external style sheet.

Do inline styles override stylesheets?

Inline styles added to an element (e.g., style="font-weight: bold;" ) always overwrite any normal styles in author stylesheets, and therefore, can be thought of as having the highest specificity.


1 Answers

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):

  1. If the cascade results in a value, use it.
  2. 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.
  3. 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

like image 119
user3466437 Avatar answered Nov 09 '22 01:11

user3466437