XML technologies / CSS / Advanced websites with CSS / Text effects and cascading / Cascading and inheritance in CSS / Importance (!important)

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

A property assigned the value !important always takes precedence in the cascade over all properties without this value.

It is therefore relatively easy to ensure that a style rule is not overridden by others. For example, to display all third-level headings on a page only in blue and indented 20 pixels to the right, specify the following style rule:

h3 {
  color: blue !important;
  margin-left: 20px !important;
}

If another style rule now attempts to give this heading a different color or padding, the browser simply ignores it and favors our rule declared “important.”

Remember, however, that even with !important you cannot be completely certain that your rule will be applied. Properties declared by the user as !important carry even more weight. If two style rules have equal importance, origin determines which is selected.

Since user settings cannot be controlled anyway, you should assume for your design that your rules with the value !important will not be overridden. User settings will compete with your stylesheets only very rarely; when they do, the responsibility lies with the user, not with you.

  

 

 

 

back to top