Week 6 Lecture 2: Tables
Week 6 Lecture 2: Tables
Some content is best displayed as a table. Tables consist of columns and rows. Where each
column and row intersect, you have a cell. The minimum required for a table are table, table
row, and table data cells:
<table>
<tr>
<td></td>
</tr>
</table>
To add more columns across, just add more <td> tags on each row. Then add more rows as
needed after the first.
You can also add <thead>, <tbody>, and <tfoot> tags to group the cells. This makes it easier to
style certain portions of your table. <th> tags give your table a header. These cells are
automatically bold.
Note: Your book uses <tfooter> instead of <tfoot>. <tfoot> is the official tag per the W3C.
Be sure to use <tfoot> to ensure your pages validate correctly.
Tables can be styled like most other elements. There is a good list of properties on page 390.
Most of them are self-explanatory, such as height, width, and text-align. Border-collapse is a
little different. The default setting for borders has every cell with its own border, giving it a
double-line border, like this:
Creating a table is not difficult, but requires a lot of tags, which can make it tedious. One thing
that can make it easier is to create a row with all of its tags, but no data yet. Then copy and paste
that row over and over as many times as needed. Then go back and add data to the cells.
Creating the entire empty table first, can make it easier in the long run.
Tables are also sometimes used for layouts. It used to be the best way to get sections of your
page in exactly the spot you wanted them, so you may see it in older pages (or those done by
older developers ). Now we have more options in HTML5, so you see this used less. Tables
are also more difficult to work with when designing for smaller screens. But tables still have lots
of great uses, so they are well worth learning.