Introduction
HTML tables are used to display data in rows and columns. They are commonly used to present structured information such as schedules, reports, pricing tables, and comparison data.
Tables help organize information clearly, making it easier for users to read and understand data.
In this lesson, you will learn how to create tables in HTML and understand different table elements.
What You’ll Learn
- HTML
<table>tag - Table rows, columns, and cells
<tr>,<th>, and<td>tags- Table borders
- Spanning rows and columns
- Best practices for tables
🔹 Basic Table Structure
An HTML table is created using the <table> tag.
Syntax:
<table>
<tr>
<th>Heading</th>
<th>Heading</th>
</tr>
<tr>
<td>Data</td>
<td>Data</td>
</tr>
</table>
Explanation:
<table>→ Defines the table<tr>→ Table row<th>→ Table header cell<td>→ Table data cell
🔹 Example of HTML Table
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
<td>Delhi</td>
</tr>
<tr>
<td>Jane</td>
<td>22</td>
<td>Surat</td>
</tr>
</table>
Output:

A table with 3 columns (Name, Age and City) and multiple rows will be displayed.
🔹 Table Borders
You can add borders using the border attribute.
<table border="1">
<tr>
<th>Course</th>
<th>Duration</th>
<th>Fees</th>
</tr>
<tr>
<td>HTML</td>
<td>2 Weeks</td>
<td>Free</td>
</tr>
</table>
🔹 Table Head, Body & Footer:
HTML tables can be divided into three sections:<thead> for header, <tbody> for main data, and <tfoot> for footer.
This helps organize large tables and improves readability.
<table border="1">
<thead>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Laptop</td>
<td>₹50,000</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td>₹50,000</td>
</tr>
</tfoot>
</table>
The <th> tag is used for table headings. Text inside <th> is bold and centered by default.
- Text inside
<th>is bold and centered by default - Used to define column titles
The <tbody> tag contains the main data of the table.
- Holds all the actual rows (
<tr>) and data (<td>) - This is the largest part of the table
The <tfoot> tag is used for summary or footer content.
- Often used for totals, averages, or summary rows
- Appears at the bottom of the table
🔹 Colspan & Rowspan
Colspan Example:
The colspan attribute is used to merge columns.
<tr>
<td colspan="2">Total Students</td>
</tr>
👉 This cell will span across two columns.
Rowspan Example:
The rowspan attribute is used to merge rows.
<tr>
<td rowspan="2">HTML</td>
<td>Beginner</td>
</tr>
🔹 How Tables Appear in Browser
- Tables are displayed in rows and columns
- Borders appear if defined
- Headers are bold and centered
- Cells adjust based on content
- Table width adjusts based on content.
- Tables can be styled using CSS for better design.
🔹 Complete Example:
<!DOCTYPE html>
<html>
<head>
<title>HTML Tables Complete Example</title>
</head>
<body>
<h2>Student Report Table</h2>
<table border="1" cellpadding="10">
<caption>Student Marks Report</caption>
<thead>
<tr>
<th rowspan="2">Name</th>
<th colspan="2">Marks</th>
<th rowspan="2">Total</th>
</tr>
<tr>
<th>Math</th>
<th>Science</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>80</td>
<td>85</td>
<td>165</td>
</tr>
<tr>
<td>Jane</td>
<td>90</td>
<td>88</td>
<td>178</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">Total Marks</td>
<td>343</td>
</tr>
</tfoot>
</table>
</body>
</html>
Explanation:
<caption>→ Displays table title<thead>→ Contains table headings<tbody>→ Contains main data<tfoot>→ Shows summary (total marks)rowspan→ Merges rows (Name column)colspan→ Merges columns (Marks section)border&cellpadding→ Improve table appearance
👉 This example combines all major table concepts in one place.
🔹 Real-World Uses of Tables
- Student marksheets
- Pricing tables
- Timetables
- Admin dashboards
- Product comparison tables
🔹 Important Notes
- Always use
<th>for headings - Keep tables simple and readable
- Avoid using tables for page layout
- Use CSS for styling tables
- Use proper structure for accessibility
🔹 Common Mistakes
- Missing
<tr>tags - Using
<td>instead of<th>for headings - Incorrect colspan/rowspan usage
- Overusing tables for layout
⭐ Best Practices
✔ Use tables only for data, not layout
✔ Use <th> for headings
✔ Keep tables readable
✔ Later, style tables using CSS
🔹 Practice Tasks:
- Task 1: Create a table with 2 columns and 3 rows.
- Task 2: Add headings using
<th>. - Task 3: Use
colspanto merge two columns. - Task 4: Use
rowspanto merge rows. - Task 5: Add spacing using
cellpadding. - Task 6: Create a table with
<thead>,<tbody>, and<tfoot>.
FAQs
What is an HTML table?
An HTML table is used to display data in rows and columns.
What is the difference between <th> and <td>?
<th> is used for headings, while <td> is used for data cells.
Can tables be styled using CSS?
Yes, modern tables are styled using CSS.
Conclusion
HTML tables are useful for displaying structured data clearly. By using rows, columns, and table elements like <th> and <td>, you can organize information effectively.
In the next tutorial, you will learn more advanced HTML Forms to build interactive and structured web pages.
