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.
Syntax:
<table>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
</table>
Border Attribute
The HTML <table> border Attribute is used to specify the border of a table. It sets the border around the table cells.
Syntax:
<table border=”1″>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
</table>
Background Attribute
The HTML <table> bgcolor Attribute is use to specify the background color of a table.
Syntax:
<table border=”1″ bgcolor=”yellow”>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
</table>
Table Heading
Table heading can be defined using the <th> tag. Normally you will put your top row as a table heading as shown below, otherwise, you can use <th> element in any row. Headings, which are defined in the <th> tag are centered and bold by default.
Syntax:
<table border=”1″>
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>Ram</td>
<td>8,000</td>
</tr>
</table>
Cellpadding & Cellspacing Attributes
There are two attributes called cellpadding and cellspacing which you will use to adjust the white space in your table cells. The cellspacing attribute defines space between table cells, while cellpadding represents the distance between cell borders and the content within a cell.
Syntax: Output
<table border = “1” cellpadding = “15″ cellspacing = “15″>
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>Ram</td>
<td>8,000</td>
</tr>
</table>
Colspan and Rowspan Attributes
You will use colspan attribute if you want to merge two or more columns into a single column. Similar way you will use rowspan if you want to merge two or more rows.
Syntax:
<table border = “1”>
<tr>
<td rowspan = “2”>Row 1 Cell 1</td>
<td>Row 1 Cell 2</td>
<td>Row 1 Cell 3</td>
</tr>
<tr>
<td>Row 2 Cell 2</td>
<td>Row 2 Cell 3</td>
</tr>
<tr>
<td colspan = “3”>Row 3 Cell 1</td>
</tr>
</table>
Table Caption
The caption tag will serve as a title or explanation for the table and it shows up at the top of the table. This tag is deprecated in newer version of HTML/XHTML.
Syntax:
<table border = “1” >
<caption>This is the caption</caption>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</table>
Table Header, Body, and Footer
Tables can be divided into three portions − a header, a body, and a foot. The head and foot are rather similar to headers and footers in a word-processed document that remain the same for every page, while the body is the main content holder of the table.
The three elements for separating the head, body, and foot of a table are −
<thead> − to create a separate table header.
<tbody> − to indicate the main body of the table.
<tfoot> − to create a separate table footer.
Syntax:
<table border = “1” width = “50%”>
<thead>
<tr>
<td colspan = “2”>This is the head of the table</td>
</tr>
</thead>
<tbody>
<tr>
<td>Cell 1</td>
<td>Cell 2</td> </tr>
</tbody>
<tfoot>
<tr>
<td colspan = “2”>This is the foot of the table</td>
</tr>
</tfoot>
</table>