HTML Tables
HTML Tables
We will want to consider using HTML tables in our website. In addition to creating
HTML tables to present data in rows and columns, we can also create HTML tables to organize
information on our web page. Coding HTML tables into our web page is fairly easy since we
need only understand a few basic table codes.
A table is an arrangement of data in rows and columns, or possibly in a more complex structure.
Tables are widely used in communication, research, and data analysis.
Tables are useful for various tasks such as presenting text information and numerical
data.
Tables can be used to compare two or more items in tabular form layout.
Tables are used to create databases.
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.
Syntax :
Let's see the example of HTML table tag. It output is shown above.
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr><th>Day Order</th><th>I</th><th>II</th><th>III</th><th>IV</th><th>V</th></tr>
<tr><td>I</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Games</td>
</tr>
<tr><td>II</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Library</td
></tr>
<tr><td>III</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Associatio
n</td></tr>
<tr><td>IV</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Remedial
</td></tr>
<tr><td>V</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Library</td
></tr>
<tr><td>VI</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Games</t
d></tr>
</table>
</body>
</html>
Output:
Table Heading
Table heading can be defined as <th> tag. Headings, which are defined in <th> tag are
centered and bold by default. This tag is used to define the heading of a cell in the table.
Syntax:
Example
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr><th>Day Order</th><th>I</th><th>II</th><th>III</th><th>IV</th><th>V</th></tr>
<tr><td>I</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Games</td>
</tr>
<tr><td>II</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Library</td
></tr>
<tr><td>III</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Associatio
n</td></tr>
<tr><td>IV</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Remedial
</td></tr>
<tr><td>V</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Library</td
></tr>
<tr><td>VI</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Games</t
d></tr>
</table>
</body>
</html>
Output:
HTML Border attribute : We can use border attribute of <table> tag in HTML to specify
border. the border is an attribute of <table> tag and it is used to put a border across all the cells.
The border attribute is used to specify the thickness of the table border in pixels. Its represented
in integer value. If you do not need a border, then you can use border = "0".
Syntax:
Example :
<table border="3">
<tr><th>Day Order</th><th>I</th><th>II</th><th>III</th><th>IV</th><th>V</th></tr>
<tr><td>I</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Game
s</td></tr>
<tr><td>II</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Libra
ry</td></tr>
<tr><td>III</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Ass
ociation</td></tr>
<tr><td>IV</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Rem
edial</td></tr>
<tr><td>V</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Libra
ry</td></tr>
<tr><td>VI</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Ga
mes</td></tr>
</table>
Output:
Polishing your table
To give our table a more polished look, we can include commands that will adjust the
size of our table, add space in the cell, add space between rows, and align the data in a cell.
Working with these commands is basically a process of trial and error to create the most
appealing presentation of our information. The type of table that we create and the overall design
of our web site will help you determine what works best for our table.
Some of the commands that enable you to customize your table include:
The WIDTH=n% command sets the width of your table as a percentage of the screen.
The letter n designates the percentage that you assign to this command. For example, if you
want the width of your table to be one half the width of the screen, you would include the
WIDTH="50%" command in the beginning table command.
Example
<html>
<head>
<title></title>
</head>
<body>
<table border="1" width="400" height="150" >
<tr><th>Day Order</th><th>I</th><th>II</th><th>III</th><th>IV</th><th>V</th></tr>
<tr><td>I</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Games</td>
</tr>
<tr><td>II</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Library</td
></tr>
<tr><td>III</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Associatio
n</td></tr>
<tr><td>IV</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Remedial
</td></tr>
<tr><td>V</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Library</td
></tr>
<tr><td>VI</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Games</t
d></tr>
</table>
</body>
</html>
Output:
Notice that the TABLE command now includes the WIDTH="50%" command. This command
extends the table across one half of the width of the text.
Alignment of Data:
The ALIGN=(LEFT, RIGHT, or CENTER) command will horizontally align the data in
a cell. For example, if you wish to place the data in the center of each cell in a row, you would
include the ALIGN=CENTER command within the row command.
The VALIGN=(TOP, MIDDLE, or BOTTOM) command will vertically align the data in
a cell. For example, if you wish to place the data in the center of each cell in a row, you would
include the ALIGN=MIDDLE command within the row command.
In addition to the codes that were explained in the previous sections, the table below now
includes some of these commands.
Example :
<html>
<head>
<title></title>
</head>
<body>
<table border="1" width="400" height="150" >
<tr align="center"><th>Day
Order</th><th>I</th><th>II</th><th>III</th><th>IV</th><th>V</th></tr>
<tr align="center">
<td>I</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Games</td>
</tr>
<tr align="center">
<td>II</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Library</td>
</tr>
<tr align="center">
<td>III</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Association</t
d>
</tr>
<tr align="center">
<td>IV</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Remedial</td>
</tr>
<tr align="center">
<td>V</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Library</td>
</tr>
<tr align="center">
<td>VI</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Games</td>
</tr>
</table>
</body>
</html>
Output:
Finally, the ALIGN="CENTER" command places all the cells in the above table.
The CELLPADDING= cellpadding represents the distance between content of the cell
and edge of the cell.n command adjusts the vertical dimension of the cells. The letter n
designates the numerical value that you assign to this command.
The CELLSPACING= The cellspacing attribute defines space between table cells,(i.e)
this attribute is used to specify the space between the individual cells n command sets the space
or border around the cells. The letter n designates the numerical value that you assign to this
command.
Example
Live Demo
<html>
<head>
<title></title>
</head>
<body>
<table border="1" width="400" height="150" cellpadding = "10" cellspacing = "10" >
<tr align="center"><th>Day
Order</th><th>I</th><th>II</th><th>III</th><th>IV</th><th>V</th>
</tr>
<tr align="center">
<td>I</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Games<
/td>
</tr>
<tr align="center">
<td>II</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Library
</td>
</tr>
<tr align="center">
<td>III</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Associ
ation</td>
</tr>
<tr align="center">
<td>IV</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Remed
ial</td>
</tr>
<tr align="center">
<td>V</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Library
</td>
</tr>
<tr align="center">
<td>VI</td><td>Tamil</td><td>English</td><td>C</td><td>DP&CO</td><td>Games
</td>
</tr>
</table>
</body>
</html>
Output :
the CELLPADDING="10" command increases the vertical dimension of the cells, and the
CELLSPACING="10" command increases the border around the cells
TD Tag :
This tag is used to define the data of each cell in the table.
Syntax:
NOWRAP : This attribute is used to display the content of the cell in one line.
Colspan attribute : if you want to merge two or more columns into a single column. it takes
an integer value. Similar way you will
rowspan attribute: if you want to merge two or more rows in a single row. it takes an integer
value.
Example
<html>
<head>
<title></title>
</head>
<body>
<tr align="center"><th>Day
Order</th><th>I</th><th>II</th><th>III</th><th>IV</th><th>V</th></tr>
</tr>
</tr>
</tr>
</tr>
</tr>
</table>
</body>
</html>
Output:
Tables Backgrounds
You can set table background using one of the following two ways −
bgcolor attribute − You can set background color for whole table or just for one cell.
background attribute − You can set background image for whole table or just for one
cell.
You can also set border color also using bordercolor attribute.
Example
<html>
<head>
<title></title>
</head>
<body>
<table border="1" bordercolor="red" bgcolor="yellow" width="400" height="150" >
<tr align="center"><td colspan="6"><b>B.Sc I- CS Time Table</b></td>
<tr align="center"><th>Day Order </th> <th>I</th> <th>II</th> <th>III</th> <th>IV</th>
<th>V</th></tr>
<tr align="center"> <td>I</td><td rowspan = "6">Tamil</td><td rowspan="6">English</td><td
colspan="3">C LAB</td>
</tr>
<tr align="center"> <td>II</td> <td>C</td><td>DP&CO</td><td>Library</td>
</tr>
<tr align="center"> <td>III</td><td>C</td><td>DP&CO</td><td>Association</td>
</tr>
<tr align="center"> <td>IV</td><td>C</td><td>DP&CO</td><td>Remedial</td>
</tr>
<tr align="center"> <td>V</td><td colspan="2">C LAB</td><td>Library</td>
</tr>
<tr align="center"> <td>VI</td><td>C</td><td>DP&CO</td><td>Games</td>
</tr>
</table>
</body>
</html>
Output :
Here is an example of using background attribute. Here we will use an image available in
/images directory.
Example
<html>
<head>
<title></title>
</head>
<body>
<tr align="center"><th>Day
Order</th><th>I</th><th>II</th><th>III</th><th>IV</th><th>V</th></tr>
<tr align="center"> <td>I</td><td rowspan = "6">Tamil</td><td rowspan="6">English</td><td
colspan="3">C LAB</td>
</tr>
</tr>
</tr>
</tr>
</tr>
</tr>
</table>
</body>
</html>
Output :
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.
<caption> Tag
Example :
<html>
<head>
<title></title>
</head>
<body>
<table border="1" width="400" height="150" >
<caption><b>B.Sc I- CS Time Table</b></caption>
<tr align="center"><th>Day
Order</th><th>I</th><th>II</th><th>III</th><th>IV</th><th>V</th></tr>
<tr align="center"> <td>I</td><td rowspan = "6">Tamil</td><td rowspan="6">English</td><td
colspan="3">C LAB</td>
</tr>
<tr align="center"> <td>II</td> <td>C</td><td>DP&CO</td><td>Library</td>
</tr>
<tr align="center"> <td>III</td><td>C</td><td>DP&CO</td><td>Association</td>
</tr>
<tr align="center"> <td>IV</td><td>C</td><td>DP&CO</td><td>Remedial</td>
</tr>
<tr align="center"> <td>V</td><td colspan="2">C LAB</td><td>Library</td>
</tr>
<tr align="center"> <td>VI</td><td>C</td><td>DP&CO</td><td>Games</td>
</tr>
</table>
</body>
</html>
Output :
Example
<html>
<head>
<title></title>
</head>
<body>
<table border="1" bgcolor="cyan" width="400" height="150" >
<thead><tr><td colspan="6" align="center"> <b>Time Table</b></td></tr></thead>
<tfoot><tr><td colspan="6" align="right"><b>Computer Science</b></td></tr></tfoot>
<tr align="center"><th>Day
Order</th><th>I</th><th>II</th><th>III</th><th>IV</th><th>V</th></tr>
<tr align="center"> <td>I</td><td rowspan = "6">Tamil</td><td rowspan="6">English</td><td
colspan="3">C LAB</td>
</tr>
<tr align="center"> <td>II</td> <td>C</td><td>DP&CO</td><td>Library</td>
</tr>
<tr align="center"> <td>III</td><td>C</td><td>DP&CO</td><td>Association</td>
</tr>
<tr align="center"> <td>IV</td><td>C</td><td>DP&CO</td><td>Remedial</td>
</tr>
<tr align="center"> <td>V</td><td colspan="2">C LAB</td><td>Library</td>
</tr>
<tr align="center"> <td>VI</td><td>C</td><td>DP&CO</td><td>Games</td>
</tr>
</table>
</body>
</html>
Output:
Nested Tables
You can use one table inside another table. Not only tables you can use almost all the
tags inside table data tag <td>.
Example
Following is the example of using another table and other tags inside a table cell.
<html>
<head>
<title></title>
</head>
<body>
<table border=”1″ width="400" height="150">
<tr>
<td align="center" colspan="2"> <b>Due Information</b></td>
</tr>
<tr>
<td>
<table border=”1″>
<tr>
<th>Name</th>
<th>Dues</th>
</tr>
<tr>
<td>Raman</td>
<td>5000</td>
</tr>
<tr>
<td>Shabbir</td>
<td>70000</td>
</tr>
</table>
</td>
<td>
<ul>
<li>Furniture</li>
<li>jewells</li>
</ul>
</td>
</tr>
<tr>
<td align = "center"><b>Due Details</b></td>
<td align = "center"><b>Purchased Things</b></td>
</tr>
</table></body>
</html>
Output:
Conclusion
Example 1:
Example 3: