Book HomeCascading Style Sheets: The Definitive GuideSearch this book Friday 20th of April 2018 04:36:40 PM

Chapter 8. Visual Formatting

Contents:

Basic Boxes
Block-Level Elements
Floated Elements
Inline Elements
Summary

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 you've stumbled across a bug that needs to be reported. (See Appendix A, "CSS Resources", for details on how to report problems 30px , the top left corner of the background imagewill be 20 pixels to the right of, and 30 pixels below, the top leftcorner of the containing element, as in Figure 6-43:

BODY {background-image: url(bg23.gif);background-repeat: no-repeat;background-position: 20px 30px;}
Figure 6-43

Figure 6-43. Offsetting the background image using length measures

This is, of course, different than percentage values, in the sensethat the offset is simply from one top left corner to another. In 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.

In order to successfully load an external style sheet, LINKmust be placed inside the HEAD element but may notbe placed inside any other element, rather likeTITLE or STYLE. This will causethe web browser to locate and load the style sheet and use whateverstyles it contains to render the HTML document, in the manner shownin Figure 1-2.

And what is the format of an external style sheet? It's simplya list of rules, just like those we saw in the previous section andtext-indent of 0 for any paragraph which immediately follows any H1 element. However, since the paragraph here is the child of a DIV, it doesn't immediately follow the H1. Therefore, we would need to add a child selector and a first-child pseudo-class:

H1 + DIV > P:first-child {text-indent: 0;}

This will match any paragraph that is the first child of a DIV that immediately follows an item under CSS1 -- CSS2 adds a few more, all of which arementioned in Chapter 10, "CSS2: A Look Ahead" -- and one shorthandproperty to tie them all together. These properties are used toaffect the type of bullet used in a list, to replace the bullet withan image, and to affect where the bullet or image appears in relationto the text of the list item.

Just in case you're unfamiliar with the concept of a"bullet," it's the little decoration to the side ofa list item, as depicted in Figure 7-79.

In an unordered list, these will be little symbols, but in an ordered list, the bullet could be a letter or number.

7.7.1. Types of Lists

This part will probably seem very familiar to those of you who have been fiddling with lists in HTML. In order to change the type of counter or bullet used for a list's items, you would use the list-style-type.