Book HomeCascading Style Sheets: The Definitive GuideSearch this book Saturday 26th of July 2014 09:14:30 PM

2.7. Specificity

Given the existence of inheritance, one might well wonder what happens in a circumstance such as this:

.grape {color: purple;}
H1 {color: red;}
<H1 CLASS="grape">Meerkat <EM>Central</EM></H1>

Since the selectors H1 and .grape can both match the H1 element shown, which one wins? As it happens, .grape is the correct answer, and so the H1 element will be colored purple. This happens because of the specificity of the two rules, and the rules CSS has to deal with such situations.

Specificity describes the relative weights of various rules. According to the specification, a simple selector (e.g., H1) has a specificity of 1, class selectors have a specificity of 10, and ID selectors a specificity of 100. Thus the following rules would have the noted specificity:

H1 {color: red;}                    /* specificity = 1 */
P EM {color: purple;}               /* specificity = 2 */
.grape {color: purple;}             /* specificity = 10 */
P.bright {color: yellow;}           /* specificity = 11 */
P.bright EM.dark {color: brown;}    /* specificity = 22 */
#id216 {color: blue;}               /* specificity = 100 */

Thus, the rule for #id216 has a much higher specificity, and therefore more weight, than any of the others listed. In cases where more than one rule can apply to an element, the styles with the higher weight win out.

2.7.1. Inheritance and Specificity

Within the framework of specificity, inherited values have, effectively, a specificity of 0. This means that any explicitly declared rule will override an inherited style. Therefore, no matter how much weight a rule might have, it is only inherited if no other rule can be applied to the inheriting element.

For example, consider the following:

BODY {background: black;}
LI {color: gray;}
UL.vital {color: white;}

You would likely expect that all list items would be gray except for those which are found in lists with a class of vital, in which case they'll be white. However, as Figure 2-26 demonstrates, this is not the case.

Figure 2-26

Figure 2-26. Apparently incorrect behavior

Why does this happen? Because the explicit declaration with the selector LI wins out over the value which might have been inherited from the UL.vital rule.

Let's look at this process in a little more detail. Given the following markup, the emphasized text will be gray, not black, since the rule for EM outweighs the value inherited from the H1:

H1#id3 {color: black;}   /* specificity = 101 */
EM {color: gray;}        /* specificity = 1 */
<H1 ID="id3">Meerkat <EM>Central</EM></H1>

This is because the specificity of the second rule (1) is higher than the specificity of the inherited value (0). The fact that the original specificity of the H1#id3 rule is 101 has no effect on the inherited value, whose weight is still 0.

If the intention is to have H1s be consistently black, while EM text in all other circumstances should be red, then the following would be a good solution:

H1, H1 EM {color: black;}   /* specificity = 1, 2 */
EM {color: red;}            /* specificity = 1 */

Given these rules, EM text in any circumstance except within an H1 will be red. However, EM text inside H1 elements will be black, because the specificity of their selector (2) is greater than that of the second rule (1). Note that since, due to selector grouping, there are effectively two rules in the first statement (one for H1 and one for H1 EM ), there are also two specificities -- one for each rule.

Elements with a STYLE attribute are defined under CSS1 to have a specificity of 100, just as though they were ID selectors such as #id3. In practice, however, this specificity value is somewhat higher, since the value of a STYLE element seems to outweigh most normal rules, even those which technically have a higher specificity (such as H1#id3 EM ). In other words, the following markup will generally have the result shown in Figure 2-27:

H1#id3 EM {color: gray;}
<H1 ID="id3">Meerkat <EM STYLE="color: black;">Central</EM>!</H1>
Figure 2-27

Figure 2-27. Inline styles have high specificity

You might choose to treat STYLE value as having a specificity value of, say, 1,000, although this interpretation is not supported by the CSS specification and so cannot be relied upon. Finally, pseudo-elements are ignored altogether when calculating specificity, but pseudo-classes are treated like regular classes.

There is one other wrinkle in the specificity picture, which is a way to pretty much override the entire specificity mechanism.

2.7.2. Importance

Ever felt like something is so important that it outweighs all other considerations? Well, it's possible to mark certain rules as being more important than others. These are called important rules due to the way in which they are declared and also because of their very nature. An important rule is marked by inserting the phrase !important just before the terminating semicolon in a rule:

