padding accepts the same one, two, or four-value shorthand syntax as margin, but padding's space sits inside the border and is filled with the element's own background color or image, visually distinct from margin's transparent space outside the border that shows the parent or page's background instead. Padding also directly adds to an element's total rendered size under the default box-sizing: content-box, since padding is added on top of the specified width and height, which is the underlying reason box-sizing: border-box exists, letting padding be included within, rather than added onto, a specified width.
1Understanding Padding
padding accepts the same one, two, or four-value shorthand syntax as margin, but padding's space sits inside the border and is filled with the element's own background color or image, visually distinct from margin's transparent space outside the border that shows the parent or page's background instead. Padding also directly adds to an element's total rendered size under the default box-sizing: content-box, since padding is added on top of the specified width and height, which is the underlying reason box-sizing: border-box exists, letting padding be included within, rather than added onto, a specified width.
Under the default box-sizing: content-box, adding padding increases an element's total rendered width and height beyond its specified width/height values — if that's not the behavior you want, apply box-sizing: border-box so padding is included within the specified dimensions instead.
.button {
padding: 12px 24px;
background: blue;
}2Practical Example
Here is a real-world application of Padding showing how it is used in production CSS code.
.box {
width: 200px;
padding: 20px;
box-sizing: content-box;
}3Best Practices
Follow these guidelines when working with Padding:
1. Use padding to create internal breathing room between an element's content and its edges, distinct from margin's external spacing between elements
2. Account for padding adding to an element's total size under the default box-sizing, or apply box-sizing: border-box if you want padding included within a fixed width instead
3. Use the one/two/four-value shorthand for concise padding declarations, rather than four separate padding-top/right/bottom/left declarations, unless only one side actually needs a distinct value
Tip: Under the default box-sizing: content-box, adding padding increases an element's total rendered width and height beyond its specified width/height values — if that's not the behavior you want, apply box-sizing: border-box so padding is included within the specified dimensions instead.
.button {
padding: 12px 24px;
background: blue;
}