19.5. PaddingThe padding area is an optional amount of space held between the content area of an element and its border. If you are putting a border on an element, it is usually a good idea to add a bit of padding as well to keep the border from bumping against the content. Now that you've seen margins and borders at work, the padding properties should look familiar. There are side-specific properties for setting an amount of padding on each side by name, and a shorthand padding property that applies padding to combinations of four sides.
Values:<length> | <percentage> | inherit Initial value:0 Applies to:All elements Inherited:No
Values:[<length> | <percentage> ]{1,4} | inherit Initial value:0 Applies to:All elements Inherited:No The padding properties specify the width of the padding area. Values may be provided in units of length or as percentages. Negative values are not permitted for padding. It is important to note that, as for margins, percentage values are always calculated based on the width of the parent element (even for top and bottom padding). So if the width of the parent element should change, so will the percentage padding values on all sides of the child element. Figure 19-11 shows examples of element padding. h2#A {padding: 4px; background: #CCC;} h2#B {padding: 20px; background: #CCC;} <h2 >Sed ultrices ligula at metus.</h2> <h2 >Sed ultrices ligula at metus.</h2> Figure 19-11. Adding padding around elements![]() Background colors and images applied to an element will display in its padding area (this sets it apart from margins, which are always transparent). So if you want an element to appear in a colored box, with or without an explicit border, padding is the way to put a little space between the edge of the box and the content. Padding does not collapse as margins do. The total padding between elements will be the sum of the padding for the adjacent sides of the elements. |