3.3.4 Height for a block

The problem with listing 4 is that the height of slot A1 cannot be computed automatically. The use of absolution-position (for slots B and C) means that these corresponding blocks are out of the normal flow of blocks. This, in return, means that the browser cannot compute the actual required height of slot A1.

The solution is to force the height of slot A1. Note that forcing the heights of slots B and C will not help because they are absolute-position blocks in a relative-position block.

Let us force the height of slot A1 to be 200 pixels. This results in listing 5.


Listing 5:height
 
1<!DOCTYPE HTML PUBLIC ”//W3C/DTD_HTML_4.01//EN” 
 
2
  ”http://www.w3.org/TR/html4/strict.dtd”> 
3<html lang=”en”> 
4  <head> 
5    <title>An illustration of CSSbased Layout</title> 
6    <style type=”text/css”> 
7      div.slotA { 
8        position: relative; 
9        background: red; 
10      } 
11      div.slotA1 { 
12        position: relative; 
13        height: 200px;  
14        background: green; 
15      } 
16      div.slotA1 > div.slotB { 
17        position: absolute; 
18        width: 20%; 
19        background: yellow; 
20      } 
21      div.slotA1 > div.slotC { 
22        position: absolute; 
23        left: 20%; 
24        width: 80%; 
25        background: gray; 
26      } 
27      div.slotD { 
28        position: relative; 
29        background: purple; 
30      } 
31    </style> 
32  </head> 
33  <body> 
34    <div class=slotA> 
35      Red stuff represents slot A. Slot A is the title portion of a document. 
36      It spans 100% of the width, and is as high as it needs to be. 
37    </div> 
38    <div class=slotA1> 
39      <p>What is this green stuff? This is slot A1. Slot A1 includes 
40      slot B and slot C. 
41      However, this portion does not belong to either one. Slot A1 includes 
42      both the main content and a left bar for navigation links.</p> 
43 
44      <p>Note that we have to set a height for this slot because it contains 
45      slot B and slot C, which are block that have absolute positions.</p> 
46 
47      <p>The entire slot A1 (including slot B and slot C) share the height 
48      of the slot A1. If the content is taller, then a scroll bar is usually 
49      rendered to scroll through this block.</p> 
50      <div class=slotB> 
51        <p>˜Slot B is colored yellow. Its width is 20% of the containing 
52        block ”SlotA1”. 
53        This portion is normally used to include navigation links.</p> 
54      </div> 
55      <div class=slotC> 
56        <p>Slot C is colored gray. This is usually where the main content goes. 
57        Its 
58        position uses slot A1 as the frame of reference. We need to specify 
59        the left to be 20% because that part is used by the slot B. 
60        The width of the slot is 80% because 20% of the full width of 
61        slot A1 is already used for slot B.</p> 
62      </div> 
63    </div> 
64    <div class=slotD> 
65      This is slot D (purple). This slot is usually reserved for copyright 
66      notice and a email link to the webmaster. 
67    </div> 
68  <body> 
69</html>

Line 13 defines the height of slot A1 to be 200 pixels. The rendering of this code is shown in figure 3


PIC

Figure 3: The rendering of the code in listing 5.


Well, it is a little better, but the footer is still overlapping with the main blocks.