What is CSS cellpadding?
What is CSS cellpadding?
When learning CSS style design, you often encounter the cellpadding
property. cellpadding
is a CSS property used to set the padding of cells in HTML table elements. This article will detail the purpose, usage, and examples of cellpadding
.
What is cellpadding
In HTML tables, each cell (<td>
) displays a certain amount of space within it, which is called padding. The cellpadding
property is used to set this padding for table cells, ensuring a certain distance between the cell content and the cell border.
How to Use CellPadding
The cellpadding
attribute can be applied directly to the <table>
tag or defined using CSS styles to define table padding. When applied directly to the <table>
tag, the value of the cellpadding
attribute applies to all cells in the table. When defined using CSS styles, different padding values can be applied to different tables.
Apply directly to <table>
tag
<!DOCTYPE html>
<html>
<head>
<style>
table {
cellpadding: 10px; /* Sets the table's padding to 10px */
}
</style>
</head>
<body>
<table border="1">
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
<tr>
<td>Cell 3</td>
<td>Cell 4</td>
</tr>
</table>
</body>
</html>
In the example above, we set the table’s padding to 10px by directly applying the cellpadding
property.
CSS style definition
<!DOCTYPE html>
<html>
<head>
<style>
.table1 {
cellpadding: 5px; /* Set the table padding to 5px */
}
.table2 {
cellpadding: 15px; /* Set the table padding to 15px */
}
</style>
</head>
<body>
<table class="table1" border="1">
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
<tr>
<td>Cell 3</td>
<td>Cell 4</td>
</tr>
</table>
<table class="table2" border="1">
<tr>
<td>Cell A</td>
<td>Cell B</td>
</tr>
<tr>
<td>Cell C</td>
<td>Cell D</td>
</tr>
</table>
</body>
</html>
In the above example, we defined two classes, table1
and table2
, using CSS styles and setting different padding values for each.
Example Effect
Through the above example code, we can see the effect of different padding settings on a table. By adjusting the value of the cellpadding
property, we can achieve different table display effects. Padding can make a table look more attractive and easier to read and understand.
Summary
cellpadding
is a CSS property used to set the padding of table cells. By appropriately setting padding values, you can make table content clearer and more aesthetically pleasing. In actual development, properly setting the cellpadding
property based on design requirements and aesthetics can improve the reading experience and user-friendliness of web pages.