3.3.3 Absolute positioning

Let’s have a little more fun now. Let us now specify slotB and slotC to use absolute positioning. Note that slotB and slotC are both children of slotA1. As such, their absolute positioning use slotA1 as the frame of reference.

Listing 4 shows the HTML code.


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

The rendering is shown in figure 2.


PIC

Figure 2: The (ugly) rendering of the code in listing 4.


Note that the slot D (purple) block is rendered on top of slot B and slot C! This is because all the relative blocks share the same vertical position “counter”. As a result, slot D simply follows the position of the last relative block, which is the portion of slot A1 that is not in slot B or slot C.

However, even with this flaw, there are still lessons to be learned.