HTML Tables
HTML tables are used to organize and display data in rows and columns on a webpage. They are useful for displaying information such as student records, product prices, employee details, schedules, and other structured data.
HTML provides several tags to create and manage tables, including <table>, <tr>, <th>, and <td>.
1. Basic HTML Table
The <table> tag is used to create an HTML table. Each row is created using <tr>, while <th> defines a header cell and <td> defines a regular data cell.
Example:
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
<tr>
<td>Rahul</td>
<td>22</td>
<td>Kolkata</td>
</tr>
<tr>
<td>Priya</td>
<td>21</td>
<td>Bankura</td>
</tr>
</table>
Output:
| Name | Age | City |
|---|---|---|
| Rahul | 22 | Kolkata |
| Priya | 21 | Bankura |
2. HTML Table Tags
HTML provides different tags to define the structure of a table.
| Tag | Description |
|---|---|
| <table> | Defines an HTML table. |
| <tr> | Defines a table row. |
| <th> | Defines a table header cell. |
| <td> | Defines a table data cell. |
| <thead> | Groups the table header content. |
| <tbody> | Groups the main table body content. |
| <tfoot> | Groups the table footer content. |
| <caption> | Adds a title or caption to a table. |
3. HTML Table with a Caption
The <caption> tag adds a title to an HTML table. It should be placed immediately after the opening <table> tag.
Example:
<table border="1">
<caption>Student Information</caption>
<tr>
<th>Name</th>
<th>Class</th>
</tr>
<tr>
<td>Amit</td>
<td>10</td>
</tr>
</table>
4. HTML Table with thead, tbody, and tfoot
You can divide a table into three main sections using <thead>, <tbody>, and <tfoot>.
- <thead>: Contains the table heading rows.
- <tbody>: Contains the main data rows.
- <tfoot>: Contains summary or footer rows.
Example:
<table border="1">
<thead>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Laptop</td>
<td>50000</td>
</tr>
<tr>
<td>Mouse</td>
<td>500</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td>50500</td>
</tr>
</tfoot>
</table>
5. HTML Table Borders
You can use CSS to add borders to table elements. The border property defines the border around cells and the table.
Example:
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 10px;
}
</style>
6. HTML Table Cell Padding
Cell padding controls the space between the content of a table cell and its border. Use the CSS padding property to add space inside table cells.
Example:
<style>
th, td {
padding: 15px;
}
</style>
7. HTML Table Cell Spacing
Cell spacing controls the space between table cells. You can use the CSS border-spacing property when borders are separated.
Example:
<style>
table {
border-spacing: 10px;
}
</style>
8. HTML Table Colspan
The colspan attribute allows a table cell to span across multiple columns.
Example:
<table border="1">
<tr>
<th colspan="2">Student Details</th>
</tr>
<tr>
<td>Name</td>
<td>Rahul</td>
</tr>
</table>
Output:
| Student Details | |
|---|---|
| Name | Rahul |
9. HTML Table Rowspan
The rowspan attribute allows a table cell to span across multiple rows.
Example:
<table border="1">
<tr>
<th>Name</th>
<th>Subject</th>
</tr>
<tr>
<td rowspan="2">Rahul</td>
<td>HTML</td>
</tr>
<tr>
<td>CSS</td>
</tr>
</table>
Output:
| Name | Subject |
|---|---|
| Rahul | HTML |
| CSS |
10. HTML Table Background Color
You can use CSS background-color to change the background color of a table, row, or individual cell.
Example:
<table style="width:100%; border-collapse:collapse;">
<tr style="background-color:#2563eb; color:white;">
<th>Name</th>
<th>Age</th>
</tr>
<tr style="background-color:#f1f5f9;">
<td>Rahul</td>
<td>22</td>
</tr>
</table>
11. Responsive HTML Tables
Sometimes a table contains many columns and does not fit on smaller screens. You can make it horizontally scrollable using CSS overflow-x: auto.
Example:
<style>
.table-container {
width: 100%;
overflow-x: auto;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 10px;
border: 1px solid #ccc;
white-space: nowrap;
}
</style>
<div class="table-container">
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Course</th>
</tr>
<tr>
<td>Rahul</td>
<td>rahul@example.com</td>
<td>HTML</td>
</tr>
</table>
</div>
12. Important HTML Table Attributes
| Attribute | Purpose |
|---|---|
| colspan | Makes a cell span multiple columns. |
| rowspan | Makes a cell span multiple rows. |
| scope | Identifies the cells associated with a header. |
border attribute is considered outdated for modern styling. Use CSS properties such as border, padding, and border-collapse to create professional tables. Use <th scope="col"> and <th scope="row"> to improve table accessibility.
Conclusion
HTML tables provide an effective way to organize and present structured information. By learning table tags, borders, cell spacing, colspan, rowspan, and responsive table techniques, you can create clear and well-organized tables for your websites.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Tables</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
h2 {
color: #2563eb;
}
.table-container {
width: 100%;
overflow-x: auto;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 25px;
}
th, td {
border: 1px solid #cbd5e1;
padding: 12px;
text-align: left;
}
th {
background: #2563eb;
color: white;
}
tr:nth-child(even) {
background: #f1f5f9;
}
</style>
</head>
<body>
<h2>1. Basic HTML Table</h2>
<div class="table-container">
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr>
<td>Rahul</td>
<td>22</td>
<td>Kolkata</td>
</tr>
<tr>
<td>Priya</td>
<td>21</td>
<td>Bankura</td>
</tr>
<tr>
<td>Amit</td>
<td>23</td>
<td>Delhi</td>
</tr>
</tbody>
</table>
</div>
<h2>2. Table with Colspan</h2>
<table>
<tr>
<th colspan="2">Student Information</th>
</tr>
<tr>
<td>Name</td>
<td>Rahul</td>
</tr>
<tr>
<td>Course</td>
<td>HTML</td>
</tr>
</table>
<h2>3. Table with Rowspan</h2>
<table>
<tr>
<th>Name</th>
<th>Subject</th>
</tr>
<tr>
<td rowspan="2">Rahul</td>
<td>HTML</td>
</tr>
<tr>
<td>CSS</td>
</tr>
</table>
<h2>4. Table Footer</h2>
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Laptop</td>
<td>₹50,000</td>
</tr>
<tr>
<td>Mouse</td>
<td>₹500</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>Total</th>
<th>₹50,500</th>
</tr>
</tfoot>
</table>
</body>
</html>