👀 Live Preview
Table with 10 pixels of space between cells:
| Row 1, Cell 1 | Row 1, Cell 2 |
| Row 2, Cell 1 | Row 2, Cell 2 |

The cellspacing attribute is an attribute in HTML used within the <table> element to define the spacing between cells in a table. It allows developers to control the amount of space between adjacent cells both vertically and horizontally.
5, 10, 0.
Whole table.
Between cells.
CSS way.
DOM property.
Use CSS.
cellspacing AttributeThe primary purpose of the cellspacing attribute is to adjust the visual presentation of tables by specifying the amount of space between table cells. This can help improve readability and aesthetics of the table layout.
cellspacing adds space between cells. cellpadding adds space inside each cell between content and the border.
Add cellspacing to the <table> element with a pixel value:
<table cellspacing="10" border="1">
<tr><td>Cell</td></tr>
</table><table> element only.cellspacing="0" removes gaps so cells appear adjacent.border-spacing on the table.The cellspacing attribute accepts numerical values, typically in pixels, to determine the amount of space between cells. Common values include integers representing the number of pixels for the desired spacing. A value of 0 (zero) removes any spacing between cells, effectively making them appear adjacent to each other.
<table cellspacing="0">...</table>
<table cellspacing="10">...</table>
<!-- CSS alternative -->
<style>
table { border-spacing: 10px; }
</style>| Attribute | Controls | CSS Alternative |
|---|---|---|
cellspacing="10" | Gap between cells | border-spacing: 10px; |
cellpadding="10" | Padding inside cells | td, th { padding: 10px; } |
cellspacing="0" | No gap between cells | border-spacing: 0; |
| Element | <table> only | Style table directly |
| Unit | Pixels (integer) | Any CSS length with border-spacing |
| HTML5 status | Deprecated | Use CSS for new code |
| Element | Supported? | Notes |
|---|---|---|
<table> | Yes (legacy) | Applies spacing to all cells |
<td>, <th> | No | Set on table or use CSS |
<tr> | No | Not a valid target |
<div> | No | Not a table attribute |
Implementation example with cellspacing and dynamic JavaScript cellSpacing.
Table with 10 pixels of space between cells:
| Row 1, Cell 1 | Row 1, Cell 2 |
| Row 2, Cell 1 | Row 2, Cell 2 |
Let’s see how the cellspacing attribute can be used in an HTML table:
<table cellspacing="10">
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>In this example, the cellspacing attribute is set to 10, which means there will be 10 pixels of space between each cell in the table.
You can also dynamically adjust the cellspacing attribute using JavaScript. This can be useful when you want to change the spacing based on certain conditions or user interactions. Here’s a simple example:
<table id="dynamicTable">...</table>
<script>
// Dynamically set cellspacing for a table
document.getElementById("dynamicTable").cellSpacing = "5";
</script>In this script, the cellSpacing property of a table with the id dynamicTable is set to 5, effectively changing the spacing between cells to 5 pixels.
border-spacing adapts better than fixed HTML attributes.Add a pixel value on the <table> element.
A gap is inserted between adjacent cells horizontally and vertically.
The page or table background color appears in the gaps.
In modern projects, CSS border-spacing replaces cellspacing.
The cellspacing attribute is deprecated in HTML5 but still rendered by browsers on table elements.
Use CSS border-spacing for new table layouts.
Bottom line: Browsers still honor cellspacing on tables, but CSS border-spacing is the standard for new code.
border-spacing for new tablesborder-collapse: separate when using CSSborder-collapse: collapse in CSSThe cellspacing attribute is a useful tool for adjusting the spacing between cells in HTML tables. By understanding its purpose and values, you can effectively control the layout and presentation of tabular data on your web pages.
For modern development, CSS border-spacing on the table element provides the same control with better flexibility. Learn cellspacing for legacy markup, then prefer CSS in new projects.
cellspacingBookmark these before formatting HTML tables.
Gap spacing.
PurposeNot on td.
ScopeDifferent attr.
CompareDOM property.
JSborder-spacing.
CSS<table> element only.cellspacing is the gap between cells. cellpadding is space inside each cell.border-spacing on the table for new projects.tableElement.cellSpacing = "5". Note the capital S in the DOM property.table { border-spacing: 10px; border-collapse: separate; }Practice the legacy cellspacing attribute and compare it with CSS border-spacing in the Try It editor.
5 people found this page helpful