CSS Syntax (Part 4)
First Line Pseudo-element
Often in newspaper articles, such as those in the Wall Street
Journal, the first line of text in an article is displayed in bold
lettering and all capitals. CSS1 has included this capability as a
pseudo-element. A first-line pseudo-element may be used in any block-level element
(such as P H1 , etc.). An example of a first-line pseudo-element would be:
P:first-line { font-variant: small-caps;
font-weight: bold }
First Letter Pseudo-element
The first-letter pseudo-element is used to create drop caps and other effects. The first letter
of text within an assigned selector will be rendered according to the value
assigned. A first-letter pseudo-element may be used in any block-level element. For example:
P:first-letter { font-size: 300%; float: left }
would create a drop cap three times the normal font size.
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.
1. 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 }
2. 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.
3. 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.
a. Count the number of ID attributes in the selector.
b. Count the number of CLASS attributes in the selector.
c. 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 */
4. Order of Specification
To make it easy, when two rules have the same weight, the last rule specified wins.
CSS Syntax (Part 1) |
CSS Syntax (Part 2) |
CSS Syntax (Part 3)
CSS Syntax (Part 4)
|