I was reading a decent article here on this topic: http://www.plus2net.com/html_tutorial/css-types.php It came out highest ranked by Google for the search term css style sheets priorities. However I think the site misinforms you and is incomplete! Can someone confirm my suspicions?
1) User Defined style is second lowest priority. In order to override other styles with it, you need to use !important to move it to highest. 2) It fails to mention the relative priorities of <link> versus @import and @import within <link>
A more precise ordering would be (1 wins over 2 etc) :
Michael Bowers Pro CSS & HTML Design Patterns is a good source for this too. But it fails to mention inline.
Is there anything else missing?
PS: I was inferring !important was missing from 2-8. So User defined appears twice. Once with important, the second time without it. So user defined is in essence second lowest. The !important can naturally be applied at any level.
1) Inline style: Inline style has highest priority among all. 2) Id Selector: It has second highest priority. 3) Classes, pseudo-classes and attributes: These selectors has lowest priority.
Cascading Order If different styles are specified for HTML elements, the styles will cascade into new styles with the following priority: Priority 1: Inline styles. Priority 2: External and internal style sheets. Priority 3: Browser default.
Inline styles have the highest priority over the external style and external styles. Inline styles take priority over embedded styles and external styles. Inline-styles are applied directly to an element via the style attribute.
Rather than thinking of it in terms of most important and least important, think of it as cascade order. All the styles are applied, but the last applied one is the one which you see. Styles are applied in the following order:
link
or @import
)Within any one of the first three, styles are applied from least specific to most specific (then from top to bottom if the most specific can't be determined). So a tag-selected style will be applied before a class-selected style, so if they disagree about what style should be applied, the class-selected one will win. There is no rule about whether link
or @import
should be applied first, so they are mixed together and the least-specific-to-most-specific rule applies.
!important
makes a less specific style get applied after a more specific one, and an external style sheet style get applied after an internal one or an inline style. I would advise against using !important
wherever possible though, as it can cause some pretty confusing results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With