margin does).px, em, rem, %.padding-top, padding-right, padding-bottom, padding-left.Basic syntax uses a selector and the padding property:
/* Basic padding syntax for any element */
selector {
padding: value;
}
Shorthand with 1–4 values:
padding: 20px; → top, right, bottom, left all 20pxpadding: 10px 20px; → top & bottom 10px, left & right 20pxpadding: 5px 10px 15px; → top 5px, left & right 10px, bottom 15pxpadding: 5px 10px 15px 20px; → top, right, bottom, left (clockwise)Same padding on all four sides of the element.
/* Same padding on all sides of the box */
.container {
padding: 20px;
}
Control vertical (top/bottom) and horizontal (left/right) spacing separately.
/* Vertical padding (top & bottom), horizontal padding (left & right) */
.vertical-padding {
padding: 10px 0;
}
.horizontal-padding {
padding: 0 20px;
}
Fine-tune padding for each side of the element.
/* Different padding for each side of the box */
.card {
padding-top: 10px;
padding-right: 15px;
padding-bottom: 12px;
padding-left: 8px;
}
This paragraph has padding, so there is extra space between this text and the dashed border.
Padding grows the box from the inside without changing the outer margin.
Common padding-related properties and example values:
| Property | Description | Example Value |
|---|---|---|
padding |
Sets padding on all four sides | 20px, 2em, 10% |
padding-top |
Sets padding on the top side | 10px |
padding-right |
Sets padding on the right side | 15px |
padding-bottom |
Sets padding on the bottom side | 10px |
padding-left |
Sets padding on the left side | 15px |
px for precise control, em/rem for scalable, responsive designs, and % when padding should depend on container size.padding with width or max-width to create nicely centered content inside cards or boxes.<div> with a border and apply different padding values (e.g., 5px, 20px, 40px) to see how the inner spacing changes.padding-top, padding-right, padding-bottom, and padding-left individually on a card component and observe how each side moves.padding using px, em, and % to understand how each unit behaves in different layouts.