When creating tables in HTML, two key attributes control the spacing of table content and structure: cellpadding
and cellspacing
. These attributes are essential for organizing table layouts, improving readability, and enhancing visual clarity.
This guide explains both attributes separately, compares them, and introduces related table formatting concepts like colspan
, rowspan
, and valign
.
🔹 What is cellpadding
in HTML?
Cellpadding
defines the space inside a table cell—between the cell’s content and its border. Think of it as the internal padding of each cell.
When you apply cellpadding
, it ensures that content doesn’t stick directly to the edges of the table cell. This makes the table more readable and visually appealing.
Example Usage:
<table border="1" cellpadding="5">
<tr>
<th>Name</th>
<th>Subject</th>
</tr>
<tr>
<td>John</td>
<td>Math</td>
</tr>
</table>
Note: If your CSS specifies a different padding
for <td>
or <th>
elements, it will override the cellpadding
attribute in the HTML.
🔹 What is cellspacing
in HTML?
Cellspacing
defines the space between table cells. Unlike cellpadding
(which is internal), cellspacing
adds external spacing between adjacent cells—both vertically and horizontally.
This spacing gives the table a clearer, less cramped appearance.
Example Usage:
<table border="1" cellspacing="5">
<tr>
<th>Name</th>
<th>Subject</th>
</tr>
<tr>
<td>Jane</td>
<td>Science</td>
</tr>
</table>
🔄 Cellpadding vs Cellspacing – Key Differences

Feature | cellpadding | cellspacing |
---|---|---|
Defines space | Inside the cell | Between the cells |
Affects | Text alignment and readability | Overall spacing and cell separation |
Attribute tag | cellpadding="5" | cellspacing="5" |
Default value | 0 (can be set manually) | 2 in some browsers (can vary) |
Visual Effect | Content is spaced away from borders | Cells are separated with visible gaps |
Easy Analogy:
- Think of a cell as a box.
Cellpadding
= space inside the box (between content and the walls).Cellspacing
= space between the boxes (external gaps between cells).
🔧 Other Useful HTML Table Attributes

✅ colspan
Merges multiple columns into a single cell.
<td colspan="3">This spans 3 columns</td>
✅ rowspan
Extends a cell vertically across multiple rows.
<td rowspan="2">Covers 2 rows</td>
✅ valign
Aligns content vertically within a cell (top
, middle
, bottom
).
<td valign="top">Top aligned content</td>
🧪 Complete HTML Table Example with Cellpadding, Cellspacing, Colspan, Rowspan, and Valign
Students Data Table | |||
---|---|---|---|
Name | Roll# | Subject | Marks |
Ateeq | 33 | Physics | 22 |
Ali | 34 | Math | 28 |
Students older than 2011 | |||
Zara | 31 | Chemistry | 30 |
📌 Final Thoughts
Understanding how cellpadding
and cellspacing
work allows you to design cleaner, more user-friendly tables in HTML. Whether you’re building simple data tables or complex layouts, mastering these spacing tools is essential.
💡 Tip: While HTML attributes like
cellpadding
andcellspacing
still work in many browsers, modern developers often prefer using CSS (padding
andborder-spacing
) for more control and cleaner code.