Question
In an HTML table, cellpadding and cellspacing can be set directly in the markup like this:
<table cellspacing="1" cellpadding="1">
How can the same table cell padding and spacing behavior be achieved using CSS instead of HTML attributes?
Short Answer
By the end of this page, you will understand how old HTML table attributes like cellpadding and cellspacing map to modern CSS. You will learn that padding on td/th replaces cellpadding, and border-spacing on the table replaces cellspacing when border-collapse: separate is used.
Concept
HTML once allowed presentational table attributes such as cellpadding and cellspacing, but modern HTML and CSS separate structure from presentation.
cellpaddingcontrols the space inside each table cell.cellspacingcontrols the space between table cells.
In CSS, these are handled with different properties:
- Use
paddingontdandthfor inner spacing. - Use
border-spacingon thetablefor spacing between cells.
This matters because CSS gives you:
- cleaner HTML
- easier styling across many tables
- better maintainability
- more control over layout
A key detail is that border-spacing only works when the table uses the default separated border model, or explicitly:
table {
border-collapse: separate;
}
If you use border-collapse: collapse;, the space between cells disappears, and will not behave like .
Mental Model
Think of a table cell like a box:
- Padding is the space between the content and the inside edge of the box.
- Border spacing is the gap between one box and the next box.
So:
cellpadding= space inside each boxcellspacing= space between boxes
If the boxes are merged tightly together with border-collapse: collapse, there is no gap between them, so border-spacing cannot create visible spacing in the same way.
Syntax and Examples
Basic CSS replacement
table {
border-collapse: separate;
border-spacing: 1px;
}
td,
th {
padding: 1px;
}
<table class="my-table">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Ana</td>
<td>28</td>
</tr>
</table>
.my-table {
border-collapse: separate;
border-spacing: 1px;
}
.my-table ,
{
: ;
}
Step by Step Execution
Consider this example:
<table class="report">
<tr>
<td>Hello</td>
<td>World</td>
</tr>
</table>
.report {
border-collapse: separate;
border-spacing: 10px;
}
.report td {
padding: 6px;
border: 1px solid black;
}
Step by step
- The browser creates a table with one row and two cells.
border-collapse: separate;tells the browser to keep cell borders separate rather than merging them.border-spacing: 10px;adds a 10-pixel gap between the two cells.- Each
tdgetspadding: 6px;, so the text inside each cell is pushed 6 pixels away from the cell border.
Real World Use Cases
CSS table spacing is commonly used in situations like these:
- Admin dashboards: adding readable spacing to data tables.
- Reports and invoices: controlling how tightly rows and columns appear.
- Email templates: although older HTML attributes still appear in email work, modern web pages should prefer CSS.
- Comparison tables: improving readability with padded cells.
- Data-heavy apps: making tables easier to scan without changing the HTML structure.
For example, a product table might use:
paddingto give text room inside each cellborder-spacingto visually separate recordsborder-collapse: collapseinstead when a compact grid look is desired
Real Codebase Usage
In real projects, developers usually do not style every table globally. Instead, they target specific classes:
.user-table {
border-collapse: separate;
border-spacing: 0 8px;
}
.user-table td,
.user-table th {
padding: 12px;
}
Common patterns include:
- Scoped table classes so one table style does not affect all tables
- Utility classes like
.p-2or.table-compact - Guarding layout choices by deciding early whether the table should use
collapseorseparate - Theme-based spacing where padding values come from a design system
Developers also often choose between two styles:
- Compact grid
border-collapse: collapse;- little or no spacing between cells
- Card-like rows
border-collapse: separate;- visible
border-spacing
Common Mistakes
1. Using border-spacing with border-collapse: collapse
Broken example:
table {
border-collapse: collapse;
border-spacing: 10px;
}
Problem:
border-spacingdoes not work as expected when borders are collapsed.
Fix:
table {
border-collapse: separate;
border-spacing: 10px;
}
2. Applying padding to table instead of cells
Broken example:
table {
padding: 10px;
}
Problem:
- This adds space around the table itself, not inside each cell.
Fix:
td,
th {
: ;
}
Comparisons
| HTML attribute / CSS property | What it controls | Applies to | Notes |
|---|---|---|---|
cellpadding | Space inside each cell | Old HTML attribute on table | Replaced by CSS padding on td/th |
padding | Space inside an element | td, th, or other elements | Modern CSS approach |
cellspacing | Space between cells | Old HTML attribute on table | Replaced by CSS border-spacing |
Cheat Sheet
/* Replace cellpadding */
td,
th {
padding: 8px;
}
/* Replace cellspacing */
table {
border-collapse: separate;
border-spacing: 4px;
}
Rules to remember
cellpadding->paddingontdandthcellspacing->border-spacingontableborder-spacingneedsborder-collapse: separateborder-spacing: 4px 8px;means horizontal then vertical spacingpaddingaffects space inside cellsborder-spacingaffects space between cells
Common patterns
/* Compact grid */
{
: collapse;
}
,
{
: ;
}
FAQ
How do I set cellpadding in CSS?
Use padding on td and th elements.
td, th {
padding: 8px;
}
How do I set cellspacing in CSS?
Use border-spacing on the table.
table {
border-collapse: separate;
border-spacing: 4px;
}
Why is border-spacing not working?
Usually because the table has border-collapse: collapse;. Change it to separate.
Can one CSS property replace both cellpadding and cellspacing?
No. You usually need:
Mini Project
Description
Create a small styled HTML table for a product list using CSS instead of old HTML attributes. This demonstrates how to control space inside cells and space between cells in a clean, modern way.
Goal
Build a readable table that uses padding for inner cell space and border-spacing for gaps between cells.
Requirements
- Create a table with at least three product rows and a header row.
- Use CSS classes instead of
cellpaddingorcellspacingHTML attributes. - Add padding to both data cells and header cells.
- Add visible spacing between rows or cells using
border-spacing. - Make the table borders visible so the spacing effect is easy to see.
Keep learning
Related questions
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.
CSS Parent Selector Explained: Selecting a Parent <li> from an <a>
Learn whether CSS has a parent selector, how :has() works, and practical alternatives for styling a parent li from a child anchor.
CSS Previous Sibling Selector: What Exists and How to Work Around It
Learn whether CSS has a previous sibling selector, why it does not, and practical ways to style earlier elements using CSS alternatives.