HTML Tables – when and how to use tables in HTML
HTML tables should only be used to display data in tabular form.
This tutorial explains how to create tables in HTML properly.
HTML tables should only be used to display data in tabular form.
This tutorial explains how to create tables in HTML properly.
New eBook - Guide to Semantic HTMLBen Hunt adds another great eBook to the collection with his "Guide to Semantic HTML". The book gives you advice and tips on how, and why to use semantic HTML.
Included is a comprehensive list of HTML tags, each with their semantically appropriate uses, along with a worked example taking you through the process of how to build a website using semantic HTML.
New eBook - Web page production with xHTML and CSS #1Experience the thought process of a professional web producer as he guides you through the web page production process, from photoshop design to working HTML template.
The book tells you how to approach web production, beginning with semantic HTML, guiding you through how to slice up a photoshop document, and finally how to use CSS for presentation.
| Column 1 | Column 2 |
|---|---|
| Example of a simple table | The table has 2 columns, each one with a column header (above). |
| The first 2 rows each have 2 cells | One cell in each column. |
| Whereas this 3rd row has a single cell spanning 2 columns. | |
HTML tables should only be used for rendering data that belongs naturally in a grid, in other words where the data describe a number of objects that have the same properties.
This data may be enterable by the user in a form, as long as each column and row contains similar content.
| Order# | Item | Number | Total price |
|---|---|---|---|
| 145 | 1700mm Corner luxury bath | 2 | £990.00 |
| 146 | Duoflow System 1000 | 1 | £1,999.00 |
| 147 | Ideal Standard Space Offset Corner Shower Bath | 2 | £2,200.00 |
| 151 | Wilux Bebop | 1 | £849.95 |
For several years, web designers used tables as the only way to structure web pages, but CSS now makes their use redundant.
It is true that there remain a few things that are easier to do using tables.
The main benefit of tables is that all cells in a row, and all cells in a column, stretch together as the row or column stretches.
This effect is sometimes achievable in CSS, sometimes achievable with difficulty, and sometimes impossible.
However, don’t use tables for layout because:
e.g. It is possible to rearrange the order in which boxes are displayed on a page, even make them stack, just by changing a few CSS properties.
This is impossible with tables, which are rigid and immovable.
We’ll go through how to construct tables in HTML, including a few useful tricks.
The basic elements of tables are:
This tells us a table is starting.
In normal HTML, there are a few parameters you can use.
However, these are not permitted in xHTML.
The table head section does not contain actual data, just header cells for columns (<th> tags).
If you want column header cells, these should go inside a <thead>, not in a table row (<tr>).
The first table row in the table head section indicates a row of header cells.
These table head cells appear both in the table head section (<thead>) and in table rows (<tr>.
In the table heading section, they are column headers; in table rows, they are effectively row headers.
The content between the start and end tags will appear in the header cell.
After the <thead> has been closed, put the main body which will contain your rows of data.
You don’t have to use a <tbody> section, but you should if you use a <thead> section with column headers.
Each row in the table body has to be defined with start and end table row tags.
Start and end tags define an actual table data cell.
Here are a few useful techniques you’ll need when creating HTML tables.
You can use the properties colspan and rowspan for a <td> to make that cell span across more than one column or row.
The trick is making sure you remove the relevant cells in adjacent rows or columns, which are being merged.
Trial and error is the only way to get these right.
| colspan="2", rowspan="2" | colspan="2" | ||
| rowspan="2" | |||
You can define certain properties for whole columns at the top of your table (between the <table> and <thead> tags), using the <col /> or <colgroup /> tags.
<col /> is for setting the properties of one column at a time.
<colgroup /> is for setting properties for several columns together.
(Note that both tags are self-closing, so if you’re using xHTML, you must use the trailing forward-slash.)
Where I find these tags most useful is for setting the widths of columns (without having to set widths for particular cells).
What’s great about it is that you can use the same settings for a series of tables, and be sure that relevant columns will line up, even if the amount of content in cells varies.
(If you don’t set widths for columns, then your browser should squish the columns of a table around in order to minimise the table’s overall length.)
Table 1
| Col 1 | Col 2 | Col 3 |
|---|---|---|
| Some content | in this table, not lots, but | it varies. |
| This row has significantly more words in it | Although not every cell does, like the following cell is empty. |
Table 2
| Col 1 | Col 2 | Col 3 |
|---|---|---|
| You’ll notice | that this table | has more content in the third column than the previous one did. |
| Yet | its columns remain the same widths | as the one above. This is great when laying out series of tables containing similar data, but which you want to separate with other content like headings. |
<table style="90%;">
</tr>
</thead>
<tbody>
<tr>
</tr>
<tr>
</tr>
</tbody>
</table>
<br />
<p><strong>Table 2</strong></p>
<table style="90%;">
</thead>
<tbody>
<tr>
</tr>
<tr>
</tr>
</tbody>
</table>
A more flexible way to do the above would be to use CSS, assigning classnames to the different columns, e.g.
You could then set all the column-widths centrally in your stylesheet, and they would apply to every table.
Use column groups for setting the properties of numerous consecutive rows, using the span property, as follows.
(You can mix up single cols and colgroups.)
The example above would apply to a table with 5 rows.
Not every CSS style property you set in a column definition gets applied to every cell in the column, and other properties will easily override them.
" />
The result will vary according to your browser.
Most browsers will show the background-color.
| This text in this cell should be red and bold (not Mozilla). Its background should be pale yellow (yes) and border thick and black (not Mozilla). |
Do you love our approach to crafting simple & effective web sites that just work for people?
We'd love to hear about your web strategy.
Contact one of our team today!
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Example of a simple table</td>
<td>The table has 2 columns, each one with a column header.</td>
</tr>
<tr>
<td>The first 2 rows each have 2 cells</td>
<td>One cell in each column.</td>
</tr>
</tbody>
</table>