Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
<table>
,
<tr>
,
<td>
A 2D table of rows and columns of data (block element)
<table> <tr><td>1,1</td><td>1,2 okay</td></tr> <tr><td>2,1 real wide</td><td>2,2</td></tr> </table>
1,1 | 1,2 okay |
2,1 real wide | 2,2 |
table
defines the overall table, tr
each row, and td
each cell's data<th>
,
<caption>
<table> <caption>My important data</caption> <tr><th>Column 1</th><th>Column 2</th></tr> <tr><td>1,1</td><td>1,2 okay</td></tr> <tr><td>2,1 real wide</td><td>2,2</td></tr> </table>
Column 1 | Column 2 |
---|---|
1,1 | 1,2 okay |
2,1 real wide | 2,2 |
th
cells in a row are considered headers; by default, they appear boldcaption
at the start of the table labels its meaningtable { border: 2px solid black; caption-side: bottom; } tr { font-style: italic; } td { background-color: yellow; text-align: center; width: 30%; }
Column 1 | Column 2 |
---|---|
1,1 | 1,2 okay |
2,1 real wide | 2,2 |
border-collapse
propertytable, td, th { border: 2px solid black; } table { border-collapse: collapse; }
Column 1 | Column 2 |
---|---|
1,1 | 1,2 |
2,1 | 2,2 |
Column 1 | Column 2 |
---|---|
1,1 | 1,2 |
2,1 | 2,2 |
border-collapse
property merges these borders into onerowspan
and colspan
attributes<table> <tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr> <tr><td colspan="2">1,1-1,2</td> <td rowspan="3">1,3-3,3</td></tr> <tr><td>2,1</td><td>2,2</td></tr> <tr><td>3,1</td><td>3,2</td></tr> </table>
colspan
makes a cell occupy multiple columns; rowspan
multiple rowstext-align
and vertical-align
control where the text appears within a cell<col>
,
<colgroup>
<table> <col class="urgent" /> <colgroup class="highlight" span="2"></colgroup> <tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr> <tr><td>1,1</td><td>1,2</td><td>1,3</td></tr> <tr><td>2,1</td><td>2,2</td><td>2,3</td></tr> </table>
.urgent { background-color: pink; } .highlight { background-color: yellow; }
col
tag can be used to define styles that apply to an entire column (self-closing)colgroup
tag applies a style to a group of columns (NOT self-closing)table
has semantics; it should be used only to represent an actual table of datadiv
s, widths/margins, floats, etc. to perform layout