Tables are one of the most important elements in HTML used for displaying data in a structured format. They arrange information into rows and columns, making it easy to read and compare data.
HTML tables are widely used in:
School result systems
Timetables
Product comparison charts
Banking systems
Employee records
Reports and dashboards
Billing systems
In this article, we will learn:
HTML table basics
Table structure
Table tags
Rows and columns
Table headings
Table borders
Cell spacing and padding
Rowspan and colspan
Table captions
Nested tables
Modern table practices
Practical examples
What is an HTML Table?
An HTML table is used to display data in tabular form.
A table consists of:
Rows
Columns
Cells
Example:
| Name | Age | City |
|---|---|---|
| Rahul | 20 | Kolkata |
| Aman | 21 | Delhi |
HTML creates such structures using table tags.
Basic Structure of an HTML Table
HTML tables are created using:
<table><tr><th><td>
Table Tags and Their Uses
| Tag | Purpose |
|---|---|
<table> | Creates table |
<tr> | Table row |
<th> | Table heading |
<td> | Table data |
<caption> | Table title |
<thead> | Table header section |
<tbody> | Table body |
<tfoot> | Table footer |
Basic Table Syntax
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Rahul</td>
<td>20</td>
</tr>
</table>
Explanation of Table Structure
<table>
Creates the complete table.
<tr>
Defines a table row.
<th>
Creates heading cells.
Text inside <th> becomes bold and centered by default.
<td>
Defines normal data cells.
First Complete Table Program
<!DOCTYPE html>
<html>
<head>
<title>HTML Table</title>
</head>
<body>
<h1>Student Information</h1>
<table border="1">
<tr>
<th>Name</th>
<th>Class</th>
<th>Marks</th>
</tr>
<tr>
<td>Rahul</td>
<td>12</td>
<td>89</td>
</tr>
<tr>
<td>Aman</td>
<td>11</td>
<td>92</td>
</tr>
</table>
</body>
</html>
Output Structure
--------------------------------
| Name | Class | Marks |
--------------------------------
| Rahul | 12 | 89 |
| Aman | 11 | 92 |
--------------------------------
Table Borders
The border attribute adds borders to tables.
Example:
<table border="1">
Border Values
| Value | Effect |
|---|---|
| 0 | No border |
| 1 | Thin border |
| 2+ | Thicker border |
Table Width and Height
You can control table size using:
widthheight
Example:
<table border="1" width="500">
Cell Padding
Cell padding creates space inside cells.
Example:
<table border="1" cellpadding="10">
Effect of Cell Padding
Without padding:
Text touches borders
With padding:
Extra space appears around text
Cell Spacing
Cell spacing creates space between cells.
Example:
<table border="1" cellspacing="5">
Difference Between Padding and Spacing
| Feature | Meaning |
|---|---|
| Padding | Space inside cell |
| Spacing | Space between cells |
Table Caption
The <caption> tag adds a title to the table.
Example:
<table border="1">
<caption>
Student Report
</caption>
</table>
Complete Example with Caption
<table border="1" cellpadding="10">
<caption>
Employee Details
</caption>
<tr>
<th>Name</th>
<th>Department</th>
</tr>
<tr>
<td>Ravi</td>
<td>HR</td>
</tr>
</table>
Aligning Table Content
HTML supports alignment using:
alignvalign
Example:
<td align="center">
Rahul
</td>
Horizontal Alignment Values
| Value | Meaning |
|---|---|
| left | Left aligned |
| center | Center aligned |
| right | Right aligned |
Vertical Alignment Values
| Value | Meaning |
|---|---|
| top | Top |
| middle | Center |
| bottom | Bottom |
Background Colors in Tables
Example:
<table border="1" bgcolor="lightblue">
Individual Cell Color
<td bgcolor="yellow">
Rahul
</td>
Rowspan Attribute
rowspan merges rows vertically.
Example:
<td rowspan="2">Computer</td>
Example of Rowspan
<table border="1">
<tr>
<th>Subject</th>
<th>Name</th>
</tr>
<tr>
<td rowspan="2">Computer</td>
<td>Rahul</td>
</tr>
<tr>
<td>Aman</td>
</tr>
</table>
Output Structure
------------------------
| Subject | Name |
------------------------
| Computer | Rahul |
| | Aman |
------------------------
Colspan Attribute
colspan merges columns horizontally.
Example:
<td colspan="2">Merged Cell</td>
Example of Colspan
<table border="1">
<tr>
<th colspan="2">
Student Details
</th>
</tr>
<tr>
<td>Name</td>
<td>Rahul</td>
</tr>
</table>
Output Structure
-------------------------
| Student Details |
-------------------------
| Name | Rahul |
-------------------------
Nested Tables
A table inside another table is called a nested table.
Example:
<table border="1">
<tr>
<td>
<table border="1">
<tr>
<td>Inner Table</td>
</tr>
</table>
</td>
</tr>
</table>
Table Header, Body, and Footer
Modern HTML uses:
<thead><tbody><tfoot>
Example
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Marks</th>
</tr>
</thead>
<tbody>
<tr>
<td>Rahul</td>
<td>90</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td>90</td>
</tr>
</tfoot>
</table>
Advantages of Using Thead, Tbody, Tfoot
Better structure
Easier styling
Improved accessibility
Better readability
Timetable Example
<!DOCTYPE html>
<html>
<head>
<title>School Timetable</title>
</head>
<body>
<table border="1" cellpadding="10">
<caption>
Weekly Timetable
</caption>
<tr>
<th>Day</th>
<th>Subject</th>
</tr>
<tr>
<td>Monday</td>
<td>Mathematics</td>
</tr>
<tr>
<td>Tuesday</td>
<td>Computer Science</td>
</tr>
</table>
</body>
</html>
Product Table Example
<table border="1" cellpadding="10">
<tr>
<th>Product</th>
<th>Price</th>
<th>Status</th>
</tr>
<tr>
<td>Laptop</td>
<td>50000</td>
<td>Available</td>
</tr>
<tr>
<td>Mobile</td>
<td>20000</td>
<td>Out of Stock</td>
</tr>
</table>
Modern HTML Table Practices
Modern websites generally style tables using CSS instead of old HTML attributes.
CSS improves:
Colors
Borders
Hover effects
Responsiveness
Mobile compatibility
Companies like Microsoft and Google use advanced styled tables in dashboards and analytics systems.
Responsive Tables
Responsive tables adjust according to screen size.
Important for:
Mobile devices
Tablets
Small screens
Modern responsive designs are usually implemented using CSS.
Common Mistakes Beginners Make
1. Forgetting Closing Tags
Wrong:
<td>Rahul
Correct:
<td>Rahul</td>
2. Unequal Number of Columns
Wrong:
<tr>
<td>Rahul</td>
</tr>
<tr>
<td>Aman</td>
<td>20</td>
</tr>
Rows should contain equal columns.
3. Using Tables for Webpage Layout
Old websites used tables for layouts.
Modern websites use:
CSS Flexbox
CSS Grid
instead of tables for page design.
Advantages of HTML Tables
Organizes data clearly
Improves readability
Easy comparison of information
Professional data presentation
Useful for reports and records
Real-Life Applications of Tables
Tables are used in:
School management systems
Banking applications
Railway reservation systems
E-commerce websites
Hospital management systems
Result processing systems
Platforms like Amazon and Flipkart use tables internally for product and order management.
Conclusion
HTML tables are essential for displaying structured data in webpages. They help organize information in rows and columns, making websites more professional and readable.
Comments
Post a Comment