Book HomeCascading Style Sheets: The Definitive GuideSearch this book Monday 20th of October 2014 10:05:18 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

7.4.5. Borders and Inline Elements

A lotof this story will sound pretty familiar because it's largelythe same as what we discussed with margins and inline elements.

In the first place, no matter how thick you make your borders oninline elements, the line-height of the element won't change.Let's set top and bottom borders on boldfaced text:

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 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.

UL UL, so the value ofsquare won't be inherited after all. Yourbrowser may vary.

In the case of ordered lists, CSS2 goes a great dealfurther than CSS1 to provide control over the ordering. For example,there is no way in CSS1 to automatically create subsection counterssuch as "2.1" or "7.1.3." This can, however,be done under CSS2 and is briefly discussed in Chapter 10, "CSS2: A Look Ahead".

Easiest to understand are the keywords. They have the effects you'd expect from their names; for example, top right would cause the background image to be placed in the top right corner of the element. Let's go back to the small yin-yang symbol:

BODY {background-image: url(yinyang.gif);
background-repeat: no-repeat;
background-position: top right;}

Incidentally, the result, shown in Figure 6-37, only apply them to the number 255 to get the resulting values.Let's say you have a color of 25% red, 37.5% green, and 60%blue. Multiplying each of those percentages by 255, we get 63.75,95.625, and 153. We need to round those off torgb(64,96,153), however, because only integers(whole numbers) are permitted when using numbers. Percentages canhave decimals, but these numbers can't.

Of course, if you already know the percentages, there isn'tmuch point in converting them into straight numbers. This notation is

Because the floated element is both within and without the flow, thissort of thing is bound to happen. What's going on? Well, thecontent of the paragraphs is being "displaced" by thefloated element. However, each paragraph's element width isstill as wide as its parent element. Therefore, its content areaspans the width of the parent, and so does the background. The actualcontent doesn't flow all the way across its own content area inorder to avoid being obscured behind the floating element.