HTML cellpadding Attribute

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

Introduction

In HTML, the cellpadding attribute is used within table elements to control the amount of space between the content of a cell and its border. This attribute allows developers to adjust the padding of individual cells within a table, enhancing the visual presentation and spacing of tabular data.

What You’ll Learn

01

Pixels

10, 15, 0.

02

On table

Whole table.

03

vs spacing

Inside cells.

04

CSS padding

Modern way.

05

cellPadding

DOM property.

06

Deprecated

Use CSS.

Purpose of cellpadding Attribute

The primary purpose of the cellpadding attribute is to specify the amount of space, in pixels, between the content of a table cell and its border. By default, most browsers apply some level of padding to table cells for better readability. However, the cellpadding attribute provides developers with the ability to customize this padding according to their design requirements.

💡
cellpadding vs cellspacing

cellpadding adds space inside each cell. cellspacing adds space between cells. They control different parts of table layout.

📝 Syntax

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

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

Syntax Rules

  • Set on the <table> element only — not on individual td or th cells.
  • Value is a non-negative integer representing pixels of internal cell padding.
  • cellpadding="0" removes internal padding from all cells.
  • For per-cell control, use CSS padding on td and th instead.

💎 Values

The cellpadding attribute accepts a numerical value representing the desired amount of padding in pixels. The higher the value, the more space will be added between the content of the cell and its border. A value of 0 (zero) indicates no padding, while larger values increase the padding proportionally.

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

<!-- CSS alternative -->
<style>
  td, th { padding: 10px; }
</style>

⚡ Quick Reference

AttributeControlsCSS Alternative
cellpadding="10"Padding inside each celltd, th { padding: 10px; }
cellspacing="5"Gap between cellsborder-spacing: 5px;
cellpadding="0"No internal paddingpadding: 0;
Element<table> onlyStyle cells directly
UnitPixels (integer)Any CSS length unit
HTML5 statusDeprecatedUse CSS for new code

Applicable Elements

ElementSupported?Notes
<table>Yes (legacy)Applies to all cells in the table
<td>, <th>NoUse CSS padding on cells instead
<tr>NoSet on table or use CSS
<div>NoNot a table attribute

Examples Gallery

Implementation example with cellpadding and dynamic JavaScript cellPadding.

👀 Live Preview

Table with 10 pixels of cell padding:

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

Implementation Example

Let’s look at a simple example of how to use the cellpadding attribute within an HTML table:

index.html
<table cellpadding="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 cellpadding attribute is set to 10, which means each cell in the table will have a padding of 10 pixels between its content and its border.

Dynamic Values with JavaScript

While the cellpadding attribute is typically set statically within the HTML markup, you can also dynamically adjust it using JavaScript if needed. Here’s a basic example:

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

<script>
  // Dynamically set cellpadding for a table
  var table = document.getElementById("dynamicTable");
  table.cellPadding = 15;
</script>
Try It Yourself

How It Works

In this script, the cellPadding property of a table element with the id dynamicTable is set to 15 pixels. This can be useful if you need to modify the padding of table cells based on user interactions or other dynamic conditions.

♿ Accessibility

  • Adequate padding helps readability — Enough space around cell text makes tables easier to scan.
  • Do not rely on spacing alone — Use proper <th> headers and <caption> for structure.
  • Avoid excessive padding — Very large values can break layouts on small screens.
  • Use responsive CSS — CSS padding can adapt with media queries better than fixed HTML attributes.
  • Maintain contrast — Padding does not replace readable text and background contrast.

🧠 How cellpadding Works

1

Author sets cellpadding

Add a pixel value on the <table> element.

Markup
2

Browser applies to all cells

Every td and th in the table gets the same internal padding.

Table
3

Content moves inward

Text and data sit farther from the cell border edge.

Layout
=

More readable table cells

In modern projects, CSS padding on cells offers finer control.

Browser Support

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

⚠️ HTML4 legacy · Deprecated

Still renders in browsers

Use CSS padding on td and th for new tables.

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

Bottom line: Browsers still honor cellpadding on tables, but CSS padding is the standard for new code.

💡 Best Practices

✅ Do

  • Use CSS padding on td and th for new tables
  • Keep padding consistent across similar tables
  • Pair spacing with clear headers and captions
  • Test tables on mobile viewports
  • Learn the difference from cellspacing

❌ Don’t

  • Set cellpadding on individual cells (not supported)
  • Use excessive padding that breaks narrow layouts
  • Confuse cellpadding with cellspacing
  • Rely on HTML attributes alone for responsive design
  • Forget border-collapse when styling with CSS
  • Use the cellpadding attribute to control the spacing between the content of table cells and their borders, ensuring optimal readability and visual presentation of tabular data.
  • Avoid excessive padding values, as they can negatively impact the overall layout and aesthetics of the table.
  • Test your tables across different browsers to ensure consistent rendering and spacing, as browser behavior may vary.

Conclusion

The cellpadding attribute is a valuable tool for customizing the spacing between the content and borders of table cells in HTML. By understanding how to use this attribute effectively, you can create well-designed tables that effectively communicate information to users.

For modern development, CSS padding on table cells gives you per-cell control and works better with responsive layouts. Learn cellpadding for legacy markup, then prefer CSS in new projects.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about cellpadding

Bookmark these before formatting HTML tables.

5
Core concepts
📝 02

On table

Not on td.

Scope
03

vs spacing

Different attr.

Compare
⚙️ 04

cellPadding

DOM property.

JS
⚠️ 05

Deprecated

Use CSS.

HTML5

❓ Frequently Asked Questions

It adds pixel padding between cell content and the cell border for every cell in a table.
On the <table> element only — not on individual td or th tags.
cellpadding is space inside cells. cellspacing is the gap between adjacent cells.
It is deprecated. Use CSS padding on table cells for new projects.
Assign tableElement.cellPadding = 15. Note the capital P in the DOM property.
td, th { padding: 10px; } gives you the same effect with more flexibility.

Format tables with better spacing

Practice the legacy cellpadding attribute and compare it with CSS padding 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