HTML Tables
Tables are used to display structured data in rows and columns. In this chapter, you will learn how to create marksheets, timetables, fee charts and comparison tables.
table
Creates the main table structure.
tr
Creates a table row.
th
Creates a table heading cell.
td
Creates a table data cell.
After This Chapter, You Will Understand
This chapter teaches how to present data neatly using rows and columns.
Table Basics
Understand table, tr, th and td tags.
Table Sections
Use caption, thead, tbody and tfoot.
Merge Cells
Use colspan and rowspan correctly.
Projects
Create marksheet, timetable and fee chart.
Creating a Simple HTML Table
A table is created using table, tr, th and td tags.
1 Basic Table Syntax
The <table> tag creates the table, <tr> creates rows, <th> creates heading cells and <td> creates data cells.
<tr>
<th>Name</th>
<th>Course</th>
</tr>
<tr>
<td>Rahul</td>
<td>HTML</td>
</tr>
</table>
2 Output Example
| Name | Course |
|---|---|
| Rahul | HTML |
| Priya | CSS |
caption, thead, tbody and tfoot
Professional tables are easier to read when divided into proper sections.
3 Table Sections
- <caption> gives a title to the table.
- <thead> contains heading rows.
- <tbody> contains main data rows.
- <tfoot> contains summary/footer rows.
4 Marksheet Example
| Subject | Marks | Result |
|---|---|---|
| HTML | 85 | Pass |
| CSS | 90 | Pass |
| Project | 88 | Pass |
| Total | 263 | Excellent |
colspan and rowspan
These attributes are used to merge table cells across columns or rows.
5 colspan
The colspan attribute merges a cell across multiple columns.
| Subject | Marks |
|---|---|
| HTML | 80 |
| Total: 80 | |
6 rowspan
The rowspan attribute merges a cell across multiple rows.
| Batch | Subject |
|---|---|
| Morning | HTML |
| CSS |
Where Tables Are Used
Tables are powerful when data needs clear comparison or structure.
Marksheets
Subject-wise marks, total and result.
Timetables
Batch timings, subjects and trainers.
Fee Charts
Course fees, duration and services.
Comparisons
Plans, features and package comparison.
Create a Course Fee Table
Edit the code and click Run Code to see the output.
Mistakes Students Should Avoid
Correct table structure makes data easy to read and maintain.
Wrong Habits
- Using tables for complete page layout
- Forgetting tr around th or td
- Mixing rows and columns incorrectly
- Using colspan or rowspan unnecessarily
- Creating tables without headings
Correct Habits
- Use tables only for tabular data
- Use th for column headings
- Use caption for table title
- Use thead, tbody and tfoot for large tables
- Keep table data clear and simple
Test Your Understanding
Select the correct answers and check your score.
1. Which tag creates the main table?
2. Which tag creates a table row?
3. Which tag is used for table heading cell?
4. Which attribute merges columns?
5. Which tag gives a title to a table?
Remember These Points
Revise these before moving to Chapter 8.
table
The table tag creates the main table structure.
tr
The tr tag creates a table row.
th
The th tag creates a table heading cell.
td
The td tag creates a normal table data cell.
colspan
The colspan attribute merges columns.
rowspan
The rowspan attribute merges rows.