Part II: Layout Topics
|
||||||||||||||||||||||||||||||||||||||||||
Chapter 14. Creating Fixed-Width Layouts with Tables
F
ixed-width
layouts use absolute pixel measurements to determine the width of the page. The
Fixed-width layouts are the only choice for pages that require precise control over graphic design. You can't expect to place visual elements with any accuracy when a liquid layout keeps changing the width of the page to match the width of the browser window. At the same time, fixed width means fixed width, whether the
Figure 14.1. A fixed-width page can feel cramped when you look at it in a much wider browser window.
Figure 14.2. Centering the fixed-width layout
|
|
All good Web layouts start with a sketch like the one in Figure 14.3, where you mark out the main areas of your page. Don't get too clever with the layout here. HTML's colspan and rowspan attributes let you build incredibly complex tables, but do everything in your power to avoid them. Colspans and rowspans are fine for
data tables
, or tables that present rows and
GEEKSPEAK
A
data table
is a table that
|
If you want a more adventurous design, try
nested tables
instead. A nested table is a table that appears inside the
GEEKSPEAKA nested table is a table that appears inside the cell of another table. |
Once you sketch your design, you should start to think about pixel measurements. The pixel is the basic unit of measurement in HTML tables. Conveniently enough, computer operating systems measure screen widths in pixels, too, which makes sizing your layout for a particular screen setting a painless operation. You simply pick a screen width that makes sense for your
TIP
When you build your design, set the valign attribute of the cells to "top." This instructs the browser to align the content vertically along the top of the
|
Table 14.1 gives you the
|
S CREEN W IDTH |
T ABLE W IDTH |
C OMMENTS |
|---|---|---|
|
544 pixels |
544 pixels |
WebTV/MSNTV |
|
640 pixels |
600 pixels |
Absolute minimum for most applications |
|
800 pixels |
760 pixels |
The safe default size |
|
1024 pixels |
955 pixels |
The emerging default size |
|
1280 pixels |
1210 pixels |
Not recommended for general-purpose sites |
|
1600 pixels |
1530 pixels |
Not recommended for general-purpose sites |
TIP
While it's true that many people are surfing the Web with desktop and laptop
|
For the purposes of this example, play it safe and go with an 800-width screen, which calls for a table width of 760 pixels. Now that you know this, figuring out the widths of the areas inside the table isn't hard. Eyeball it. Make your best guess. You can adjust the values later if need be. Just make sure the individual widths don't add up to more (or less) than the total width of the table.
Record the numbers on your sketch as in Figure 14.4, and the table code practically
TIP
Be sure to set the layout table's border,
|
[View full width]
<div align="center"> <!-- If you don't want to center the layout table, delete the div tag here and at the endof the listing. --> <table width="760" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="760" valign="top">Logo</td> </tr> <tr> <td width="760" valign="top"> <!-- The nested table begins here. --> <table width="760" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="200" valign="top">Nav</td> <td width="560" valign="top">Content</td> </tr> </table> <!-- The nested table ends here. --> </td> </tr> </table> </div>
Notice in this example that you don't worry about the heights of the table cells. This is actually one of the advantages of building layouts with tables. The browser automatically
TIPIf a cell in your table is the same width as the table itself, you can omit the table cell's width attribute. This way, if you change your mind about the width of the table, you only have one width attribute to update. |
Here's what the listing might look like with some height values thrown in:
<div align="center">
<table width="760" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="760" height="100" valign="top">Logo</td>
</tr>
<tr>
<td width="760" height="400" valign="top">
<!-- The nested table begins here. -->
<table width="760" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="200" height="400" valign="top">Nav</td>
<td width="560" height="400" valign="top">Content</td>
</tr>
</table>
<!-- The nested table ends here. -->
</td>
</tr>
</table>
</div>
The downside to omitting the heights is that your table looks completely