HTML Tables are used to represent data in a tabular format. Simple tables are created using HTML tags <table>
, <tr>
(table row), and <td>
(table data). Complete HTML tables may also include <th>
tags for header cells and <caption>
tag for table caption. Nested tables are tables within tables. Complex tables have multiple levels of row and column spans, and may include styling using CSS. The width of cells can be specified using the width
attribute, while cell spacing and padding can be adjusted using the cellspacing
and cellpadding
attributes respectively. Column and row span allows a cell to span multiple columns or rows, effectively merging cells together.
<table></table>
: begin/end table definition<tr></tr>
: create a table row<td></td>
: create tabular data (cell)<table cellspacing="0" cellpadding="5">
<tr>
<td><img src="ppt.gif"></td>
<td><a href="lecture1.ppt">Lecture 1</a></td>
</tr>
<tr>
<td><img src="ppt.gif"></td>
<td><a href="lecture2.ppt">Lecture 2</a></td>
</tr>
<tr>
<td><img src="zip.gif"></td>
<td><a href="lecture2-demos.zip">
Lecture 2 - Demos</a></td>
</tr>
</table>
<tr>
<th>Full Name</th> <th>Mark</th>
</tr>
<tr>
<td>Nikolay Kostov</td> <td>Very good (5)</td>
</tr>
<tr>
<td>Ivan Ivanov</td> <td>Exellent (6)</td>
</tr>
<thead>
denotes table header and contains <th> elements, instead of <td> elements<tbody>
denotes collection of table rows that contain the very data<tfoot>
denotes table footer but comes BEFORE the <tbody> tag<colgroup>
and <col>
define columns (used to set column widths)<table>
<colgroup>
<col style="width:100px" /><col />
</colgroup>
<thead>
<tr><th>Column 1</th><th>Column 2</th></tr>
</thead>
<tfoot>
<tr><td>Footer 1</td><td>Footer 2</td></tr>
</tfoot>
<tbody>
<tr><td>Cell 1.1</td><td>Cell 1.2</td></tr>
<tr><td>Cell 2.1</td><td>Cell 2.2</td></tr>
</tbody>
</table>
<table>
<tr>
<td>Contact:</td>
<td>
<table>
<tr>
<td>First Name</td>
<td>Last Name</td>
</tr>
</table>
</td>
</tr>
</table>
<html>
<head><title>Table Cells</title></head>
<body>
<table cellspacing="15" cellpadding="0">
<tr><td>First</td>
<td>Second</td></tr>
</table>
<br/>
<table cellspacing="0" cellpadding="10">
<tr><td>First</td><td>Second</td></tr>
</table>
</body>
</html>
<table cellspacing="0">
<tr class="1">
<td>Cell[1,1]</td>
<td colspan="2">Cell[2,1]</td>
</tr>
<tr class="2">
<td>Cell[1,2]</td>
<td rowspan="2">Cell[2,2]</td>
<td>Cell[3,2]</td>
</tr>
<tr class="3">
<td>Cell[1,3]</td>
<td>Cell[2,3]</td>
</tr>
</table>