Saturday, 19 September 2015

HTML Table

HTML Table

HTML table tag is used to display data in tabular form (row * column). There can be many columns in a row.
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 .

HTML Table Tags

TagDescription
<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.

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>  

Output:
First_NameLast_NameMarks
SonooJaiswal60
JamesWilliam80
SwatiSironi82
ChetnaSingh72
In the above html table, there are 5 rows and 3 columns = 5 * 3 = 15 values.

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

1) 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>  

Output:
First_NameLast_NameMarks
SonooJaiswal60
JamesWilliam80
SwatiSironi82
ChetnaSingh72

2) CSS Border property

It is now recommended to use border property of CSS to specify border in table.
  1. <style>  
  2. table, th, td {  
  3.     border: 1px solid black;  
  4. }  
  5. </style>  

You can collapse all the borders in one border by border-collapse property.
  1. <style>  
  2. table, th, td {  
  3.     border: 2px solid black;  
  4.     border-collapse: collapse;  
  5. }  
  6. </style>  
Output:
NameLast NameMarks
SonooJaiswal60
JamesWilliam80
SwatiSironi82
ChetnaSingh72

HTML Table with cell padding

You can specify padding for table header and table data by two ways:
  1. By cellpadding attribute of table in HTML
  2. By padding property in CSS
The cellpadding attribute of HTML table tag is obselete 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>  

Output:
NameLast NameMarks
SonooJaiswal60
JamesWilliam80
SwatiSironi82
ChetnaSingh72

HTML Table with colspan

If you want to make a cell span more than one column, you can use the colspan attribute.
Let's see the example that span 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>  

Output:
NameMobile No.
Ajeet Maurya75035208019555879135

HTML Table with rowspan

If you want to make a cell span more than one row, you can use the rowspan attribute.
Let's see the example that span two rows.
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. </style>  
HTML code:
  1. <table>    
  2. <tr><th>Name</th><td>Ajeet Maurya</td></tr>    
  3. <tr><th rowspan="2">Mobile No.</th><td>7503520801</td></tr>    
  4. <tr><td>9555879135</td></tr>    
  5. </table>   

Output:
NameAjeet Maurya
Mobile No.7503520801
9555879135

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>  


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>  

Output:
html table even and odd

No comments:

Post a Comment