Book HomeCascading Style Sheets: The Definitive GuideSearch this book Friday 25th of July 2014 01:17:19 PM

Chapter 10. CSS2: A Look Ahead

Contents:

Changes from CSS1
CSS2 Selectors
Fonts and Text
Generated Content
Adapting to the Environment
Borders
Tables
Media Types and @-rules
Summary

In the course of writing this book, I vacillated back and forth over how to handle CSS2. It's a full W3C Recommendation, of course, but so little of it has actually been implemented correctly that it seemed almost a waste of time -- both mine and yours -- to talk about CSS2 in detail. After all, not only would I have to fake all of the screenshots (not to mention guess at correct behavior in a few cases), but you wouldn't be able to try out anything I discussed, since browsers wouldn't recognize your CSS2 rules.

On the other hand, CSS2 can hardly be ignored. So in the end, I settled on writing a chapter that talks about CSS2 in brief, abstract detail -- in other words, this chapter. The next edition of this book will almost certainly be driven by the need to add detailed information concerning CSS2, and will very likely be undertaken once

Figure 7-34

Figure 7-34. Equivalent style rules

In case you're wondering, under CSS1, there is no way to directly set the style for only a single side using something like border-top-style, since no such property exists in CSS1 (although that property, and others like it, were introduced in CSS2). You can, however, sneak around this limitation by declaring the style for a given border using one of the shorthand properties we'll discuss later in the chapter.

the dust settles and browsers start to correctly implement major portions of CSS2.

Also realize that, of the figures shown in this chapter, the vast majority are -- well -- faked. There was no other way to produce most of these examples. The point of telling you this is to spare you the frustration of trying to figure out how they were produced. So, with that in mind, here's a brief taste of what CSS2 can offer.

10.1. Changes from CSS1

Only a few CSS1 properties have gained new values. These were mostly concerned with addressing issues that did not exist, or were not considered, when CSS1 was written. The one standout is a new value called inherit, which represents a huge change to everything -- but more on that in a moment.

10.1.1. Additions and Changes to the display Property

The property display has received quite a few new values in CSS2. Now, in addition to block, inline, line-item, and none, we have run-in, compact, and marker (which we'll get to later), as well as a number of values specific to tables (which we'll also cover later on).

The display value compact has an effect similar to <DL compact> (assuming your browser supports that bit of HTML). Basically, if an element is set to display: compact, then it will appear in the margin of the next element, assuming there is enough room for it. Otherwise, both elements will be treated as block-level elements. Think of a "compacted" element as one that floats, but only if there is room for it to be displayed without altering the formatting of the following element, something like the illustration in Figure 10-1.

Figure 10-1

Figure 10-1. Compact display of a definition list

On the other hand, run-in has the effect of turning a block-level element into an inline element at the beginning of the following block-level element. Another way to think of it is that a block-level element set to run-in will be combined with the next block-level element so that the two together form a single block-level element.

Given this code:

<H3 STYLE="display: run-in;">A Heading.</H3>
<P>This is a paragraph of text....</P>

the result will look something like what's shown in Figure 10-2.

Figure 10-2

Figure 10-2. A run-in heading

The display type run-in can be applied to any block-level element, not just headings. However, this rule should only work if the next element is block-level and is not floating or positioned absolutely. So, for example, if you try to set an inline anchor to run-in, it won't have any effect.

Another change for display is that its default value is inline, not block, as was defined in CSS1. The authors have termed the original default value an error, so if you don't declare a value for display, it is assumed to be inline. Of course, your browser should have its own built-in HTML styles, so don't worry about your paragraphs suddenly running together!

10.1.2. More Inheritance

Finally, there is one very important new feature of CSS2 that belongs in this section: the value inherit. If you were to ask the question, "Okay, to which properties did inherit get applied?" the answer would be, "Every last one of them." There is not a single property in the whole of CSS that does not accept a value of inherit.

inherit is used to explicitly declare that a given computed value should be inherited from its parent. In other words, if the font-size for BODY is computed to be 14 points, then the declaration P {font-size: inherit;} would set paragraph text to 14 points in size, as long as the paragraphs are children of the BODY element. Similarly, you could make sure that hyperlinks always have the same color as the text that surrounds them by using the simple declaration:

A:link, A:visited {color: inherit;}

The power of this change should not be underestimated. In effect, you are able to override the specificity mechanism that ordinarily takes effect. Usually, hyperlinks are (for instance) blue unless you explicitly declare them to be otherwise -- and if you want differently colored links in different areas of the same page, you'd have to construct a different rule for each color.

Now, thanks to inherit, if it's okay to make them the same color as surrounding text, you just need one rule that will cover all circumstances. Note that I'm not saying this is a good idea, or the only thing for which inherit can be used. It's simply the most obvious possibility.



Library Navigation Links

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

variant of the font, and 900 as the heaviest.

In fact, there is no intrinsic weight in these numbers. The CSSspecification says only that each number corresponds to a weight atleast as heavy as the number that precedes it. Thus,100, 200,300, and 400 might all map tothe same relatively lightweight variant, while 500and 600 could correspond to the same heavier fontvariant, and 700, 800, and render dashed and dottedborders, since it does render them as solid,it's not behaving badly.

Figure 7-35

Figure 7-35. Using solid to stand in for unrecognized border styles

You may have noticed that all of the examples in this section hadborders of exactly the same width. That's because wedidn't define a width, so it defaulted to a certain value.Next, we'll find out about that default, and muchmore.

  • For nonroot elements that are not absolutely positioned, the containing block for an element is set as the content edge of the nearest block-level ancestor. This is true even in relative positioning, although it might not seem so at first.

  • For nonroot elements that are absolutely positioned using a position of absolute, the containing block is set to the nearest ancestor (of any kind) that elements in a document to be one-and-one-half times theirfont-size. You would declare:

    BODY {line-height: 1.5;}

    This scaling factor of 1.5 is passed down fromelement to element, and at each level the factor is used as amultiplier of the font-size of each element.Therefore, the following markup would be displayed as shown in Figure 8-64 (backgrounds added for illustrative purposes):

    P {font-size: 12px; line-height: 1.5;}browsers to make all links have a white background. When you design
    your page, though, you set anchors to have a white foreground, and
    you don't want a background on those anchors. In order to make
    sure that this happens, you would declare:
    

    A:link {color: white; background-color: transparent;}

    If you left out the background color, then your white foreground would combine with the user's white background to yield totally unreadable links.