You may be familiar with the HTML 'cellpadding' attribute of the 'table' tag. This attribute creates space inside of a table cell so that you get a nice bit of white space, or "padding", between your element and the sides of the table.
Well, there isn't actually a CSS 'cellpadding' property or attribute, but there is the CSS padding property which allows you to achieve the same effect - and more. You can use this property to set the padding on your table cells. You can even set different padding for each side of the cell. Furthermore, you can use this property on most elements - you're not limited to table cells.
HTML 'cellpadding' Example
Here's a typical example of cellpadding usage in HTML.
CSS 'padding' Example
To achieve the same effect using CSS, use the following code.
Padding for Each Side
One of the great things about the CSS 'padding' property is that you can apply different padding to each side of the cell.
There are two methods of specifying padding for different sides of an element.
This method uses a shorthand property for setting padding-top, padding-right, padding-bottom, and padding-left. This method is quicker. It also uses less code than the previous method. Actually, it's the same property that we used in our first example.
Here, we apply multiple values against the padding property.
You don't need to provide different values for all four sides. You can provide one, two, three, or four values. Here's how it works:
If there is only one value, it applies to all sides. If there are two values, the top and bottom paddings are set to the first value and the right and left paddings are set to the second. If there are three values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third. If there are four values, they apply to the top, right, bottom, and left, respectively.
In other words:
- All four sides have padding of 10 pixels.
- Top and bottom have padding of 10 pixels.
- Right and left have padding of 20 pixels.
padding:10px 20px 30px;
- Top is 10px
- Left and right are 20px
- Bottom is 30px
padding:10px 20px 30px 40px;
- Top is 10px
- Right is 20px
- Bottom is 30px
- Left is 40px
Padding on Other Elements
As mentioned, you can apply padding to most HTML elements - not just table cells.
In this case, instead of applying padding against a table cell, we apply it to a div element.
As you can see, applying padding to an element can affect the size of that element. In the example above, both
div elements are specified to be 100 pixels wide. However, the padding on the first
div pushes the size out, resulting in a larger
You can't apply CSS padding to table-row-group, table-header-group, table-footer-group, table-row, table-column-group and table-column elements.
You can also use CSS border-collapse to collapse the border. Although this can have a similar effect to cellpadding in some cases, it isn't padding and shouldn't really be used as such.
Enjoy this page?
- Link to this page (copy/paste into your own website or blog):
- Link to Quackit using one of these banner ads.
Thanks for supporting Quackit!