Question
In an HTML table, cellpadding and cellspacing can be set directly in markup like this:
<table cellspacing="1" cellpadding="1">
How can the same table cell padding and spacing be achieved using CSS instead of HTML attributes?
Short Answer
By the end of this page, you will understand how old table attributes like cellpadding and cellspacing map to modern CSS. You will learn which CSS properties to use, how they behave, and how to style tables correctly in real projects.
Concept
HTML used to allow presentational table attributes such as cellpadding and cellspacing. These controlled:
cellpadding: the space inside each table cell, between the cell content and its bordercellspacing: the space between table cells
In modern web development, presentation should be handled with CSS, not HTML attributes.
The CSS replacements are:
- Use
paddingontdandthforcellpadding - Use
border-spacingontableforcellspacing
There is one important detail: border-spacing only works when the table uses:
table {
border-collapse: separate;
}
If a table uses border-collapse: collapse, the borders are merged together and has no effect.
Mental Model
Think of a table like a set of boxes arranged in a grid:
paddingis the cushion inside each boxborder-spacingis the gap between boxes
So:
- Want text to sit farther from the cell edge? Use padding.
- Want cells to sit farther apart from each other? Use border-spacing.
A useful way to remember it:
- Inside the cell →
padding - Between the cells →
border-spacing
Syntax and Examples
Basic CSS replacement
table {
border-collapse: separate;
border-spacing: 1px;
}
td, th {
padding: 1px;
}
This is the CSS equivalent of:
<table cellspacing="1" cellpadding="1">
Full example
<table class="demo-table">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Alice</td>
<td>24</td>
</>
Bob
31
Step by Step Execution
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;
}
What happens step by step
- The browser creates a table with one row and two cells.
- Each
tdgets a1pxborder. - Each
tdgetspadding: 6px.- This pushes the text away from the border inside the cell.
- So
Helloand have breathing room.
Real World Use Cases
Admin dashboards
Tables showing users, orders, or reports often need readable spacing. Padding makes data easier to scan, and border spacing can create a less cramped design.
Pricing or comparison tables
Cells often need generous internal spacing so headings and values do not feel crowded.
Email-like data layouts
Some internal tools still render tabular data. Controlled cell padding improves readability.
Export previews and reporting tools
Financial or analytics tables often need precise spacing for legibility.
CMS and legacy project modernization
Older HTML may still use cellpadding and cellspacing. Replacing them with CSS helps standardize styling and makes updates easier.
Real Codebase Usage
In real projects, developers usually avoid styling all tables globally unless the project is very small. Instead, they target a specific class:
.data-table {
border-collapse: separate;
border-spacing: 0;
}
.data-table th,
.data-table td {
padding: 0.75rem;
}
Common patterns include:
- Scoped table classes: prevent accidental styling of every table on the site
- Design-system spacing tokens: use consistent values like
0.5rem,1rem, or CSS variables - Responsive tables: combine padding with
overflow-x: autoon small screens - Header styling: use larger padding in
ththantd - Guarding layout rules: use
border-collapse: collapsefor tight grid tables, orseparatewhen visual spacing is needed
Example with CSS variables:
:root {
--table-cell-padding: ;
: ;
}
{
: separate;
: (--table-cell-gap);
}
,
{
: (--table-cell-padding);
}
Common Mistakes
1. Using border-spacing with border-collapse: collapse
Broken example:
table {
border-collapse: collapse;
border-spacing: 10px;
}
Why it fails:
border-spacingdoes not work 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;
}
Why it fails:
- This adds space around the table itself, not inside each cell.
Fix:
td, th {
: ;
}
Comparisons
cellpadding vs padding, cellspacing vs border-spacing
| Old HTML attribute | Modern CSS replacement | Applies to | Purpose |
|---|---|---|---|
cellpadding | padding | td, th | Space inside each cell |
cellspacing | border-spacing | table | Space between cells |
border-spacing vs padding
Cheat Sheet
Quick mapping
<table cellspacing="1" cellpadding="1">
becomes:
table {
border-collapse: separate;
border-spacing: 1px;
}
td, th {
padding: 1px;
}
Rules to remember
cellpadding→paddingontdandthcellspacing→border-spacingontableborder-spacingonly works withborder-collapse: separatemargindoes not create spacing between table cells- Use table classes to avoid changing every table globally
Common syntax
FAQ
How do I replace cellpadding in CSS?
Use padding on td and th.
td, th {
padding: 8px;
}
How do I replace cellspacing in CSS?
Use border-spacing on the table, and make sure border-collapse is set to separate.
table {
border-collapse: separate;
border-spacing: 8px;
}
Why is border-spacing not working?
The most common reason is that the table has border-collapse: collapse;. Change it to separate.
Can I use different spacing horizontally and vertically?
Yes.
Mini Project
Description
Build a small styled data table for a student grade report. This project demonstrates how to replace old HTML table attributes with modern CSS for inner cell spacing and spacing between cells.
Goal
Create a readable HTML table that uses CSS padding and border-spacing instead of cellpadding and cellspacing attributes.
Requirements
- Create an HTML table with at least three rows of student data
- Add header cells for the column names
- Use CSS
paddingon table cells to create inner spacing - Use CSS
border-spacingon the table to create space between cells - Ensure the table uses
border-collapse: separate
Keep learning
Related questions
Can You Style Half a Character in CSS? Text Effects with CSS and JavaScript
Learn how to style half of a character using CSS and JavaScript, including overlay techniques for dynamic text effects.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers.
Get the Selected Radio Button Value with jQuery
Learn how to find which radio button is selected in jQuery and get its value with simple examples and common mistakes.