👀 Live Preview
Table with 10 pixels of cell padding:
| Row 1, Cell 1 | Row 1, Cell 2 |
| Row 2, Cell 1 | Row 2, Cell 2 |

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.
10, 15, 0.
Whole table.
Inside cells.
Modern way.
DOM property.
Use CSS.
cellpadding AttributeThe 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 adds space inside each cell. cellspacing adds space between cells. They control different parts of table layout.
Add cellpadding to the <table> element with a pixel value:
<table cellpadding="10" border="1">
<tr><td>Cell content</td></tr>
</table><table> element only — not on individual td or th cells.cellpadding="0" removes internal padding from all cells.padding on td and th instead.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.
<table cellpadding="0">...</table>
<table cellpadding="10">...</table>
<table cellpadding="20">...</table>
<!-- CSS alternative -->
<style>
td, th { padding: 10px; }
</style>| Attribute | Controls | CSS Alternative |
|---|---|---|
cellpadding="10" | Padding inside each cell | td, th { padding: 10px; } |
cellspacing="5" | Gap between cells | border-spacing: 5px; |
cellpadding="0" | No internal padding | padding: 0; |
| Element | <table> only | Style cells directly |
| Unit | Pixels (integer) | Any CSS length unit |
| HTML5 status | Deprecated | Use CSS for new code |
| Element | Supported? | Notes |
|---|---|---|
<table> | Yes (legacy) | Applies to all cells in the table |
<td>, <th> | No | Use CSS padding on cells instead |
<tr> | No | Set on table or use CSS |
<div> | No | Not a table attribute |
Implementation example with cellpadding and dynamic JavaScript cellPadding.
Table with 10 pixels of cell padding:
| Row 1, Cell 1 | Row 1, Cell 2 |
| Row 2, Cell 1 | Row 2, Cell 2 |
Let’s look at a simple example of how to use the cellpadding attribute within an HTML table:
<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>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.
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:
<table id="dynamicTable">...</table>
<script>
// Dynamically set cellpadding for a table
var table = document.getElementById("dynamicTable");
table.cellPadding = 15;
</script>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.
<th> headers and <caption> for structure.Add a pixel value on the <table> element.
Every td and th in the table gets the same internal padding.
Text and data sit farther from the cell border edge.
In modern projects, CSS padding on cells offers finer control.
The cellpadding attribute is deprecated in HTML5 but still rendered by browsers on table elements.
Use CSS padding on td and th for new tables.
Bottom line: Browsers still honor cellpadding on tables, but CSS padding is the standard for new code.
padding on td and th for new tablescellspacingborder-collapse when styling with CSScellpadding attribute to control the spacing between the content of table cells and their borders, ensuring optimal readability and visual presentation of tabular data.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.
cellpaddingBookmark these before formatting HTML tables.
Content gap.
PurposeNot on td.
ScopeDifferent attr.
CompareDOM property.
JSUse CSS.
HTML5<table> element only — not on individual td or th tags.cellpadding is space inside cells. cellspacing is the gap between adjacent cells.padding on table cells for new projects.tableElement.cellPadding = 15. Note the capital P in the DOM property.td, th { padding: 10px; } gives you the same effect with more flexibility.Practice the legacy cellpadding attribute and compare it with CSS padding in the Try It editor.
5 people found this page helpful