5.1 Tables
The HTML tables allow web authors to arrange data like text, images, links, other tables, etc. into rows and columns of cells.
The HTML tables are created using the <table> tag in which the <tr> tag is used to create table rows and <td> tag is used to create data cells. The elements under <td> are regular and left aligned by default
Example 5.1:
Company | Contact | Country |
---|---|---|
Alfreds Futterkiste | Maria Anders | Germany |
Centro comercial Moctezuma | Francisco Chang | Mexico |
Ernst Handel | Roland Mendel | Austria |
Island Trading | Helen Bennett | UK |
Laughing Bacchus Winecellars | Yoshi Tannamuri | Canada |
Magazzini Alimentari Riuniti | Giovanni Rovelli | Italy |
5.2 Define an HTML Table
The <table>
tag defines an HTML table.
Each table row is defined with a <tr>
tag. Each table header is defined with a <th>
tag. Each table data/cell is defined with a <td>
tag.
By default, the text in <th>
elements are bold and centered.
By default, the text in <td>
elements are regular and left-aligned.
Example 5.2:
A simple HTML table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<table style="width:100%"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> </table> |
Note: The <td>
elements are the data containers of the table.
They can contain all sorts of HTML elements; text, images, lists, other tables, etc.
5.3 HTML Table – Add a Border
To add a border to a table, use the CSS border
property:
Example 5.3:
1 2 3 |
table, th, td { border: 1px solid black; } |
Example 5.4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px solid black; } </style> </head> <body> <h2>Table With Border</h2> <p>Use the CSS border property to add a border to the table.</p> <table style="width:100%"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> <tr> <td>John</td> <td>Doe</td> <td>80</td> </tr> </table> </body> </html> |
Table With Border
Use the CSS border property to add a border to the table.
Remember to define borders for both the table and the table cells.
5.4 HTML Table – Collapsed Borders
To let the borders collapse into one border, add the CSS border-collapse
property:
Example 5.5:
CSS
1 2 3 4 |
table, th, td { border: 1px solid black; border-collapse: collapse; } |
Collapsed Borders
If you want the borders to collapse into one border, add the CSS border-collapse property.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } </style> </head> <body> <table style="width:100%"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> <tr> <td>John</td> <td>Doe</td> <td>80</td> </tr> </table> </body> </html> |
5.5 HTML Table -Add Cell Padding
- Cell padding specifies the space between the cell content and its borders.
- If you do not specify a padding, the table cells will be displayed without padding.
To set the padding, use the CSS padding
property:
Example 5.6:
CSS
1 2 3 |
th, td { padding: 15px; } |
Cellpadding
Cell padding specifies the space between the cell content and its borders.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } th, td { padding: 15px; } </style> </head> <body> <h2>Cellpadding</h2> <p>Cell padding specifies the space between the cell content and its borders.</p> <table style="width:100%"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> <tr> <td>John</td> <td>Doe</td> <td>80</td> </tr> </table> <p><strong>Tip:</strong> Try to change the padding to 5px.</p> </body> </html> |
5.6 HTML Table – Left-align Headings
By default, table headings are bold and centered.
To left-align the table headings, use the CSS text-align
property:
Example 5.7:
CSS
1 2 3 |
th { text-align: left; } |
Left-align Headings
To left-align the table headings, use the CSS text-align property.
5.7 HTML Table – Add Border Spacing
Border spacing specifies the space between the cells.
To set the border spacing for a table, use the CSS border-spacing
property:
Example 5.8:
CSS
1 2 3 |
table { border-spacing: 5px; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px solid black; padding: 5px; } table { border-spacing: 15px; } </style> </head> <body> <h2>Border Spacing</h2> <p>Border spacing specifies the space between the cells.</p> <table style="width:100%"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> <tr> <td>John</td> <td>Doe</td> <td>80</td> </tr> </table> <p><strong>Tip:</strong> Try to change the border-spacing to 5px.</p> </body> </html> |
Note: If the table has collapsed borders,
border-spacing
has no effect.
5.8 HTML Table – Cell that Spans Many Columns
To make a cell span more than one column, use the colspan
attribute:
Example 5.9:
1 2 3 4 5 6 7 8 9 10 11 |
<table style="width:100%"> <tr> <th>Name</th> <th colspan="2">Telephone</th> </tr> <tr> <td>Bill Gates</td> <td>55577854</td> <td>55577855</td> </tr> </table> |
Cell that spans two columns
To make a cell span more than one column, use the colspan attribute
5.9 HTML Table – Cell that Spans Many Columns
To make a cell span more than one row, use the rowspan
attribute:
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<table style="width:100%"> <tr> <th>Name:</th> <td>Bill Gates</td> </tr> <tr> <th rowspan="2">Telephone:</th> <td>55577854</td> </tr> <tr> <td>55577855</td> </tr> </table> |
Cell that spans two rows
To make a cell span more than one row, use the rowspan attribute.
HTML Table – Add a Caption
To add a caption to a table, use the <caption>
tag:
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<table style="width:100%"> <caption>Monthly savings</caption> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td>January</td> <td>$100</td> </tr> <tr> <td>February</td> <td>$50</td> </tr> </table> |
Table Caption
To add a caption to a table, use the caption tag.
Note: The
<caption>
tag must be inserted immediately after the<table>
tag.
A Special Style for One Table
To define a special style for one particular table, add an id
attribute to the table:
Example
1 2 3 4 5 6 7 8 9 10 11 12 |
<table id="t01"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> </table> |
Now you can define a special style for this table:
1 2 3 4 |
#t01 { width: 100%; background-color: #f1f1c1; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } th, td { padding: 15px; text-align: left; } #t01 { width: 100%; background-color: #f1f1c1; } </style> </head> <body> <h2>Styling Tables</h2> <table style="width:100%"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> <tr> <td>John</td> <td>Doe</td> <td>80</td> </tr> </table> <br> <table id="t01"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> <tr> <td>John</td> <td>Doe</td> <td>80</td> </tr> </table> </body> </html> |
Styling Tables
And add more styles:
1 2 3 4 5 6 7 8 9 10 |
#t01 tr:nth-child(even) { background-color: #eee; } #t01 tr:nth-child(odd) { background-color: #fff; } #t01 th { color: white; background-color: black; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<!DOCTYPE html> <html> <head> <style> table { width:100%; } table, th, td { border: 1px solid black; border-collapse: collapse; } th, td { padding: 15px; text-align: left; } #t01 tr:nth-child(even) { background-color: #eee; } #t01 tr:nth-child(odd) { background-color: #fff; } #t01 th { background-color: black; color: white; } </style> </head> <body> <h2>Styling Tables</h2> <table> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> <tr> <td>John</td> <td>Doe</td> <td>80</td> </tr> </table> <br> <table id="t01"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> <tr> <td>John</td> <td>Doe</td> <td>80</td> </tr> </table> </body> </html> |
Styling Tables
Chapter Summary
- Use the HTML
<table>
element to define a table - Use the HTML
<tr>
element to define a table row - Use the HTML
<td>
element to define a table data - Use the HTML
<th>
element to define a table heading - Use the HTML
<caption>
element to define a table caption - Use the CSS
border
property to define a border - Use the CSS
border-collapse
property to collapse cell borders - Use the CSS
padding
property to add padding to cells - Use the CSS
text-align
property to align cell text - Use the CSS
border-spacing
property to set the spacing between cells - Use the
colspan
attribute to make a cell span many columns - Use the
rowspan
attribute to make a cell span many rows - Use the
id
attribute to uniquely define one table