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 border-spacing will not behave like cellspacing.
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 td,
.my-table th {
padding: 1px;
}
What this does
border-spacing: 1px;adds 1 pixel of space between neighboring cells.padding: 1px;adds 1 pixel of space inside each cell.
Different horizontal and vertical spacing
table {
border-collapse: separate;
border-spacing: 4px 8px;
}
This means:
4pxhorizontal spacing8pxvertical spacing
Common modern table styling
table {
border-collapse: separate;
border-spacing: 0;
width: 100%;
}
td,
th {
padding: 8px 12px;
border: 1px solid #ccc;
}
This setup removes gaps between cells while keeping comfortable padding inside them.
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. - The
border: 1px solid black;makes the cell boundaries visible.
Result
- The words
HelloandWorldare not touching the borders because ofpadding. - The two cells are not touching each other because of
border-spacing.
That is the CSS equivalent of using both cellpadding and cellspacing.
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 - more padding inside cells
So the exact CSS depends on whether the UI should look dense or spacious.
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 {
padding: 10px;
}
3. Expecting cellpadding and cellspacing behavior from one CSS property
There is no single CSS property that replaces both.
paddinghandles inner cell spaceborder-spacinghandles gaps between cells
4. Forgetting th elements
Broken example:
td {
padding: 8px;
}
Problem:
- Header cells may look different because
this not included.
Fix:
td,
th {
padding: 8px;
}
5. Styling every table on the site unintentionally
Risky example:
table {
border-spacing: 10px;
}
Problem:
- This may affect layout in places you did not intend.
Fix:
.orders-table {
border-spacing: 10px;
}
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 |
border-spacing | Gap between table cells | table | Works with border-collapse: separate |
border-collapse: collapse | Merges adjacent cell borders | table | Removes separated-cell spacing behavior |
border-collapse: separate vs collapse
| Mode | Effect | Best for |
|---|---|---|
separate | Cells keep their own borders and can have spacing between them | Tables with visible gaps |
collapse | Adjacent borders merge into one grid | Compact data tables |
Old HTML vs CSS
| Approach | Recommended? | Reason |
|---|---|---|
cellpadding / cellspacing attributes | No | Presentational HTML is outdated |
CSS padding / border-spacing | Yes | Cleaner and easier to maintain |
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 */
table {
border-collapse: collapse;
}
td,
th {
padding: 6px;
}
/* Spacious table */
table {
border-collapse: separate;
border-spacing: 0 8px;
}
td,
th {
padding: 12px;
}
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:
paddingfor inside the cellsborder-spacingfor between the cells
Should I still use cellpadding and cellspacing in HTML?
For modern web development, no. CSS is the recommended approach.
Does padding on table affect cell content?
No. It affects the table element itself, not the inside of each cell.
Can I set different horizontal and vertical cell spacing?
Yes.
table {
border-spacing: 6px 12px;
}
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
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
CSS :not() Selector for Elements Without a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common pitfalls.
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.