Welcome back! In the previous lesson, you learned how to create a basic table using <table>, <tr>, <th>, and <td>.
Now, imagine looking at your school's Weekly Class Timetable:
On Friday afternoon, Periods 4 and 5 are combined into one single, extended Double Robotics & Computer Lab session!
Every day at 12:00 PM, all classes pause for the Lunch Break. The lunch break doesn't need 6 separate little boxes; it stretches across the entire day as one big continuous banner!
In spreadsheet apps like Microsoft Excel or Google Sheets, this feature is called "Merge Cells".
In HTML, we don't have a "Merge" button. Instead, we have two powerful attributes:
1
colspan (Column Span): Merges cells horizontally across multiple columns (left to right ↔️).
2
rowspan (Row Span): Merges cells vertically down multiple rows (top to bottom ↕️).
Let's master how to use both without messing up your table layout!
1. The colspan Attribute (Horizontal Merging ↔️)
The word colspan stands for Column Span.
It tells the browser: "Hey, make this single cell stretch wider so it occupies the space of multiple columns!"
$$\text{colspan="number\_of\_columns"}$$
The Cardinal Rule of colspan:
If a normal row has 3 columns, and you tell one cell to take up 2 columns (colspan="2"), your row already has: $$2 + 1 = 3\text{ columns worth of space!}$$
Therefore, you must delete one <td> from that row! If you forget to delete it, that row will have an extra box sticking out of the table edge like a crooked brick!
Visualizing colspan:
1Row 1: [ Subject ] [ Max Marks ] [ Marks Scored ] <── 3 normal columns
2 ──────────────────────────────────────────────
3Row 2: [ Grand Total Marks ] [ 192 ] <── colspan="2" + 1 cell = 3!
15<!-- Second Row: Notice we DO NOT write the Subject cell here! -->
16<tr>
17<tdstyle="padding: 8px;">Practical Lab</td>
18<tdstyle="padding: 8px;">28 / 30</td>
19</tr>
20</table>
Look closely at the second <tr>. It only has two <td> elements! The first column is already claimed by <td rowspan="2">Science</td> from the row above.
3. Putting Both Together: School Timetable 🏆
Let's build a real school timetable that uses both colspan and rowspan!
colspan="4" for the universal Lunch Break that spans all periods.
rowspan="2" for a Double Practical Lab on Friday.
1<!DOCTYPEhtml>
2<htmllang="en">
3<head>
4<metacharset="UTF-8">
5<title>Class 10 Timetable</title>
6<style>
7table{
8width: 100%;
9border-collapse: collapse;
10font-family: Arial, sans-serif;
11text-align: center;
12}
13th, td{
14border: 1px solid #94A3B8;
15padding: 10px;
16}
17th{
18background-color: #0A2540;
19color: white;
20}
21.lunch{
22background-color: #FED7AA;
23font-weight: bold;
24letter-spacing: 2px;
25color: #9A3412;
26}
27.lab{
28background-color: #DBEAFE;
29font-weight: bold;
30color: #1E40AF;
31}
32</style>
33</head>
34<body>
35
36<h2>Weekly Class Timetable (Class 10th-B)</h2>
37
38<table>
39<tr>
40<th>Day</th>
41<th>Period 1</th>
42<th>Period 2</th>
43<th>Period 3</th>
44<th>Period 4</th>
45</tr>
46
47<!-- Monday Row -->
48<tr>
49<td><strong>Monday</strong></td>
50<td>Mathematics</td>
51<td>English</td>
52<td>Science</td>
53<td>Social Science</td>
54</tr>
55
56<!-- Tuesday Row -->
57<tr>
58<td><strong>Tuesday</strong></td>
59<td>Hindi</td>
60<td>Mathematics</td>
61<td>Computer Science</td>
62<td>Physical Education</td>
63</tr>
64
65<!-- Lunch Break Row: Stretches across all 4 periods using colspan="4"! -->
Tip 1: Zebra Striping (Easy-to-Read Alternate Rows)
When a table has 20 rows of student marks, it is easy for eyes to slip across lines. In CSS, you can color every even row with a soft background using :nth-child(even):
1/* Gives alternate rows a gentle light gray tint */
2tbody tr:nth-child(even){
3background-color: #F8FAFC;
4}
Tip 2: Hover Highlight
Make rows highlight when the user hovers their mouse:
1tbody tr:hover{
2background-color: #F1F5F9;
3cursor: pointer;
4}
Common Beginner Mistakes (And How to Avoid Them) ⚠️
1. ⚠️ The "Pushed-Out Box" Mistake (Forgetting to delete extra cells)
When you add colspan="2", you are adding width. You must remove one regular <td> from that row. Otherwise, the table will look broken with one cell hanging outside!
2. ⚠️ Putting colspan or rowspan on <tr>
Rows cannot span columns. Only individual cells (<th> or <td>) can have colspan or rowspan!
1<!-- ❌ WRONG: tr cannot have colspan! -->
2<trcolspan="3">...</tr>
3
4<!-- ✅ CORRECT: Place on th or td -->
5<tr>
6<tdcolspan="3">...</td>
7</tr>
3. ⚠️ Adding extra cells in rows under a rowspan
Remember that rowspan pushes down into the row below. If your table has 3 columns and Row 1 spans into Row 2, Row 2 only needs 2 cells!
Quick Summary
colspan merges cells horizontally across columns (left to right ↔️).
When using colspan="N", you must remove (N - 1) cells from that row so the row stays balanced.
rowspan merges cells vertically down rows (top to bottom ↕️).
When using rowspan="N", subsequent rows covered by the span must contain fewer cells.
colspan and rowspan can only be added to <th> or <td> tags (never to <tr>).
You can combine both in the same table to build complex schedules, invoices, and timetables.
Interactive Quiz• Knowledge Check
Practice Quiz
Test your understanding with these multiple-choice questions:
Question 1
Which attribute is used to merge two or more columns horizontally?
Question 2
If a table has 4 columns and you write <td colspan="3">, how many more <td> cells can you add to that row?
Question 3
Which attribute is used to make a cell stretch vertically down across multiple rows?
Question 4
On which HTML tag can you legally apply the colspan attribute?
Question 5
If a cell in Row 1 has rowspan="2", how many cells should you omit from the corresponding column in Row 2?
Hands-on Practice Challenge 🎯
Open VS Code and create a file named school-sports-schedule.html.
Your Challenge:
Create a "School Annual Sports Day Tournament Schedule":
1
Header Row: Create 4 columns: Time Slot, Court / Field, Sport Event, and Age Category.
2
Morning Session: Add two matches (e.g. Badminton Under-14 and Table Tennis Under-16).
3
Opening Ceremony Banner: Use <td colspan="4"> to create a bold opening ceremony banner across all 4 columns: "10:00 AM — Grand Torch Lighting & March Past by Chief Guest".
4
Cricket Match: A cricket match takes two time slots (11:00 AM - 01:00 PM). Use <td rowspan="2"> in the Sport Event column to show a 2-hour Double-Slot Cricket Final!
5
Test your file in your browser and make sure all borders align perfectly without any crooked cells!
Congratulations! You have completed Chapter 8: Tables! You can now organize complex numerical data, marks, and schedules like a professional web developer!
🚀 What's Next?
In the next lesson, we will continue your web development journey with Block vs Inline Elements (Block & Inline).