Set cellpadding and cellspacing in CSS?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In modern HTML and CSS, you do not use the old cellpadding and cellspacing attributes to control table spacing. The CSS replacements are padding on the table cells and border-spacing on the table, with one important limitation: border-spacing works only when the table uses separate borders.
Replace cellpadding With Cell padding
The old HTML cellpadding attribute controlled the space inside each cell. In CSS, that is just the normal padding property on td and th.
That means the text gets breathing room inside the cell border. If you only want horizontal or vertical space, use directional padding values instead of one shorthand value.
Replace cellspacing With border-spacing
The old cellspacing attribute controlled the gap between neighboring table cells. The CSS equivalent is border-spacing on the table itself.
You can also give separate horizontal and vertical spacing values:
The first number is horizontal spacing. The second is vertical spacing.
Why border-collapse Matters
This is the detail that causes most confusion: border-spacing has no visible effect when border-collapse: collapse is in use.
That rule does not create gaps, because the collapsed-border layout model removes the separate-cell spacing behavior.
So the real decision is:
- use
border-collapse: separateif you want gaps between cells - use
border-collapse: collapseif you want a tight grid with shared borders
Once that is clear, the rest of the styling becomes much easier.
Example Using Both Concepts Together
A realistic table usually needs both kinds of spacing: internal padding and external cell gaps.
In that example:
- '
paddingadds space inside each cell' - '
border-spacingadds space between cells'
They do different jobs, so it is normal to use both.
Modern CSS Is More Flexible Than Old HTML Attributes
The old attributes applied table-wide and were limited. CSS lets you target exactly what you want. For example, you can give header cells extra padding while keeping body cells compact.
That is one reason the HTML attributes became obsolete. CSS is not just the modern replacement. It is the more capable tool.
Know When To Use Tables At All
One more practical point: table spacing rules only apply when you are actually using semantic HTML tables for tabular data. Developers sometimes try to recreate layout grids with tables and then fight padding and border-spacing to achieve page-level alignment.
If the content is page layout rather than tabular data, CSS Grid or Flexbox is usually the better tool. Use table spacing rules when you really are styling a table, not when the table is standing in for a layout system.
Common Pitfalls
A frequent mistake is expecting border-spacing to work while border-collapse: collapse is active. It will not.
Another issue is confusing inside space with between-cell space. padding affects content inside a cell, while border-spacing affects the gap between cells.
Developers also sometimes put padding on the table element and expect that to behave like cellpadding. It does not. The correct target is the cells themselves.
Summary
- Use
paddingontdandthinstead of oldcellpadding. - Use
border-spacingon thetableinstead of oldcellspacing. - '
border-spacingworks only withborder-collapse: separate.' - Padding controls internal cell space; border spacing controls gaps between cells.
- CSS gives you much finer control than deprecated HTML table attributes.

