|
CSS Home
CSS TutorialIntroductionCSS Advantages CSS Syntax Implementing CSS CSS Class CSS ID CSS Font CSS Text CSS Background Code CSS Border CSS Margin CSS Padding CSS Lists CSS Height & Width CSS Positioning CSS Float CSS Layers CSS Summary CSS CodesCSS AlignCSS bgcolor CSS Cellpadding CSS Cellspacing CSS Floating Menu CSS Hyperlinks CSS Leading Inline Style Sheets Embedded Style Sheets External Style Sheets CSS Scrollbars CSS Table Width CSS Print Version CSS Table-layout CSS ExamplesCSS ExamplesCSS Table-layout Example CSS ReferenceCSS Mini TutorialCSS Properties CSS Color Codes CSS Template CSS Media Types |
CSS CellpaddingYou 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' ExampleHere's a typical example of cellpadding usage in HTML. Code:
Result:
CSS 'padding' ExampleTo achieve the same effect using CSS, use the following code. Code:
Result:
Padding for Each SideOne 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. Method 1This method sets padding using the padding-top, padding-right, padding-bottom, and padding-left properties. Code:
Result:
Method 2This 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. Code:
Result:
VariationsYou 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:
Padding on Other ElementsAs 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. Also, this example uses inline style sheets to apply the padding (the previous example uses embedded style sheets). Code:
Result: As you can see, applying padding to an element can affect the size of that element. In the example above, both Further InformationYou 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. Also see CSS padding, CSS Cellspacing, and CSS border-spacing. |
Need Website Content?
Get unique, quality digital content for your website.
|