Book HomeCascading Style Sheets: The Definitive GuideSearch this book Sunday 04th of December 2016 04:51:01 AM

Chapter 8. Visual Formatting


Basic Boxes
Block-Level Elements
Floated Elements
Inline Elements

In the previous chapter, we covered a great deal of information on how CSS handles the visual formatting of a document. However, we did this in a mostly practical fashion: lots of explanation about how things work, with only a little lip service paid to the questions of why. In this chapter, we turn to the theoretical side of visual rendering, with only occasional references to the practical.

You may wonder why it's necessary to spend an entire chapter on the theoretical underpinnings of visual rendering in CSS. The main reason is to cover all the bases. I attempted to provide as many and varied examples as possible in the previous chapters, but with a model as open and powerful as that contained within CSS, no book could hope to cover every possible way of combining properties and effects. Every reader of this book will obviously go on to discover new ways of using CSS for their own document effects.

In the course of so doing, you may encounter what seems like strange behavior on the part of user agents. With a thorough grasp of how the visual rendering model works in CSS, you'll be able to determine whether the behavior is a correct (if unexpected) consequence of the rendering engine CSS defines or whether


Percentage values refer to the width of the parent element.

These propertiesoperate as you'd expect by now. For example, the following tworules will give the same amount of padding:

you've stumbled across a bug that needs to be reported. (See Appendix A, "CSS Resources", for details on how to report problems with rendering engines.)

8.1. Basic Boxes

In the rendering of elements, CSS assumes that every element generates one or more rectangular boxes, called element boxes . (Future versions of the specification may allow for nonrectangular boxes, but for now everything is rectangular.) Each element box consists of a content area at its core. This content area is surrounded by optional amounts of padding, borders, and margins. These are considered optional because all could be set to a width of zero, effectively removing them from the element box. An example content area is shown in Figure 8-1, along with the surrounding regions of padding, border, and margins.

Figure 8-1

Figure 8-1. The content area and its surroundings

Each of the margins, borders, and padding can be set using various properties, such as margin-left or border-bottom. The content's background (for example, a color or tiled image) is also applied to the padding, while the margins are always transparent, allowing the background of any parent elements to be visible. In effect, the margins simulate the HSPACE and VSPACE attributes of images, although in a much more sophisticated fashion. Padding cannot be set to a negative value, but margins can. The effects of negative margins are explored later in this chapter.

The borders, on the other hand, have their own rules. Borders are generated using defined styles, such as solid or inset, and their color can be set using the border-color property. If no color is set, then the color of the border is based on the foreground color of the element's content. For example, if the text of a paragraph is white, then any borders around that paragraph will be white, unless a different border color is explicitly declared by the author. If a border style has "gaps" of some type, then the element's background is visible through those gaps; in other words, the border has the same background as the content and padding. Finally, the width of a border can never be negative.

There are differences in how different types of elements are formatted, however. Block-level elements are not treated in the same way that inline elements are, for example, and floated elements introduce a whole new level of complexity. Let's examine each type of element in turn.

Library Navigation Links

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

Now the document is a bit more even. Adjusting the placement of theDIVs will make it as even as possible, but sincewe still aren't quite finished, let's put that off yetagain.

At the end of the article is a block of text that says a few wordsabout the author of the piece. This is in a smaller text size,italicized, and separated from the rest of the article by a smallspace and a thin line. We could put a horizontal rule in for theline, but let's stick to CSS1 whenever possible. The followingreplication apply to border styles, just as they did with margins andpadding. Thus, the following two statements would have the sameeffect, as depicted in Figure 7-34:

P.new1 {border-style: solid dashed none;}P.new2 {border-style: solid dashed none dashed;}
Figure 7-34

Figure 7-34. Equivalent style rules

In case you're wondering, under CSS1, there is no way todirectly set the style for only a single side using something likeborder-top-style, since no such property exists in contains an inline boldface element, and the second an absolutely positioned boldface element. In the second paragraph, the styles used would be something like what is shown here:

P {position: relative;}   /* establish containing blocks */
<B STYLE="position: absolute; top: auto; right: 0; bottom: 0; left: auto;
width: 8em; height: 4em;">...</B>
Figure 9-19

Figure 9-19. The effects of absolute positioning

For the most part, the text in both paragraphs looks fairly normal. In the second one, however, the place where the boldface elementAs you can see, @import is found inside theSTYLE container. It must be placed there, beforethe other CSS rules, or else it won't work at all.

<STYLE TYPE="text/css">@import url(styles.css); /* @import comes first */H1 {color: gray;}</STYLE>

Like LINK, there can be more than one@import statement in a document. Unlike to :hover, you can specify a hover style very easily:

A:link {background: white; color: blue;}
A:hover {background: blue; color: white;}

These styles will cause anchors to "reverse" in color when the mouse pointer hovers over them, as illustrated in Figure 10-9.

Figure 10-9

Figure 10-9. Hover styles

As a matter of fact, the rule for A:hover would be