Book HomeCascading Style Sheets: The Definitive GuideSearch this book Sunday 25th of February 2018 10:19:35 PM

7.5. Padding

Between the borders and the content area, we find the padding of the element box. It will come as no surprise that the simplest property used to affect this area is called padding.

padding

Values

[ <length> | <percentage> ]{1,4}

Initial value

0

Inherited

no

Applies to

all elements

WARNING

Percentage values refer to the width of the parent element.

As you can see, this property accepts any length value or a percentage. That's all. So if you want all H1 elements to have 10 pixels of padding on all sides, it's this easy, as the result shown in Figure 7-56 makes clear:

H1 {padding: 10px; background-color: silver;}
Figure 7-56

Figure 7-56. Padding applied to an H1 element

On the other hand, we might want H1 elements to have uneven padding and H2 elements to have regular padding, as shown in Figure 7-57:

H1 {padding: 10px 0.25em 3ex 3cm;} /* uneven padding */
H2 {padding: 0.5em 2em;} /* values replicate to the bottom and left sides */
Figure 7-57

Figure 7-57. Uneven padding

It's a little tough to see the padding, though, so let's add a background color, as shown in Figure 7-58:

H1 {padding: 10px 0.25em 3ex 3cm; background: silver;}
H2 {padding: 0.5em 2em; background: silver;}
Figure 7-58

Figure 7-58. Uneven padding with background colors

As Figure 7-58 demonstrates, the background of an element extends into the padding. As we discussed before, it also extends to the outer edge of the border, but the background has to go through the padding before it even gets to the border.

The default value of padding is 0 (zero), and padding values cannot be negative.

WARNING

Opera 3.5 allows negative padding values, but this was fixed in Opera 3.6. The other browsers don't allow negative padding lengths.

7.5.1. Percentage Values and Padding

As stated earlier, it's possible to set percentage values for the padding of an element. Percentages are computed in relation to the width of the parent element, so they can change if the parent element's width changes in some way. For example, assume the following, which is illustrated in Figure 7-59:

P {padding: 10%; background-color: silver;}
<DIV STYLE="width: 200px;">
<P>This paragraph is contained within a DIV which has a width of 200 pixels,
so its padding will be 10% of the width of the paragraph's parent element.
Given the declared width of 200 pixels, the padding will be 20 pixels on
all sides.</P>
</DIV>
<DIV STYLE="width: 100px;">
<P>This paragraph is contained within a DIV with a width of 100 pixels,
so its padding will still be 10% of the width of the paragraph's parent.
There will, therefore, be half as much padding on this paragraph as that
on the first paragraph.</P>
Figure 7-59

Figure 7-59. Padding, percentages, and the widths of parent

We've seen this before, of course -- in Section 7.3, "Margins", in case you don't remember -- but it's worth reviewing again, just to see how it operates.

7.5.2. Single-Side Padding

You guessed it: there are properties that let you set the padding on a single side of the box, without affecting the other sides.

padding-top, padding-right, padding-bottom, padding-left

Values

<length> | <percentage>

Initial value

0

<LINK REL="stylesheet" TYPE="text/css" HREF="link-styles.css" TITLE="Linked"> <STYLE TYPE="text/css"> @import url(import-styles.css); </STYLE>

Because Explorer will read in both style sheets, it will use the cascade to determine which rules should actually be applied. If you've ordered things correctly, and the imported style sheet comes after the linked style sheet, its rules will win out over the rules in the linked style sheet.

Inherited

no

Applies to

all elements

WARNING

Percentage values refer to the width of the parent element.

These properties operate as you'd expect by now. For example, the following two rules will give the same amount of padding:

H1 {padding: 0 0 0 0.25in;}
H2 {padding-left: 0.25in;}

7.5.3. Padding and Inline Elements

There is one major difference between margins and padding when it comes to inline elements. Let's turn things around and talk about left and right padding first off. Here, if we set values for the left or right padding, they will be visible, as Figure 7-60 makes apparent.

