CSS Padding
The CSS padding properties are used to generate space around an element's content, inside of any defined borders.
CSS Padding
The CSS padding properties are used to generate space around an element's content, inside of any defined borders.
With CSS, you have full control over the padding. There are properties for setting the padding for each side of an element (top, right, bottom, and left).
Example 1: Padding - Individual Sides
CSS has properties for specifying the padding for each side of an element:
padding-toppadding-rightpadding-bottompadding-left
Example 2: Padding - Shorthand Property
To shorten the code, it is possible to specify all the padding properties in one single property.
(top right bottom left)
Example 3: Padding and Element Width
The CSS width property specifies the width of the element's content area. The content area is the portion inside the padding, border, and margin of an element (the box model).
So, if an element has a specified width, the padding added to that element will be added to the total width of the element. This is often an undesirable result.
Example 4: Box Sizing
To keep the width at 300px, no matter the amount of padding, you can use the box-sizing property. This causes the element to maintain its width; if you increase the padding, the available content space will decrease.
Example
div {
padding: 25px 50px 75px 100px;
}
.box {
width: 300px;
padding: 25px;
box-sizing: border-box;
}