The way that an element is displayed is dictated by two things, Cascading and Inheritance.
If a particular property of an element is not specified in any rule applicable to that element, the property may be inherited from its parent element.
I say may be, because not all properties are inherited.
For example, font properties are, but margin and padding are not.
Then there is the added complication of Cascading, with the potential of multiple rules in multiple style sheets that could be applied.
As always, keeping things simple is the safest way. Keeping an external style sheet as simple as possible and just overriding those values using embedded styles when the situation calls for it, is a fairly safe method to adopt and shouldn't cause too many problems. Once you start using multiple style sheets from different pages and sites though, problems can occur. However, even with just one style sheet you may find conflicting rules for an element.
There are four factors to take into consideration in deciding which style declaration wins out, origin, weight, specificity and order.
This refers to the source of the declaration. It may be specified in a style sheet created by the author, it may originate from a user's style sheet or it could be the user agent's default style for that element. A personal style sheet can be constructed which can be used for viewing every web page. A user agent is software like an HTML browser which retrieves and renders Web content. Imported style sheets have the same origin as the style sheet that imported it.
Normally the author's style will override a user's style.
There is of course an exception.
A rule can be designated important by the use of the !important keyword.
h1 {color: red !important;}
In this situation a user's style will override the author's.
A CSS selector is assigned a specificity.
Generally speaking, the closer a rule's selector matches the element (i.e. it is more specific), the more likely this will be the rule to be applied.
For instance, in the example below, the selector of h1.warning is more specific than just h1 and so <h1 class="warning">...</h1> would be rendered in red.
h1.warning {color: red;}
h1 {color: blue;}
In this case it doesn't matter about the order of the rules as the latter one is a more generic rule. Specificity takes precedence over order and so the first rule would be applied. It is fairly obvious from this example how this happens but it is not so obvious when you throw another couple of selectors into the equation.
div#main h1 {color: black;}
h1#title {color: green;}
h1.warning {color: red;}
h1 {color: blue;}
The actual calculation of the specificity value is based on the composition of the selector itself, with different components awarded a particular value. These individual ratings are added up to provide the selector's specificity. There are three different types of component considered in the calculation, id selectors, non-id selectors (excluding pseudo-elements) and named elements. Each of these component types are assigned their own weighting, 100, 10 and 1 respectively but they are normally shown as a comma or hyphen separated list for clarity. The following steps are taken arrive at the value.
The table below gives some examples of this:
| selector | IDs | Other | Names | Specificity |
|---|---|---|---|---|
| h1 | 0 | 0 | 1 | 0,0,1 |
| li span em | 0 | 0 | 3 | 0,0,3 |
| .warning | 0 | 1 | 0 | 0,1,0 |
| h1.warning | 0 | 1 | 1 | 0,1,1 |
| h1#title | 1 | 0 | 1 | 1,0,1 |
| div#main h1 | 1 | 0 | 2 | 1,0,2 |
| div#nav a:hover | 1 | 1 | 1 | 1,1,1 |
| dd#first p:first-line | 1 | 0 | 2 | 1,0,1 |
Note in the last example, the pseudo-element :first-line is not included in the calculation.
If the origin, weight and specificity considerations do not result in a winner then the order of the rules is brought into action. The rule declared last will be the one that is applied.
It is important to remember this point, as it is often assumed, incorrectly, that an embedded style will override an external one.
The fact that it might do would be because the <link> element for the external style sheet precedes the embedded one.
If <link> is coded after <style> then the external rule will be applied.
Inline styles, which are applied directly to an element, will always take precedence over any other rule.
Top