|
Inheritance
The order in which style instructions are obeyed is called Cascading. When you have a linked, embedded, and inline style sheet all defined on the same page, the order of precedence is ascending. For example, if both an embedded and inline style sheet attempts to manipulate the same aspect of the same element, inline dominates.
Inheritance refers to how the style of one element in "passed on" to another provided no explicit style is assigned to the later. Some style declarations (ie: font-family) are inherited, meaning they affect not just the element it was assigned to, but elements under it as well:
<P style="font-family:Verdana">This font is Verdana. <b>So is this font</b></P>
This font is Verdana. So is this font
Both P and B carry the font Verdana, even though the style was only defined on P. Inheritance exists to save the user the hassle of redefining styles that are obviously intended for everything the element encapsulates.
I now want a green 'H1' element with an emphasized element inside:
<H1 STYLE={background-color:palegreen}>
The headline <EM>is</EM> important!</H1>
The headline is important!
If no background color has been assigned to the 'EM' element, the emphasized "is" will inherit the color of the parent element, i.e. it will also appear on green. Other style properties are likewise inherited, e.g. 'font-family' and 'font-size' if they have been defined for H1 either in the HEAD or in this (H1) parent element.
Remember, in this case, EM is a child of H1, because it is contained within the H1.
The H1 element was changed using 'inline style' and the end tag for the element ended the special properties. H1 now reverts to 'normal'.
Cascading Order
When multiple style sheets are used, the style sheets may fight over control of a particular selector. In these situations, there must be rules as to which style sheet's rule will win out. The following characteristics will determine the outcome of contradictory style sheets.
- ! important
Rules can be designated as important by specifying ! important. A style that is designated as important will win out over contradictory styles of otherwise equal weight. Likewise, since both author and reader may specify important rules, the author's rule will override the reader's in cases of importance. A sample use of the ! important statement:
BODY { background: url(bar.gif) white;
background-repeat: repeat-x ! important }
- Origin of Rules (Author's vs. Reader's)
As was previously mentioned, both authors and readers have the ability to specify style sheets. When rules between the two conflict, the author's rules will win out over reader's rules of otherwise equal weight. Both author's and reader's style sheets override the browser's built-in style sheets.
Authors should be wary of using ! important rules since they will override any of the user's ! important rules. A user may, for example, require large font sizes or special colors due to vision problems, and such a user would likely declare certain style rules to be ! important, since some styles are vital for the user to be able to read a page. Any ! important rules will override normal rules, so authors are advised to use normal rules almost exclusively to ensure that users with special style needs are able to read the page.
- Selector Rules: Calculating Specificity
Style sheets can also override conflicting style sheets based on their level of specificity, where a more specific style will always win out over a less specific one. It is simply a counting game to calculate the specificity of a selector.
- Count the number of ID attributes in the selector.
- Count the number of CLASS attributes in the selector.
- Count the number of HTML tag names in the selector.
Finally, write the three numbers in exact order with no spaces or commas to obtain a three digit number. (Note, you may need to convert the numbers to a larger base to end up with three digits.) The final list of numbers corresponding to selectors will easily determine specificity with the higher numbers winning out over lower numbers. Following is a list of selectors sorted by specificity:
#id1 {xxx} /* a=1 b=0 c=0 --> specificity = 100 */
UL UL LI.red {xxx} /* a=0 b=1 c=3 --> specificity = 013 */
LI.red {xxx} /* a=0 b=1 c=1 --> specificity = 011 */
LI {xxx} /* a=0 b=0 c=1 --> specificity = 001 */
- Order of Specification
To make it easy, when two rules have the same weight, the last rule specified wins.
|