CSS Reference ยท Box Model and Sizing
The padding property
Reference for the CSS padding shorthand: one to four values, percentages, how padding interacts with box-sizing, syntax, examples and browser support.
The padding property sets the space inside an element, between its content and its border. Padding takes on the element's background, so it makes the box look bigger and gives text room to breathe. It is a shorthand for padding-top, padding-right, padding-bottom and padding-left.
Values are read clockwise from the top, and missing values are copied from the opposite side.
CSS
padding: 1rem; /* all four sides */ padding: 1rem 2rem; /* top and bottom | left and right */ padding: 1rem 2rem 3rem; /* top | left and right | bottom */ padding: 1rem 2rem 3rem 4rem; /* top | right | bottom | left */
Try it
Syntax
CSS
padding: <padding-top>{1,4} /* each value: */ <length> | <percentage>
- Initial value:
0for every side - Applies to: all elements except internal table row groups, header groups, footer groups, rows, and column groups;
::first-letterand::first-linetoo - Inherited: no
- Percentages: relative to the width of the containing block, for all four sides
- Animatable: yes
- Computed value: each side as a percentage or absolute length
Values
| Value | Description |
|---|---|
<length> |
A fixed size such as 12px, 1rem or 2em. |
<percentage> |
A share of the containing block's width, even for top and bottom. |
Negative values and auto are not allowed.
Usage notes
- Padding adds to the size when
box-sizingiscontent-box(the browser default): awidth: 200pxbox withpadding: 30pxis 260px wide. Settingbox-sizing: border-boxmakeswidthinclude padding and border, which most stylesheets prefer. - No collapsing. Unlike margins, padding never merges with neighboring padding.
- Percentages follow width.
padding-top: 50%is half the container's width, which is a known trick for ratio boxes. Modern code should useaspect-ratio. - Clickable area. Padding on links and buttons is part of the click target, so generous padding helps touch users.
- Background and overflow. The background paints under the padding (see
background-clip), and scrollable content scrolls inside the padding box. - Inline elements. Horizontal padding pushes text apart; vertical padding paints the background but does not change the line height.
- Logical alternatives.
padding-inlineandpadding-blockfollow the writing mode.
Browser support
Supported in every browser.
Related
padding-top,padding-right,padding-bottom,padding-leftmarginโ space outside the borderbox-sizing- Lessons: Padding, The Box Model