HTML Table Padding & Spacing
HTML tables can adjust the padding inside each cell and the space between cells.
HTML Table - Cell Padding
Cell padding is the space between a cell's edges and its content. By default it is 0. Add padding with the CSS padding property. You can also set each side on its own with padding-top, padding-bottom, padding-left, and padding-right.
All sides
15 pixels of padding on every side of the cells.
Prompt
A bordered table with 15px of padding inside all cells using the CSS padding property on th and td, with a sample company table.
Output: complete HTML page
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
table, th, td {
border: 1px solid black;
}
th, td {
padding: 15px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
</body>
</html>
Each side
Different padding on the top, bottom, left, and right.
Prompt
A bordered table that sets padding individually on each side of the cells: 10px top, 20px bottom, 30px left, 40px right, with a sample table.
Output: complete HTML page
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
table, th, td {
border: 1px solid black;
}
th, td {
padding-top: 10px;
padding-bottom: 20px;
padding-left: 30px;
padding-right: 40px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
</body>
</html>
HTML Table - Cell Spacing
Cell spacing is the space between cells. By default it is 2 pixels. Change it with the CSS border-spacing property on the table element.
Example
30 pixels of space between cells.
Prompt
A bordered table with 30px of space between cells using the CSS border-spacing property on the table element, with a sample table.
Output: complete HTML page
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
table, th, td {
border: 1px solid black;
}
table {
border-spacing: 30px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
</body>
</html>