Chapter 24. Coloring and Formatting CSS Divs
Topic 23 showed you how to color the cells of a table. You can specify background colors for divs, too, along with a couple of other appearance attributes that HTML tables just don't support. Once again, as with all things CSS, make sure you test your design in a number of browsers before you upload to the Web. Who knows how your target browsers will garble your perfectly legitimate CSS markup this time.If the layout in Topic 18, to be exact. No background colors appear in the code, as the View Source bears out. As you can see in Figure 24.2, adding the background-color attribute to the style definition creates a more graphical effect.
Listing 24.1. View Source for Figure 24.1.
<div id="logo" style="position: absolute; width: 100%; height: 100px; top: 0px; left: 0px;">
Logo
</div>
<div id="nav" style="position: absolute; width: 200px; top: 100px; left: 0px;">
Nav
</div>
<div id="links" style="position: absolute; width: 175px; top: 100px; right: 0px;">
Links
</div>
<div id="content" style="position: absolute; top: 100px; left: 200px; right: 175px;">
Content
</div>
Figure 24.1. This CSS layout doesn't specify background colors for any of the divs.
[View full size image]

Figure 24.2. This CSS layout creates a more graphical effect by specifying background colors in the Logo, Nav, and Links divs.
[View full size image]

Listing 24.2. View Source for Figure 24.2.
[View full width]
As with table cells, the background color is a hexadecimal color code. See Topic 23 for a list of common colors and their hexadecimal values.Since you're working with CSS, you don't have to stop at coloring the background. For instance, you could add a solid black border around the Links div, as in Figure 24.3.
<div id="logo" style="position: absolute; width: 100%; height: 100px; top: 0px; left: 0px;background-color: #99CCFF;">
Logo
</div>
<div id="nav" style="position: absolute; width: 200px; top: 100px; left: 0px;background-color: #99CCFF;">
Nav
</div>
<div id="links" style="position: absolute; width: 175px; top: 100px; right: 0px;background-color: #99FF99;">
Links
</div>
<div id="content" style="position: absolute; top: 100px; left: 200px; right: 175px;">
Content
</div>
Figure 24.3. Nothing but your own design sensibilities and the CSS capabilities of your target browser prevent you from adding a border around the Links div.
[View full width]<div id="links" style="position: absolute; width: 175px; top: 100px; right: 0px;
Links
</div>
Maybe you want a coupon-style dashed line instead of a solid line, and maybe you want a blue border instead of a black one, and maybe you want a lighter border weight, as in Figure 24.4.
Figure 24.4. Choose a different border style, and the border changes.
[View full size image]

TIPYou can define different appearances for the top, left, right, and bottom segments of the border. See Table 24.1 at the end of this topic for details.
|
Listing 24.3. View Source for Figure 24.4.
[View full width]
<div id="links" style="position: absolute; width: 175px; top: 100px; right: 0px;background-color: #99FF99; border-width: 2px; border-style: dashed; border-color: #0000FF;">
Links
</div>
Figure 24.5. Introduce extra whitespace with the padding attribute.
[View full size image]

TIPAs with border styles, you can go crazy with padding and define different amounts of it for the top, left, right, and bottom sides of the div. It's all in Table 24.1. |
Listing 24.4. View Source for Figure 24.5.
[View full width]
And, as you saw (or didn't see) in Topic 10, you can actually make div elements disappear without deleting their code, as in Figure 24.6.
<div id="links" style="position: absolute; width: 175px; top: 100px; right: 0px;background-color: #99FF99; border-width: 2px; border-style: dashed; border-color: #0000FF;
padding: 12px;">
Links
</div>
Listing 24.5. View Source for Figure 24.6.
[View full width]
<div id="links" style="position: absolute; width: 175px; top: 100px; right: 0px;background-color: #99FF99; border-width: 2px; border-style: dashed; border-color: #0000FF;
padding: 12px; visibility: hidden;">
Links
</div>
Figure 24.6. Where did the div go? It's still in the code, but its visibility attribute is set to hidden.
[View full size image]
