2 HTML Table
2 HTML Table
table
header cell header cell header cell
row Menu item Calories Fat (g)
Figure 8-2. Tables are made up of rows that contain cells. Cells are the containers for
content.
Simple enough, right? Now let’s look at how those parts translate into ele-
ments (Figure 8-3).
<table>
<td>Chicken Noodle
<tr> Soup</td> <td>120</td> <td>2</td> </tr>
</table>
Figure 8-3. The elements that make up the basic structure of a table.
Figure 8-3 shows the elements that identify the table (table), rows (tr, for
“table row”), and cells (th, for “table headers,” and td, for “table data”).
Cells are the heart of the table, because that’s where the actual content goes.
The other elements just hold things together.
09 285886-ch07.qxp 5/19/08 3:39 PM Page 110
Assign a
Table Border
110
09 285886-ch07.qxp 5/19/08 3:39 PM Page 111
111
09 285886-ch07.qxp 5/19/08 3:39 PM Page 114
114
09 285886-ch07.qxp 5/19/08 3:39 PM Page 115
If I want to precisely control the dimensions in Can I set the width for a single cell and
my table, do I need to set a width and height not have it affect the other cells?
for all the cells? When you change the width of a cell, all the
If you have a table with more than one cell, no. When other cells in that column adjust to the same
you set a width for one cell, this also sets width. If you want one cell to span
the width for the cells above or below. one or more columns, you can
When you set a height for a cell, this use another set of codes to
also sets the height for all the cells to CELL control the individual cell
the left or right of it. Because of this, width. See the section “Extend
you need to set the width or height Cells across Columns and
only once for each row or column. Rows” to learn more.
115
09 285886-ch07.qxp 5/19/08 3:39 PM Page 117
Create Newspaper-Style
Columns Working with Tables chapter 7
ws
You can use the table format to present columns of
aily Ne
text on your Web page, much like a newspaper. For The D
example, you may want to organize your text into
two or three columns. Paragraphs of text are
contained within each column.
You can use the vertical alignment
attribute to make each column
align at the top of the table.
117
09 285886-ch07.qxp 5/19/08 3:39 PM Page 118
Create Side
Navigation
118
09 285886-ch07.qxp 5/19/08 3:39 PM Page 119
Add a Table
Caption Working with Tables chapter 7
You can add a caption to your table to help users identify s
the information contained within the table. Table captions Sport
Daily
can appear at the top or bottom of the table. By default,
captions appear above the table unless you specify another
alignment attribute. Captions always appear on a separate
line of text from the table.
r
afte
You can format your caption text using the HTML o ry
Lane win
Vict 500
formatting tags. See Chapter 4 to learn more. t Zig
in
aitis tive HT M L
M at c o n s e c u
3rd
119
09 285886-ch07.qxp 5/19/08 3:39 PM Page 120
Control which
Borders to Display
120
09 285886-ch07.qxp 5/19/08 3:39 PM Page 121
HSIDES Borders on the top and bottom of the table ALL Borders throughout the table cells
(default)
VSIDES Borders on the left and right sides of the
table
BORDER Borders on every side of the table (default)
121
09 285886-ch07.qxp 5/19/08 3:39 PM Page 122
Adjust the
Table Size
122
09 285886-ch07.qxp 5/19/08 3:39 PM Page 123
Is it possible to set a table too small for its To what size does a browser set my table if I
contents? do not specify an exact width?
No. If you accidentally make a table too small for If you do not set a width using the WIDTH attribute,
the contents, the browser ignores the the browser sizes the table based on the
measurements and tries to make cell contents. When text is in the
the table fit as best it can. On table, the browser expands the
the other hand, if you set a table enough to fit its largest
table too wide, users are forced contents but not past the right
to scroll to see parts of the table. edge of the browser window. If a
For best results, do not make your table contains large images, it may
table wider than 750 pixels. extend beyond the browser window.
123
09 285886-ch07.qxp 5/19/08 3:39 PM Page 124
Change Cell
Alignment
124
09 285886-ch07.qxp 5/19/08 3:39 PM Page 125
Can I override the alignment of a column or How do I justify data in a table cell?
row group for a single cell? Justification sets both left and
Yes. You can set the alignment right alignment and stretches
for a column or row and OVERRIDE the text to span the area
then override the alignment between the cell borders.
for an individual cell within
the group. Simply add the
Although there is an HTML
attribute for justification, J-U-S-T-I-F-Y
alignment attribute to the justify, not all Web
cell. See the section “Create browsers currently support
Column and Row Groups” to the setting.
learn more.
125
Spanning Cells
In this way, they are a key tool for making table content accessible. Don’t try
to fake headers by formatting a row of td elements differently than the rest of
the table. Conversely, don’t avoid using th elements because of their default
rendering (bold and centered). Mark up the headers semantically and change
the presentation later with a style rule.
That covers the basics. Before we get fancier, try your hand at Exercise 8-1.
Spanning Cells
One fundamental feature of table structure is cell spanning, which is the
stretching of a cell to cover several rows or columns. Spanning cells allows
you to create complex table structures, but it has the side effect of making
the markup a little more difficult to keep track of. You make a header or data
cell span by adding the colspan or rowspan attributes, as we’ll discuss next.
Column spans
Column spans, created with the colspan attribute in the td or th element,
stretch a cell to the right to span over the subsequent columns (Figure 8-6).
Here a column span is used to make a header apply to two columns. (I’ve
WA R NIN G added a border around cells to reveal the table structure in the screenshot.)
Be careful with colspan values. If you <table>
specify a number that exceeds the num- <tr>
ber of columns in the table, most brows- <th colspan="2">Fat</th>
</tr>
ers will add columns to the existing
<tr>
table, which typically screws things up. <td>Saturated Fat (g)</td>
<td>Unsaturated Fat (g)</td>
</tr>
</table>
Figure 8-6. The colspan attribute stretches a cell to the right to span the specified
number of columns.
Notice in the first row (tr) that there is only one th element, while the sec-
ond row has two td elements. The th for the column that was spanned over
is no longer in the source; the cell with the colspan stands in for it. Every
row should have the same number of cells or equivalent colspan values. For
example, there are two td elements and the colspan value is 2, so the implied
number of columns in each row is equal.
Figure 8-7. Practice column spans by writing the mvwarkup for this table.
Row spans
Row spans, created with the rowspan attribute, work just like column spans,
but they cause the cell to span downward over several rows. In this example,
the first cell in the table spans down three rows (Figure 8-8).
<table>
<tr>
<th rowspan="3">Serving Size</th>
<td>Small (8oz.)</td>
</tr>
<tr>
<td>Medium (16oz.)</td>
</tr>
<tr>
<td>Large (24oz.)</td>
</tr>
</table>
Again, notice that the td elements for the cells that were spanned over (the
first cells in the remaining rows) do not appear in the source. The rowspan="3"
implies cells for the subsequent two rows, so no td elements are needed.
Figure 8-8. The rowspan attribute stretches a cell downward to span the specified number
of rows.
Figure 8-9. Practice row spans by writing the markup for this table.
Table Accessibility
As a web designer, it is important that you always keep in mind how your
site’s content is going to be used by non-sighted visitors. It is especially chal-
lenging to make sense of tabular material using a screen reader, but there are
measures you can take to improve the experience and make your content
more understandable.
128
09 285886-ch07.qxp 5/19/08 3:39 PM Page 129
group of columns set read the <TH> tag and automatically set
aside for certain data, center alignment and bold text. You can, Title Cell
12g 16g
while the remaining cells however, align the cell separately using the
40g
13g 10g
30g 23g
129
09 285886-ch07.qxp 5/19/08 3:39 PM Page 130
HEAD
You can use row groups to divide a table HEAD
BODY
into horizontal sections. You create row BODY
HEAD
groups using the <THEAD> and <TBODY> BODY
BODY T
tags. The <THEAD> tag creates a header for FOO
130
09 285886-ch07.qxp 5/19/08 3:39 PM Page 131
5
6
After I assign a column or row group, how How do I add borders between my groups?
do I align every cell in the group? You can specify exactly which cell and table borders
You can add the ALIGN attribute to appear in your table using the FRAME
the <THEAD>, <TFOOT>, or attribute. For example, you might
<TBODY> tag to assign set a thick, colored border at
alignment to the entire group. the top and bottom of a row
For example, if you type group to make the group stand
<TBODY ALIGN=”right”>, out from the rest of the table.
all the cell content in the See the section “Control Which
group aligns to the right of Borders to Display” earlier in this
the cells. chapter to learn more.
131
09 285886-ch07.qxp 5/19/08 3:39 PM Page 139
Nest a Table
within a Table Working with Tables 7
chapter
139