HTML tables concepts
There is alot you can do with tables. You can add headings & captions to tables, male cells span multiple rows & columns, align the content within cells, and more.
This tutorial focuses on:
- Empty cells
- Headings
- Captions
- Cellspacing & Cellpadding
- Spanning multiple/rows columns
- Aligning table content
Empty cells
Sometimes you may not need to place content inside of a table cell, instead you just need the cell to be there. But a table cell may not appear unless it has content inside of it.
You can include empty cells in a table by placing the character entity inside a table cell. This way a table cell technically has something inside of it (just a space character) but it's not visible and the table cell will appear empty on a webpage.
| Four cells in this table | One is empty |
| Three are filled |
Headings
You can display headings in a table using the <th> tag. A heading is an emphasized cell in a table. Such an emphasis in tables can be important depending on your situation. Use the <th> tag just like the <td> tag.
| Web development | Software development |
|---|---|
| HTML | C++ |
| PHP | Fortran |
Captions
A caption is added to a table using the <caption> tag which should be placed right below the <table> tag before the first <tr> tag.
| Web development | Software development |
|---|---|
| HTML | C++ |
| PHP | Fortran |
Cellspacing & Cellpadding
By default, the cells of a table look really stuffy and smashed together. To fix this, the cellspacing and cellpadding properties are used.
The cellspacing property takes a numeric value denoting how much space to put between cells. The cellpadding property takes a numeric value denoting how much padding is required between what is in the cells and the cell walls.
| Web development | Software development |
|---|---|
| HTML | C++ |
| PHP | Fortran |
| Web development | Software development |
|---|---|
| HTML | C++ |
| PHP | Fortran |
Spanning multiple/rows columns
Table column spanning
Why should all rows have the same number of columns in a table? There may be situations where you need a cell or cells to span more than one column. This is achieved using the colspan attribute of the <td> tag. The colspan attribute takes a numeric value indicating how many columns a cell should span.
| Client-side | HTML | CSS | Javascript |
|---|---|---|---|
| Server-side | ColdFusion | PHP | |
Table row spanning
If a cell can span more than one column, why can't it span more than one row?
You can make a cell span more than one row using the rowspan attribute of the <td> tag. The rowspan attribute takes a numeric value indicating how many rows it should span.
| Web languages | HTML | CSS |
|---|---|---|
| Javascript | PHP | |
| Software languages | C | C++ |
| FORTRAN | Visual Basic |
Aligning table content
You can align table content using the align attribute inside the <td> tag. This attribute can take the values "left" to align content to the left, "center" to align content to the center, or "right" to align content to the right.
| Web development | Software development |
|---|---|
| HTML | C++ |
| PHP | Fortran |
Aligning the table itself
Just as you can set the alignment of table content, you can set the alignment of the table itself. This is done the same way as setting the alignment of table content.
| Web development | Software development |
|---|---|
| HTML | C++ |
| PHP | Fortran |