CSS Padding
CSS Padding
Section titled “CSS Padding”The CSS padding properties are used to create space inside an element, between its content and its border. Padding contributes to the element’s visible size.
Understanding Padding
Section titled “Understanding Padding”Padding clears an area around the content within the element’s boundaries. Any background color or image applied to the element will extend into the padding area.
Think of it like the matting around a picture in a frame: the padding is the matting, the content is the picture, and the border is the frame.
You can control the padding on all four sides individually or use a shorthand property.
Padding Values
Section titled “Padding Values”Padding can be specified using various units:
| Value Type | Description |
|---|---|
length | Defines a fixed padding using units like px, em, rem. Example: padding: 10px; |
% | Defines padding as a percentage relative to the width of the containing block. Example: padding: 5%; |
Note: Percentage padding (even padding-top and padding-bottom) is calculated based on the width of the containing block, which can sometimes be counter-intuitive.
Individual Padding Properties
Section titled “Individual Padding Properties”You can set the padding for each side separately:
padding-toppadding-rightpadding-bottompadding-left
Example:
.box { padding-top: 10px; padding-right: 20px; padding-bottom: 10px; padding-left: 20px; border: 1px solid black; background-color: lightyellow;}Padding Shorthand Property
Section titled “Padding Shorthand Property”The padding property is a shorthand that allows you to set padding for all sides in one declaration. It can accept one, two, three, or four values:
- One value:
padding: 10px;(Applies 10px padding to all four sides) - Two values:
padding: 10px 20px;(First value applies to top and bottom, second value applies to left and right) - Three values:
padding: 10px 20px 5px;(First value for top, second for left and right, third for bottom) - Four values:
padding: 10px 20px 5px 15px;(Values applied in clockwise order: top, right, bottom, left)
Example using two values:
.button { /* 5px padding top/bottom, 15px padding left/right */ padding: 5px 15px; border: 1px solid gray; background-color: lightblue; display: inline-block; /* To make padding visible */}Example using four values:
.content-area { /* 10px top, 20px right, 30px bottom, 40px left */ padding: 10px 20px 30px 40px; border: 1px solid green;}Padding and the Box Model
Section titled “Padding and the Box Model”Remember that padding increases the total space occupied by an element. How this affects the calculated width and height depends on the box-sizing property.
box-sizing: content-box(default): Padding is added outside the specifiedwidthandheight.box-sizing: border-box: Padding is included within the specifiedwidthandheight(content area shrinks).
Using box-sizing: border-box; is often recommended for more predictable layouts when using padding.
All CSS Padding Properties
Section titled “All CSS Padding Properties”| Property | Description |
|---|---|
| padding | Shorthand to set all four padding properties. |
| padding-top | Sets the top padding. |
| padding-right | Sets the right padding. |
| padding-bottom | Sets the bottom padding. |
| padding-left | Sets the left padding. |