B {padding-left: 10px; padding-right: 10px; background: silver;}
Figure 7-60

Figure 7-60. Padding on an inline element

Note the extra background space that appears on either end of the boldfaced text. There's your padding.

This all seems familiar enough, even when the boldfaced text stretches across multiple lines. Turn to Figure 7-61 to see what happens with padding set on an inline element displayed across multiple lines:

B {padding: 10px; background: silver;}
Figure 7-61

Figure 7-61. Padding on an inline element that spans multiple lines

As with margins, the left padding is applied to the beginning of the element, and the right padding to the end of it; padding is not applied to the right and left side of each line.

Now let's talk about top and bottom padding. In theory, an inline element with a background color and padding could have the background extend above and below the element. Figure 7-61 gives us some idea of what that might look like. The line height isn't changed, of course, but since padding does extend the background, it should be visible, right?

Here's where the famous phrase returns: "there may be implementation-specific limits." User agents aren't required to support this type of effect.

7.5.4. Padding: Known Issues

In the first place, padding and Navigator 4.x just plain don't get along. The main problem is that you can set padding on an element with a background color, but the background won't extend into the padding unless you get very sneaky. You need to add a border, as was discussed earlier in "Margins: Known Issues." Therefore, if you have a background color, some padding, and a border set for an element, you'll see the background fill the content area and the padding as requested, but a transparent space will incorrectly appear between the two, as shown in Figure 7-62.

Figure 7-62

Figure 7-62. Padding problems in Navigator 4

This may be an interesting effect, but it isn't permissible under the CSS specification, and no other browser will do the same thing, so it's best to avoid this altogether.

Even worse, if you try applying padding to inline elements in Navigator 4.x, you get a huge mess. The same sorts of things that happen when you apply margins to inline elements will happen if you apply padding, so it is wise to avoid setting margins, borders, or padding on inline elements.

Opera 3.5 incorrectly permits negative values for padding, but version 3.6 does not suffer from this problem. Internet Explorer 4.x will not apply padding to inline elements at all -- which is probably just as well.



Library Navigation Links

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

borders. Thanks to the grayscale nature of this book, I've beensticking mostly to shades of gray, but any color could be used. Ifyou wanted an H1 with a red, green, blue, andyellow border, it's this easy:

H1 {border-style: solid; border-color: red green blue yellow;}
Figure 7-43

Figure 7-43. One border, many colors

As previously discussed, if no colors are defined, then the defaultcolor is the foreground color of the element. Thus, the followingdeclaration will be displayed as shown in Figure 7-44:faces with labels like Italic, Cursive, and Kursiv are usually mapped to the italic keyword, while oblique can be assigned faces with labels such as Oblique, Slanted, and Incline.zero values to its own default margins.

Figure 7-26

Figure 7-26. Navigator 4.x and margins

If you want to overcome this space, you can always use negativemargins. Here's one possible declaration:

H1 {margin-bottom: 0;}P {margin-top: -1em;}

The problem with this solution arises when the document is viewed inInternet Explorer, which will display both the top and bottom of the content-height to arrive at an inlinebox which is, in this case, 12px high. This12-pixel inline box is centered vertically within the content-heightof the element.

So far it sounds like we've done the same thing to each bit oftext. This is not quite the case. The inline boxes don'tactually line up, as we can see in Figure 8-50,because the text is all baseline-aligned.

In your application layer, you can create many interesting Java applications. The apps can run on the server side or client side or both. They may have graphical user interfaces or they may be web based. When I use the word application or app in this chapter, I don't exclude Java applets; I mean application (or app) in the broad sense of the word, i.e., I mean it to describe a software system written in Java that solves a real-world problem.

3 Main categories

There are many different types of software that you can write in Java to make use of XML. I have created 3 major categories to describe certain types of apps (that are currently popular) that are really well suited to the use of XML. This is by no means a comprehensive set of categories; you can create your own, and many more major categories will emerge as XML becomes more popular.

Client side - Graphical Java Applications