HTML
HTML Table Padding & Spacing
HTML table padding and spacing enhance the readability and structure of tabular data. Proper implementation ensures a visually appealing and accessible presentation.
HTML Table - Cell Padding
Cell padding is the space between the content of a table cell and its
border. It improves readability by creating a buffer around the text
or other elements within a cell. To add padding, use the CSS padding
property on <th>
and <td>
elements. The default padding is 0
,
which can make tables look cramped.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 15px; /* Adds 15px padding inside each cell */
text-align: center;
}
</style>
</head>
<body>
<h1>Web Development Tools</h1>
<table>
<tr>
<th>Tool</th>
<th>Category</th>
<th>Rating</th>
</tr>
<tr>
<td>VS Code</td>
<td>Editor</td>
<td>5/5</td>
</tr>
<tr>
<td>Git</td>
<td>Version Control</td>
<td>5/5</td>
</tr>
</table>
</body>
</html>
Output:
Web Development Tools
Tool | Category | Rating |
---|---|---|
VS Code | Editor | 5/5 |
Git | Version Control | 5/5 |
Customizing Padding for Specific Sides
You can control padding individually for the top, right, bottom, or
left sides using specific properties like padding-top
or
padding-left
.
Example:
<style>
td {
padding-top: 10px;
padding-bottom: 20px;
padding-left: 5px;
padding-right: 15px;
}
</style>
HTML Table - Cell Spacing
Cell spacing refers to the space between the borders of adjacent
cells. It separates the cells visually, making the table content more
organized. The CSS border-spacing
property is used to define spacing
between cells. By default, most browsers have a small gap between
table cells, but this can be customized.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-spacing: 10px; /* Adds 10px spacing between cells */
width: 100%;
border: 1px solid black;
}
th, td {
border: 1px solid gray;
padding: 10px;
text-align: left;
}
</style>
</head>
<body>
<table>
<tr>
<th>Language</th>
<th>Type</th>
</tr>
<tr>
<td>Python</td>
<td>Interpreted</td>
</tr>
<tr>
<td>JavaScript</td>
<td>Scripting</td>
</tr>
</table>
</body>
</html>
Output:
Language | Type |
---|---|
Python | Interpreted |
JavaScript | Scripting |
Removing Cell Spacing
To remove spacing between cells, use the border-collapse: collapse;
property. This merges the borders of adjacent cells.
Example
<style>
table {
border-collapse: collapse; /* Removes all cell spacing */
width: 100%;
}
th, td {
border: 1px solid black;
padding: 10px;
}
</style>
Updated <style>
output:
Language | Type |
---|---|
Python | Interpreted |
JavaScript | Scripting |
FAQs on HTML Table Padding & Spacing
What is the default padding in an HTML table?
By default, table cells have no padding (0
). You can add padding
with the CSS padding
property to make the content more visually
appealing.
How can I add spacing between table rows?
You can use the border-spacing
property to add space between rows
and columns. For vertical spacing only, specify it as border-spacing: 0 10px;
.
table {
border-spacing: 0 15px; /* Adds 15px spacing between rows */
}
How do I remove spacing between table cells?
Use border-collapse: collapse;
on the <table>
element to remove
cell spacing entirely and merge cell borders.
table {
border-collapse: collapse;
}
What is the difference between cell padding and cell spacing?
Cell padding refers to the space between the content inside a cell and
its border, managed using the padding
property in CSS. Cell spacing
is the space between the borders of adjacent cells, which can be
adjusted using the border-spacing
property or removed with
border-collapse
.
Can I use padding and spacing together in a table?
Yes, both can be applied to achieve a balanced layout. Use padding
to control the spacing within cells and border-spacing
to adjust the
spacing between cells.
This guide ensures a practical understanding of padding and spacing for designing clean and user-friendly HTML tables. Experiment with these properties to create the best layout for your content.