![]() ![]() |
Styling Inside the BoxCSS box properties -- once they're fully supported by the major browsers -- will enable Web developers to position text and images without the use of tables. This month, we'll take a look at some of the CSS box properties to show their potential advantages. We'll start out with an explanation of the difference between margins and padding, and toward the end of the article, we'll explain how to float elements to create some interesting effects.
Box properties are used to control the appearance of an element's "box" -- thus the name. A "box" is the area of space an element occupies when displayed by the browser. For example, if you were to draw four lines to form a square around this paragraph, that would be the box for this paragraph. Because there are a lot of box properties, we won't get to all of them in this article. Instead, we'll hit two major groups of properties. (The table at right lists all of the box properties defined in the CSS1 specification.) It's important to remember that box properties are one of the areas of the CSS specification that aren't fully supported by the major browsers. To see which properties are supported, I'd recommend you peruse the CSS Compatibility Charts. A basic example (Figure 1) shows a diagram of the various components of an element's box. The olive-colored area is the Content Box, so named because it's the box containing the text. Beyond this box is Padding, then the Border, and finally the Margin of the box. Added together, these areas make up the element's box, and there are properties that can address just about every part of the box. Note that the line around the outer edge of the margins is included here for illustrative purposes; there are no properties for setting visible lines at that point. Also, as we'll see in a moment, the padding should really be a different color, but I left it gray for clarity's sake.
Padding and MarginsAt the outer edge of the element box, outside of the borders, are the margins. These are areas of blank space found around the box through which the background is visible. For example, if you have a paragraph with a white background, and your page's background is blue, the margins will be blue. Inside the margins, between the borders and the element's content, is the padding. The difference between padding and margins (besides where they sit in relation to the borders) is while margins do not take on the element's background, the padding does. Thus, if you have a paragraph with a white background, and your page's background is blue, the padding will be white. The difference can be seen in Figure 2.
<P style="margin-left: 20px;">
The padding and margins can be defined using the properties
<P style="padding: 10px;"> Here's ten pixels of padding.</P>
The easiest case is assuming that you want the same value all the way
around. For example, let's say you want a paragraph with a white
background and 10 pixels of padding on all sides. This would require the
declarations
However, suppose we want different amounts of padding on each side: 10
pixels on top, but only 3 on the bottom, 20 on the left, and none on the
right. In that case, you can string the values together, but you must do
so in a specific "clockwise" order: top, right, bottom, left. Thus, the
above values would be declared as
There are a few shortcuts in this approach, thankfully enough. Let's say
we want 10 pixels of padding on the sides, but 30 pixels of padding
on the top and bottom. In that case, you might think you'd have to write
<P style="padding: 30px 10px;"> Here's some uneven but symmetrical padding. </P>
How does this work? The declaration
The same rules apply when using the
Floating AlongFloating is pretty simple to grasp, assuming you're familiar with HTML v3.2 or later. As you're no doubt aware, it's possible to make images stick to either the left or right side of a page, with text (and other elements) flowing past it. This is how you would code the HTML to float an image to the left:
When you do this to an image, you're floating it, and adding padding
around the image using the
Well, thanks to CSS1, you can float anything using, of course, the
Or simply declare at the top of the document that all images (perhaps those of a given class) are floated. As I said, however, we can (in theory) float anything: headings, tables, even whole paragraphs. Think of it -- actual sidebars which aren't dependent on tables, but are instead paragraphs which have been floated to the side and given a different color! It would be a wonderful thing.
And again, I say "would be" because browsers aren't very good about
handling floated text just yet. Navigator does the best job, though it
is a tad buggy, and Explorer can be coaxed into floating some text (see
the sidebar Making
This is the code used to get the above effect: <STYLE type="text/css">
.fig5 {color: black;
background: olive;
float: right;
width: 8em;
padding: 5px;}
</STYLE>
<P class="fig5">
This is a sidebar past which...</P>
<P>This is the main text of the page...</P>
Another way to use <STYLE type="text/css">
.dropcap {float: left;
font-size: 24pt;
background: black;
color: silver;
padding: 5px 1px;}
</STYLE>
<P>
<SPAN class="dropcap">T</SPAN>his paragraph
starts ...</P>
Assuming that the page's background is also silver, then the drop-cap should look like a black box with the shape of an uppercase "T" punched out of it. Closing the BoxI realize there is a lot more to be covered, but I'm running out of space, and besides, properly explaining box properties could be one or two whole chapters of a book. Hopefully, this month's installment has piqued your interest in box properties enough to want to experiment with them a bit. Next month, be here for a special issue on CSS2, including some in-depth examinations of what's new and what's changed compared to CSS1, a how-to on CSS positioning and how it may someday make your life a lot easier, and a look at the efforts of some experts to make authoring style sheets a heck of a lot easier for us. |
||||||||
|
Go Back |