P.dark {color: #333 !important; background: white;}

Here, the color value of #333 is marked !important, whereas the background value of white is not. If you wish to mark both rules as important, then each rule will need its own !important:

P.dark {color: #333 !important; background: white !important;}

It is important to ensure that you place the !important correctly, or else the rule can be invalidated. The !important always goes at the end of the declaration, right before the semicolon. This is especially important -- no pun intended -- when it comes to properties that allow values which contain multiple keywords, such as font:

P.light {color: yellow; font: 11pt Times !important;}

If the !important were placed anywhere else in the font declaration, then that entire declaration would very likely be invalidated and none of the styles applied.

Rules that are marked !important do not have a defined specificity value, but authors can assume that they have a conveniently high value, such as 10,000 -- in other words, a value that outweighs all others. Note that while author-defined styles are treated as having a greater weight than reader-defined styles (see Section 2.8, "The Cascade", later in this chapter), the reverse is true of !important rules: important reader-defined rules take precedence over author-defined styles, even those marked !important.

Indeed, an !important rule will override the contents of an inline STYLE attribute. Thus, given the following code, the result will be gray text, not black:

H1 {color: gray !important;}
<H1 STYLE="color: black;">Hi there!</H1>

There is one last scenario to consider. Consider the following:

P#warn {color: red ! important;}
EM {color: black;}
<P ID="warn">This text is red, but <EM>emphasized text is black.</EM></P>

Remember that inherited values always have a specificity of 0. This is true even if the rule from which the value comes has an !important attached. All of its importance is lost outside the elements which match that rule.

WARNING

As of this writing, very few browsers implement !important. Internet Explorer 5 and Opera 3.6 have it right, but that's all. On the other hand, !important is expected to be supported in Navigator 6.

Client and Server side - Application Servers

The 2nd category of Java applications called Java Application Servers (or app servers) and they make good use of XML. Unlike client side graphical Java apps (from the previous section) which are very standalone in their operations, app servers tie many different networked software components together in order to provide information from multiple sources to a set of client side Java apps or web browsers (maybe even running on different devices). This is shown in Figure 2. An app server is actually a conglomeration of several distributed and client/server software systems. So when you write an app server, you are actually writing many different software systems which are all networked to work together, to process information that comes from various sources, and distribute this information to a set of client apps (that you also have to write) running on different devices and platforms.

How can XML help app servers do their work? As you can see in Figure 2, in order for the app server to harvest information from such a rich variety of sources, there must be some common ground between all of these sources (each of which might be running on a different hardware and software system). This common ground is the information which flows throughout the entire system, regardless of what source the information comes from. CORBA is an example of tying disparate systems together based on the interfaces that certain remote objects implement. XML does the same thing for data. It allows these disparate systems to share information in a medium that consists only of pure information (and the structural relationships that exist inside of that information). By taking the lowest common denominator approach by using plain text to encode data, XML allows these systems to talk with each other without requiring any special binary information format converters or other service layers to translate between binary formats (for encoding data). Also, since HTTP already supports transmission of plain text, it is completely natural to move XML around using the Hyper Text Transfer Protocol through firewalls and disparate networks. This is shown in Figure 3. XML can be transmitted between systems using one of the most prevalent protocols in use today, Hypertext Transfer Protocol or HTTP 1.1 (which is the protocol of the web).



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.

As previously discussed, if no colors are defined, then the defaultcolor is the foreground color of the element. Thus, the followingdeclaration will be displayed as shown in Figure 7-44:

P.shade1 {border-style: solid; border-width: thick; color: gray;}P.shade2 {border-style: solid; border-width: thick; color: gray;border-color: black;}

The result is that the first paragraph has a gray border, havingtaken the value gray from the foreground color of depending as it does on "words," which are difficult to define in a programmatic way.

Example

vertical-alignIE4 P/P IE5 P/Y NN4 N/N Op3 P/-

Used to set the vertical alignment of an element's baseline with respect to its line-height. Negative percentage values are permitted, and will cause the element to be lowered, not raised.

Example

In technical terms, every element in a line generates acontent area, which is determined by the size of thefont. This content area also generates an inlineboxwhich is, in the absence of any other factors, exactly equal to thecontent area. However, the leading generated byline-height can increase or decrease the height ofthe inline box. This is done by dividing the leading in half andapplying each half-leading to the top and bottom of the content area. elements based on what attributes they have. As an example, the rules shown here will match the following two INPUT tags, respectively:

INPUT[type="radio"] {color: #333333;}
INPUT[type="checkbox"] {color: #666666;}
<INPUT TYPE="radio" NAME="r2" VALUE="A ">
<INPUT TYPE="checkbox" NAME="c3" VALUE="one ">

This allows you to dispense with the classes altogether, at least in this instance. See the Chapter 10, "CSS2: A Look Ahead", for more details Explorer 4.x and 5.x both recognize :hover on anchors only. As of this writing, no other browser will recognize :hover under any circumstances.

10.2.3.2. :focus

Similar