CSS Padding
Padding is the space between an element's content and its border. It sits inside the element, so it takes on the element's background colour and makes the clickable or visible area larger. You use padding to give text room to breathe inside buttons, cards, table cells and sections.
Writing padding: longhand and shorthand
There are four longhand properties: padding-top, padding-right, padding-bottom and padding-left. Most of the time you will use the padding shorthand, which accepts one to four values and reads clockwise starting at the top.
| Shorthand | Meaning |
|---|---|
padding: 16px; | All four sides 16px. |
padding: 8px 16px; | Top and bottom 8px, left and right 16px. |
padding: 8px 16px 24px; | Top 8px, sides 16px, bottom 24px. |
padding: 4px 8px 12px 16px; | Top, right, bottom, left in that order. |
Padding values cannot be negative. If you need to pull something outward, that is a job for negative margin instead.
How padding changes the size of a box
By default, CSS uses box-sizing: content-box. With that setting, width describes only the content area, and padding is added on top. A box with width: 300px and padding: 20px ends up 340px wide before borders are counted.
That extra width breaks many layouts, especially ones based on percentages. The widely used fix is to switch every element to border-box, where padding and border are included in the declared width:
*, *::before, *::after {
box-sizing: border-box;
}
.card {
width: 300px;
padding: 20px;
border: 1px solid #ddd;
/* total width stays 300px */
}
.button {
padding: 0.6em 1.2em;
font-size: 1rem;
}
Using em units for button padding makes the space grow with the font size, so a larger button keeps the same proportions.
Percentage padding and logical properties
A percentage value for padding is calculated from the width of the containing block, even for top and bottom padding. This odd rule was once used to create fixed-ratio boxes for videos. Today the aspect-ratio property is a cleaner way to do that.
Modern CSS also offers logical properties: padding-inline for the start and end of a line (left and right in English) and padding-block for top and bottom. They adapt automatically to right-to-left languages and vertical writing.
Padding or margin? A frequent mix-up
Beginners often reach for margin when they want space inside a button, or padding when they want space between two cards. A simple test helps:
- If the space should show the element's background and be part of its click area, use padding.
- If the space should separate the element from its neighbours, use margin or a
gapon the parent.
Another subtle point: padding on inline elements such as <a> or <span> expands the background vertically, but it does not push surrounding lines apart. If you want vertical padding to affect layout, set the element to inline-block or block.
Remember
- Padding is inside the border and shares the background.
- Shorthand goes top, right, bottom, left.
- Set
box-sizing: border-boxso padding does not make boxes wider than you planned.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.