HTML Tables

Tables are very useful to arrange in HTML and they are used very frequently by almost all web developers. Tables are just like spreadsheets and they are made up of rows and columns.

You will create a table in HTML/XHTML by using <table> tag. Inside <table> element the table is written out row by row. A row is contained inside a <td> tag . which stands for table row. And each cell is then written inside the row element using a <td> tag . which stands for table data.

<table border="1">
<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>
Try it with the Editor

Here border is an attribute of <table> tag and it is used to put a border across all the cells. If you do not need a border then you can use border="0".

Table Heading – The <th> Element:

Table heading can be defined using <th> element. This tag will be put to replace <td> tag which is used to represent actual data. Normally you will put your top row as table heading as shown below, otherwise you can use <th> element at any place:

<table border="1">
<tr>
    <th>Name</th>
    <th>Salary</th>
</tr>
<tr>
    <td>Prakash Thakur</td>
    <td>Pritam Sarode</td>
</tr>
<tr>
    <td>30000</td>
    <td>40000</td>
</tr>
</table>
Try it with the Editor

Table Cellpadding and Cellspacing:

There are two attribiutes called cellpadding and cellspacing which you will use to adjust the white space in your table cell. Cellspacing defines the width of the border, while cellpadding represents the distance between cell borders and the content within. Following is the example:

<table border="1" cellpadding="5" cellspacing="5">
<tr>
    <th>Name</th>
    <th>Salary</th>
</tr>
<tr>
    <td>Prakash Thakur</td>
    <td>Pritam Sarode</td>
</tr>
<tr>
    <td>30000</td>
    <td>40000</td>
</tr>
</table>
Try it with the Editor

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. Following is the example:

<table border="1">
<tr>
    <th>Column 1</th>
    <th>Column 2</th>
    <th>Column 3</th>
</tr>
<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 2</td>
</tr>
<tr>
    <td colspan="3">Row 3 Cell 1</td>
</tr>
</table>
Try it with the Editor

Tables Backgrounds

You can set table background using of the following two ways:

NOTE : You can set border color also using bordercolor attribute.

<table border="1" bordercolor="red" bgcolor="yellow">
<tr>
    <th>Name</th>
    <th>Salary</th>
</tr>
<tr>
    <td>Prakash Thakur</td>
    <td>Pritam Sarode</td>
</tr>
<tr>
    <td>30000</td>
    <td>40000</td>
</tr>
</table>
Try it with the Editor

Table height and width:

You can set a table width and height using width and height attrubutes. You can specify table width or height in terms of integer value or in terms of percentage of available screen area. Following is the example:

<table border="1" width="400" height="150">
<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>
Try it with the Editor

Using Table Caption:

The caption tags will serve as a title or explanation and show up at the top of the table. This tag is depracated in newer version of HTML/XHTML.

<table border="1" width="100%">
<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>
Try it with the Editor

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 of the table.

The three elements for separating the head, body, and foot of a table are:

<table border="1">
<thead>
    <tr>
        <td colspan="4">This is the head of the table</td>
    </tr>
</thead>
<tfoot>
    <tr>
        <td colspan="4">This is the foot of the table</td>
    </tr>
</tfoot>
<tbody>
    <tr>
        <td>Cell 1</td>
        <td>Cell 2</td>
        <td>Cell 3</td>
        <td>Cell 4</td>
    </tr>
</tbody>
</table>
Try it with the Editor