CSS Padding Property
The padding property is a core component of the CSS Box Model. It defines the innermost spacing between an element's content and its border. Unlike margin, which pushes other elements away, padding expands the internal size of the element itself.
The Box Model Context
In the standard box model, adding padding increases the total width and height of the element. For example, a 100px wide box with 20px of padding on all sides will actually render as 140px wide on screen, unless you use box-sizing: border-box.
Shorthand Syntax
Padding can be set using 1, 2, 3, or 4 values:
- 1 Value: All 4 sides (e.g.,
10px). - 2 Values: Vertical (Top/Bottom) then Horizontal (Left/Right).
- 4 Values: Clockwise starting from Top (Top, Right, Bottom, Left).
Padding vs Margin
Padding: Internal space. Takes on the background color/image of the element. Included in the click area.
Margin: External space. Always transparent. Creates distance between this element and neighbors.
Best Practices
Use padding to give your text and content "breathing room" so it doesn't touch the borders. Consistent padding (e.g., utilizing CSS variables) is key to a polished UI design.
