🍽️An HTML table is used to organize data into rows and columns — just like a spreadsheet. Think of it as the OG data grid before Excel got cool.
It’s like a mini spreadsheet built with code — perfect for showing things like results, schedules, price lists, or rankings.
🔍 In plain words:
It organizes information neatly, so instead of this:
Name: X, Subject: Physics, Marks: 95
Name: Y, Subject: Chemistry, Marks: 89
You get this:
| Name |
Subject |
Marks |
| X |
Physics |
95 |
| Y |
Chemistry |
89 |
🧩 Breakdown:
| Tag |
Meaning |
Description |
| <table> |
Table |
The container that holds all rows and cells |
| <tr> |
Table Row |
Defines a single row |
| <th> |
Table Header |
Defines a header cell (bold + centered by default) |
| <td> |
Table Data |
Defines a normal data cell |
🪄 Optional (but useful) attributes:
| Attribute |
Description |
Example |
| border |
Adds border |
<table border="1"></table> |
| cellpadding |
Adds padding inside cells |
<table cellpadding="10"></table> |
| cellspacing |
Adds space between cells |
<table cellspacing="5"></table> |
| colspan |
Merge column |
<td colspan="2"> |
| rowspan |
Merge row |
<td rowspan="2"> |
Description of these attributes:
Cellspacing:
Cellspacing is an HTML attribute used in the <table> tag. It controls the space between table cells — basically the little gaps between each box in the table grid.
🧱 Think of it like this:
i.cellspacing = space outside each cell
ii.cellpadding = space inside each cell
🧪 Example:
<table border="1" cellspacing="10">
<tr>
<th>Name</th>
<th>Subject</th>
</tr>
<tr>
<td>Fahim</td>
<td>Math</td>
</tr>
</table>
Output:
, but it’s all about the space inside each cell — like the cell’s personal bubble. 😄
<table border="1" cellpadding="10">
<tr>
<th>Name</th>
<th>Subject</th>
</tr>
<tr>
<td>Fahim</td>
<td>Math</td>
</tr>
</table>
Output:
| Name |
Subject |
| Fahim |
Physics |
Here, cellpadding="15" adds 15 pixels of padding inside each cell, making the content look less cramped and more readable.
Colspan & Rowspan:
When you create a table, every <tr> (table row) normally has the same number of <td> or <th> cells — that’s how the grid stays aligned.
But sometimes, you want one cell to stretch across more than one column or row — that’s where colspan and rowspan come in.
Colspan-(Merge columns-left to right)
Definition:
The colspan attribute tells the browser:
“Hey, this cell should take up more than one column.”
Example:
<table border="1" cellpadding="10">
<tr>
<th colspan="3">Student Report</th>
</tr>
<tr>
<th>Name</th>
<th>Subject</th>
<th>Marks</th>
</tr>
<tr>
<td>Fahim</td>
<td>Math</td>
<td>95</td>
</tr>
</table>
Output:
| Student Report |
| Name |
Subject |
Marks |
| Fahim |
Math |
95 |
🧠 Explanation:
colspan="3" merges 3 columns (Name + Subject + Marks) into a single header cell called “Student Report”.
So that one cell now covers 3 cells’ worth of horizontal space.
Rowspan-(merge rows=>top to bottom)
Rowspan is an attribute you use inside a table cell (<td> or <th>) to make that cell stretch vertically — across multiple rows.
In short:
rowspan = how many rows this one cell will cover.
<table border="1" cellpadding="10">
<tr>
<th rowspan="2">Name</th>
<th>Subject</th>
<th>Marks</th>
</tr>
<tr>
<th>Math</th>
<th>95</th>
</tr>
</table>
Output:
| Name |
Subject |
Marks |
| Math |
95 |
🧠 Explanation:
rowspan="2" makes the Name cell cover two rows vertically.
So the “Name” header aligns with both “Subject” and “Marks”.
Colspan and rowspan both works on &td> &<th> tag.