HTML cellspacing Attribute

Beginner
⏱️ 4 min read
📚 Updated: Jun 2026
🎯 2 Examples
Tables & Layout

Introduction

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.

What You’ll Learn

01

Pixels

5, 10, 0.

02

On table

Whole table.

03

vs padding

Between cells.

04

border-spacing

CSS way.

05

cellSpacing

DOM property.

06

Deprecated

Use CSS.

Purpose of cellspacing Attribute

The 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 vs cellpadding

cellspacing adds space between cells. cellpadding adds space inside each cell between content and the border.

📝 Syntax

Add cellspacing to the <table> element with a pixel value:

cellspacing.html
<table cellspacing="10" border="1">
  <tr><td>Cell</td></tr>
</table>

Syntax Rules

  • Set on the <table> element only.
  • Value is a non-negative integer representing pixels between cells.
  • cellspacing="0" removes gaps so cells appear adjacent.
  • For modern layouts, use CSS border-spacing on the table.

💎 Values

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.

cellspacing-values.html
<table cellspacing="0">...</table>
<table cellspacing="10">...</table>

<!-- CSS alternative -->
<style>
  table { border-spacing: 10px; }
</style>

⚡ Quick Reference

AttributeControlsCSS Alternative
cellspacing="10"Gap between cellsborder-spacing: 10px;
cellpadding="10"Padding inside cellstd, th { padding: 10px; }
cellspacing="0"No gap between cellsborder-spacing: 0;
Element<table> onlyStyle table directly
UnitPixels (integer)Any CSS length with border-spacing
HTML5 statusDeprecatedUse CSS for new code

Applicable Elements

ElementSupported?Notes
<table>Yes (legacy)Applies spacing to all cells
<td>, <th>NoSet on table or use CSS
<tr>NoNot a valid target
<div>NoNot a table attribute

Examples Gallery

Implementation example with cellspacing and dynamic JavaScript cellSpacing.

👀 Live Preview

Table with 10 pixels of space between cells:

Row 1, Cell 1Row 1, Cell 2
Row 2, Cell 1Row 2, Cell 2

Implementation Example

Let’s see how the cellspacing attribute can be used in an HTML table:

index.html
<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>
Try It Yourself

How It Works

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.

Dynamic Values with JavaScript

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:

index.html
<table id="dynamicTable">...</table>

<script>
  // Dynamically set cellspacing for a table
  document.getElementById("dynamicTable").cellSpacing = "5";
</script>
Try It Yourself

How It Works

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.

♿ Accessibility

  • Spacing aids scanning — Moderate gaps between cells can make dense tables easier to read.
  • Do not rely on spacing alone — Use headers, captions, and semantic structure for accessibility.
  • Avoid excessive gaps — Large cellspacing values can push content off small screens.
  • Use responsive CSSborder-spacing adapts better than fixed HTML attributes.
  • Maintain contrast — Cell gaps show the page background; ensure borders and text remain readable.

🧠 How cellspacing Works

1

Author sets cellspacing

Add a pixel value on the <table> element.

Markup
2

Browser separates cells

A gap is inserted between adjacent cells horizontally and vertically.

Layout
3

Background shows through

The page or table background color appears in the gaps.

Visual
=

Spaced-out table grid

In modern projects, CSS border-spacing replaces cellspacing.

Browser Support

The cellspacing attribute is deprecated in HTML5 but still rendered by browsers on table elements.

⚠️ HTML4 legacy · Deprecated

Still renders in browsers

Use CSS border-spacing for new table layouts.

Legacy Rendering support
Google Chrome Legacy rendering
Deprecated
Mozilla Firefox Legacy rendering
Deprecated
Apple Safari Legacy rendering
Deprecated
Microsoft Edge Legacy rendering
Deprecated
cellspacing attribute Deprecated

Bottom line: Browsers still honor cellspacing on tables, but CSS border-spacing is the standard for new code.

💡 Best Practices

✅ Do

  • Use CSS border-spacing for new tables
  • Combine with border-collapse: separate when using CSS
  • Test spacing values for readability on mobile
  • Distinguish cellspacing from cellpadding
  • Keep gaps proportional to table content

❌ Don’t

  • Set cellspacing on individual cells (not supported)
  • Use excessive spacing that wastes page width
  • Confuse cellspacing with cellpadding
  • Expect cellspacing to work with border-collapse: collapse in CSS
  • Rely on HTML attributes for responsive table design
  • Use cellspacing judiciously to enhance the visual presentation of tables without cluttering the layout with excessive space.
  • Test the appearance of tables with different cellspacing values to find the optimal spacing for your design.
  • Remember that excessive spacing may lead to tables occupying more space on the page, so balance aesthetics with functionality.

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about cellspacing

Bookmark these before formatting HTML tables.

5
Core concepts
📝 02

On table

Not on td.

Scope
03

vs padding

Different attr.

Compare
⚙️ 04

cellSpacing

DOM property.

JS
⚠️ 05

Deprecated

border-spacing.

CSS

❓ Frequently Asked Questions

It adds pixel spacing between adjacent table cells horizontally and vertically.
On the <table> element only.
cellspacing is the gap between cells. cellpadding is space inside each cell.
It is deprecated. Use CSS border-spacing on the table for new projects.
Assign tableElement.cellSpacing = "5". Note the capital S in the DOM property.
table { border-spacing: 10px; border-collapse: separate; }

Control gaps between table cells

Practice the legacy cellspacing attribute and compare it with CSS border-spacing in the Try It editor.

Try table example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful