HTML Table

HTML Table Tutorials

1. HTML Table

HTML table tag is used to display data in tabular form (row * column). There can be many columns in a row.

We can create a table to display data in tabular form, using <table> element, with the help of <tr> , <td>, and <th> elements.

In Each table, table row is defined by <tr> tag, table header is defined by <th>, and table data is defined by <td> tags.

HTML tables are used to manage the layout of the page e.g. header section, navigation bar, body content, footer section etc. But it is recommended to use div tag over table to manage the layout of the page .

2. HTML Table Tags

Tag Description
<table> It defines a table.
<tr> It defines a row in a table.
<th> It defines a header cell in a table.
<td> It defines a cell in a table.
<caption> It defines the table caption.
<colgroup> It specifies a group of one or more columns in a table for formatting.
<col> It is used with <colgroup> element to specify column properties for each column.
<tbody> It is used to group the body content in a table.
<thead> It is used to group the header content in a table.
<tfooter> It is used to group the footer content in a table.

3. HTML Table Example

Let's see the example of HTML table tag. It output is shown above.

 
  1. <table>  
  2. <tr><th>First_Name</th><th>Last_Name</th><th>Marks</th></tr>  
  3. <tr><td>Sonoo</td><td>Jaiswal</td><td>60</td></tr>  
  4. <tr><td>James</td><td>William</td><td>80</td></tr>  
  5. <tr><td>Swati</td><td>Sironi</td><td>82</td></tr>  
  6. <tr><td>Chetna</td><td>Singh</td><td>72</td></tr>  
  7. </table>  

4. Output:

First_Name Last_Name Marks
Sonoo Jaiswal 60
James William 80
Swati Sironi 82
Chetna Singh 72

In the above html table, there are 5 rows and 3 columns = 5 * 3 = 15 values.

5. HTML Table with Border

There are two ways to specify border for HTML tables.

  1. By border attribute of table in HTML
  2. By border property in CSS

6. HTML Border attribute

You can use border attribute of table tag in HTML to specify border. But it is not recommended now.

 
  1. <table border="1">  
  2. <tr><th>First_Name</th><th>Last_Name</th><th>Marks</th></tr>  
  3. <tr><td>Sonoo</td><td>Jaiswal</td><td>60</td></tr>  
  4. <tr><td>James</td><td>William</td><td>80</td></tr>  
  5. <tr><td>Swati</td><td>Sironi</td><td>82</td></tr>  
  6. <tr><td>Chetna</td><td>Singh</td><td>72</td></tr>  
  7. </table>  

7. Output:

First_Name Last_Name Marks
Sonoo Jaiswal 60
James William 80
Swati Sironi 82
Chetna Singh 72

8. CSS Border property

It is now recommended to use border property of CSS to specify border in the table.

 
  1. <style>  
  2. table, th, td {  
  3.   border: 1px solid black;  
  4. }  
  5. style>  

ou can collapse all the borders in one border by border-collapse property. It will collapse the border into one.

 
  1. <style>  
  2. table, th, td {  
  3.   border: 2px solid black;  
  4.   border-collapse: collapse;  
  5. }  
  6. </style>  

 

9. Output:

Name Last Name Marks
Sonoo Jaiswal 60
James William 80
Swati Sironi 82
Chetna Singh 72

10. HTML Table with cell padding

You can specify padding for table header and table data in two ways:

  1. By cellpadding attribute of table in HTML
  2. By padding property in CSS

The cellpadding attribute of the HTML table tag is obsolete now. It is recommended to use CSS. So let's see the code of CSS.

 
  1. <style>  
  2. table, th, td {  
  3.   border: 1px solid pink;  
  4.   border-collapse: collapse;  
  5. }  
  6. th, td {  
  7.   padding: 10px;  
  8. }  
  9. </style>  

11. Output:

Name Last Name Marks
Sonoo Jaiswal 60
James William 80
Swati Sironi 82
Chetna Singh 72

12. HTML Table width:

We can specify the HTML table width using the CSS width property. It can be specify in pixels or percentage.

We can adjust our table width as per our requirement. Following is the example to display table with width.

 
  1. table{  
  2.      width: 100%;   
  3.     }  

13. Example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>table</title>  
  5.     <style>  
  6.         table{  
  7.             border-collapse: collapse;  
  8.             width: 100%;   
  9.         }  
  10.     th,td{  
  11.         border: 2px solid green;   
  12.         padding: 15px;  
  13.     }  
  14.                
  15.     </style>  
  16.   </head>  
  17. <body>  
  18.   <table>  
  19.     <tr>  
  20.         <th>1 header</th>  
  21.         <th>1 header</th>  
  22.         <th>1 header</th>  
  23.     </tr>  
  24.     <tr>  
  25.         <td>1data</td>  
  26.         <td>1data</td>  
  27.         <td>1data</td>  
  28.     </tr>  
  29.     <tr>  
  30.         <td>2 data</td>  
  31.         <td>2 data</td>  
  32.         <td>2 data</td>  
  33.     </tr>  
  34.     <tr>  
  35.         <td>3 data</td>  
  36.         <td>3 data</td>  
  37.         <td>3 data</td>  
  38.     </tr>  
  39. </table>  
  40. </body>  
  41. </html>  

14. Output:

15. HTML Table with colspan

If you want to make a cell span more than one column, you can use the colspan attribute.

It will divide one cell/row into multiple columns, and the number of columns depends on the value of colspan attribute.

Let's see the example that spans two columns.

CSS code:

 
  1. <style>  
  2. table, th, td {  
  3.   border: 1px solid black;  
  4.   border-collapse: collapse;  
  5. }  
  6. th, td {  
  7.   padding: 5px;  
  8. }  
  9. </style>  

HTML code:

 
  1. <table style="width:100%">  
  2.   <tr>  
  3.   <th>Name</th>  
  4.   <th colspan="2">Mobile No.</th>  
  5.   </tr>  
  6.   <tr>  
  7.   <td>Ajeet Maurya</td>  
  8.   <td>7503520801</td>  
  9.   <td>9555879135</td>  
  10.   </tr>  
  11. </table>  

16. HTML table with caption

HTML caption is diplayed above the table. It must be used after table tag only.

 
  1. <table>  
  2. <caption>Student Records</caption>  
  3. <tr><th>First_Name</th><th>Last_Name</th><th>Marks</th></tr>  
  4. <tr><td>Vimal</td><td>Jaiswal</td><td>70</td></tr>  
  5. <tr><td>Mike</td><td>Warn</td><td>60</td></tr>  
  6. <tr><td>Shane</td><td>Warn</td><td>42</td></tr>  
  7. <tr><td>Jai</td><td>Malhotra</td><td>62</td></tr>  
  8. </table>  

17. Styling HTML table even and odd cells

CSS code:

 
  1. <style>  
  2. table, th, td {  
  3.   border: 1px solid black;  
  4.   border-collapse: collapse;  
  5. }  
  6. th, td {  
  7.   padding: 10px;  
  8. }  
  9. table#alter tr:nth-child(even) {  
  10.   background-color: #eee;  
  11. }  
  12. table#alter tr:nth-child(odd) {  
  13.   background-color: #fff;  
  14. }  
  15. table#alter th {  
  16.   color: white;  
  17.   background-color: gray;  
  18. }  
  19. </style>  

18. Output: