3.3.2 Relative positions

Listing 3 specifies some of the styles to use relative positioning. Relative positioning is useful because it makes the document less tied to the actual screen size.


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

The rendering of this document is the same as that of 2. This is because the default value of the position property is relative! Note that slotB and slotC do not have this property.