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 Figure 24.1 looks familiar, that's because you've seen something like it before, 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 style="position: absolute; width: 100%; height: 100px; top: 0px; left: 0px;"> Logo </div> <div style="position: absolute; width: 200px; top: 100px; left: 0px;"> Nav </div> <div style="position: absolute; width: 175px; top: 100px; right: 0px;"> Links </div> <div 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.Figure 24.2. This CSS layout creates a more graphical effect by specifying background colors in the Logo, Nav, and Links divs.Listing 24.2. View Source for Figure 24.2.[View full width] <div style="position: absolute; width: 100%; height: 100px; top: 0px; left: 0px; background-color: #99CCFF;"> Logo </div> <div style="position: absolute; width: 200px; top: 100px; left: 0px; background-color: #99CCFF;"> Nav </div> <div style="position: absolute; width: 175px; top: 100px; right: 0px; background-color: #99FF99;"> Links </div> <div style="position: absolute; top: 100px; left: 200px; right: 175px;"> Content </div> 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. 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.To create the border, you add three new attributes to the div's style definition:
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.
Maybe you don't like the way the text fits snugly against the corner of the div. Just give the div some interior whitespace with the padding attribute, as in Figure 24.5. Listing 24.3. View Source for Figure 24.4.[View full width] <div 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.
Listing 24.4. View Source for Figure 24.5.[View full width] <div 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> 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. Listing 24.5. View Source for Figure 24.6.[View full width] <div 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.
Table 24.1 lists common CSS attributes for formatting the divs of your layout. Remember, each div can have its own custom style, so CSS provides a much larger range of options than HTML tables ever could. Then again, HTML tables are more stable, so you sacrifice broad browser compatibility when you start tinkering with CSS. |