HTML table tag

Example

A simple HTML table, containing two columns and two rows:

<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
</table>

Definition and Usage

The <table> tag defines an HTML table.

An HTML table consists of one <table> element and one or more <tr><th>, and <td> elements.

The <tr> element defines a table row, the <th> element defines a table header, and the <td> element defines a table cell.

An HTML table may also include <caption><colgroup><thead><tfoot>, and <tbody> elements.

Browser Support

Element
<table> Yes Yes Yes Yes Yes

Global Attributes

The <table> tag also supports the Global Attributes in HTML.

Event Attributes

The <table> tag also supports the Event Attributes in HTML.

More Examples

Example

Collapsed borders : Two borders of different cell will combine to form one border.

How to add collapsed borders to a table (with CSS):

<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>

<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
<tr>
<td>February</td>
<td>Rs.80</td>
</tr>
</table>

</body>
</html>

Example

How to right-align a table (with CSS):

<table style="float:right">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
<tr>
<td>February</td>
<td>Rs.80</td>
</tr>
</table>

Example

How to center-align a table (with CSS):

<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
table.center {
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>

<table class="center">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
<tr>
<td>February</td>
<td>Rs.80</td>
</tr>
</table>

Example

How to add background-color to a table (with CSS):

<table style="background-color:#0000ff; color: #fff">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
<tr>
<td>February</td>
<td>Rs.80</td>
</tr>
</table>

Example

How to add padding to a table (with CSS):

<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}

th, td {
padding: 10px;
}
</style>
</head>
<body>

<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
<tr>
<td>February</td>
<td>Rs.80</td>
</tr>
</table>

</body>
</html>

Example

How to set table width (with CSS):

<table style="width:400px">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
<tr>
<td>February</td>
<td>Rs.80</td>
</tr>
</table>

Example

How to create table headers:

<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
</tr>
<tr>
<td>SamS</td>
<td>sam.s@lovelife.com</td>
<td>720-911-7653</td>
</tr>
</table>

Example

How to create a table with a caption:

<table>
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
<tr>
<td>February</td>
<td>Rs.80</td>
</tr>
</table>

Example

How to define table cells that span more than one row or one column:

<table>
<tr>
<th>Name</th>
<th>Email</th>
<th colspan="2">Phone</th>
</tr>
<tr>
<td>SamS</td>
<td>sam.s@lovelife.com</td>
<td>123-45-678</td>
<td>720-911-7653</td>
</tr>
</table>

Default CSS Settings

Most browsers will display the <table> element with the following default values:

Example

table {
display: table;
border-collapse: separate;
border-spacing: 2px;
border-color: gray;
}

Comments