CSS Cellpadding Explained

CSS Cellpadding Detailed Explanation

CSS Cellpadding Detailed Explanation

In HTML, tables are a common element used to display data. In a table, each cell can contain content, and the spacing between the content and the cell is controlled using CSS properties. The cellpadding property is used to define the spacing between cell content and the cell border.

What is Cellpadding

cellpadding is a CSS property used to set the padding of table cells. In an HTML table, each cell can contain text, https://coder-cafe.com/wp-content/uploads/2025/09/images, or other elements. The cellpadding property allows us to control the spacing between cell content and the cell border, thereby improving the readability and appearance of the table.


How to Use Cellpadding

The cellpadding property can be applied directly to the <table> tag or set in a CSS style sheet. When the cellpadding property is applied to the <table> tag, all cells inherit the specified padding value.

Here is a simple example of an HTML table with the cellpadding attribute applied:

<!DOCTYPE html> 
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
cellpadding: 10px;
}
th, td {
border: 1px solid black;
text-align: center;
}
</style>
</head>
<body>

<table cellpadding="10">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Xiaoming</td>
<td>20</td>
</tr>
<tr>
<td>Xiaohong</td>
<td>22</td>
</tr> 
</table> 

</body> 
</html> 

In the example above, we specify a cellpadding value of 10px, which creates a 10-pixel space between the cell content and the cell border. To enhance the appearance of the table, we also set the cell border style and center alignment.

Setting Cellpadding with CSS Style Sheets

In addition to setting the cellpadding property directly within the <table> tag, we can also set the table’s padding using CSS style sheets. This method provides greater flexibility, allowing you to define different padding values ​​for different tables.

The following is an example of setting the table cellpadding property via CSS stylesheets:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
table { 
border-collapse: collapse; 
width: 100%; 
} 
table.padding-10 { 
cellpadding: 10px; 
} 
th, td { 
border: 1px solid black; 
text-align: center; 
} 
</style> 
</head> 
<body> 

<table class="padding-10"> 
<tr> 
<th>Name</th> 
<th>Age</th> 
</tr> 
<tr> 
<td>Xiaoming</td> 
<td>20</td>
</tr>
<tr>
<td>Xiaohong</td>
<td>22</td>
</tr>
</table>

</body>
</html>

Setting the cellpadding property via CSS offers greater flexibility than setting it directly within the <table> tag. We can define different padding values ​​within different classes and then create different table styles by adding the corresponding classes.

Practical Application of Table Cellpadding

In actual projects, the cellpadding attribute is often used to adjust the style of tables to make them more readable and beautiful. By controlling cell padding, we can maintain a certain distance between table content and borders, avoiding overcrowding and improving the user experience.

In addition to setting fixed pixel values, the cellpadding property also supports percentages and em units. This allows us to adjust cell padding according to design requirements, making the table more consistent with the overall style of the web design.

Summary

cellpadding is a very useful CSS property for controlling the spacing between table cell content and borders. By appropriately setting the cellpadding property, we can improve the readability and appearance of tables, enhancing the user experience. In real-world projects, the appropriate use of the cellpadding property can add aesthetics and personalization to tables.

Leave a Reply

Your email address will not be published. Required fields are marked *