CSS Padding Property: Complete Reference
What Is the CSS padding Property?
The CSS padding property defines the space between an element's content and its border. It is the innermost part of the box model, sitting between the content area and the border. Unlike margin (which creates space outside the border), padding pushes the content inward, expanding the element's background and clickable area without affecting the space between elements.
Padding can be applied uniformly to all four sides or individually to the top, right, bottom, and left sides. It accepts values in various CSS units (px, em, rem, %, etc.) and cannot be negative.
Why Padding Matters
Padding is fundamental for creating visually balanced, readable, and user-friendly interfaces. Here are key reasons why it matters:
- Readability: Prevents text from touching borders or edges, improving legibility.
- Visual hierarchy: Creates breathing room around elements, helping separate content sections.
- Touch targets: On mobile devices, adequate padding ensures buttons and links are easy to tap.
- Background extension: Expands the area covered by background colors or images.
- Accessibility: Larger clickable areas with proper padding meet WCAG success criteria.
- Box-sizing interaction: Padding works with
box-sizingto control element sizing behavior.
How to Use the padding Property
1. Shorthand Notation (One to Four Values)
The shorthand padding property accepts 1–4 values, following the TRBL (top-right-bottom-left) clockwise order:
- One value: Applied equally to all four sides.
padding: 20px; - Two values: First = top/bottom, second = left/right.
padding: 10px 20px; - Three values: Top, left/right, bottom.
padding: 10px 20px 15px; - Four values: Top, right, bottom, left.
padding: 10px 20px 15px 25px;
/* All sides equal */
.box {
padding: 20px;
}
/* Vertical / horizontal */
.box {
padding: 10px 30px;
}
/* Top / horizontal / bottom */
.box {
padding: 5px 20px 15px;
}
/* Top / right / bottom / left */
.box {
padding: 5px 10px 15px 20px;
}
2. Longhand Properties
For granular control, use the individual side properties:
padding-toppadding-rightpadding-bottompadding-left
/* Equivalent longhand example */
.card {
padding-top: 10px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
}
/* Same as: padding: 10px 20px; */
3. Acceptable Values
Padding can take the following value types:
- Length values:
px,em,rem,pt,cm, etc. - Percentage: Relative to the width of the containing block (not height).
- Inherit:
inherittakes the computed value from the parent element. - Initial:
initialresets to default (0).
/* Different value types */
.relative-padding {
padding: 5%; /* 5% of parent's width */
}
.em-padding {
padding: 2em; /* relative to element's font-size */
}
.inherit-padding {
padding: inherit; /* takes parent's padding */
}
4. Padding and the Box Model
By default (box-sizing: content-box), padding adds to the element's total width and height. With box-sizing: border-box, padding is included within the specified width/height, making sizing more predictable.
/* Content-box (default) – padding adds to size */
.content-box {
box-sizing: content-box;
width: 200px;
padding: 20px;
/* Total width = 200 + 20 + 20 = 240px */
}
/* Border-box – padding shrinks content area */
.border-box {
box-sizing: border-box;
width: 200px;
padding: 20px;
/* Total width = 200px (content = 160px) */
}
Practical Code Examples
Example 1: Button with Accessible Padding
<button class="btn">Click Me</button>
<style>
.btn {
padding: 12px 24px; /* comfortable touch target */
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
}
.btn:hover {
background-color: #0056b3;
}
</style>
Example 2: Card Component with Internal Spacing
<div class="card">
<h2 class="card-title">Card Title</h2>
<p class="card-content">This is the card content with proper padding for readability.</p>
</div>
<style>
.card {
background: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 20px; /* overall card padding */
max-width: 400px;
}
.card-title {
margin-top: 0;
margin-bottom: 10px;
padding: 0; /* reset default if needed */
}
.card-content {
margin: 0;
padding: 10px 0; /* vertical padding only */
line-height: 1.6;
}
</style>
Example 3: Responsive Padding Using Clamp
<div class="responsive-box">Responsive padding</div>
<style>
.responsive-box {
padding: clamp(10px, 3vw, 30px); /* min 10px, preferred 3vw, max 30px */
background: #e9ecef;
border: 1px solid #adb5bd;
}
</style>
Example 4: Padding on Inline Elements
<p>This is a <span class="highlight">highlighted</span> word.</p>
<style>
.highlight {
background-color: #fff3cd;
padding: 2px 6px; /* works only on inline elements horizontally */
border-radius: 3px;
}
</style>
Note: On inline elements, padding is applied horizontally but can overflow vertically. For consistent vertical padding on inline text, consider using display: inline-block or line-height adjustments.
Best Practices for Using Padding
- Use a consistent spacing scale: Define a set of padding values (e.g., 4px, 8px, 12px, 16px, 24px, 32px) and stick to them for visual harmony.
- Prefer
box-sizing: border-box: Set it globally to avoid unexpected sizing calculations. This makes padding and border part of the element's defined width/height. - Use percentages carefully: Percentage padding is based on parent's width, which can cause unexpected vertical spacing. For aspect-ratio tricks, this behavior is useful.
- Consider responsive units: For fluid layouts, use
rem,em,vw, orclamp()to scale padding proportionally. - Avoid negative padding: The
paddingproperty does not accept negative values; usemarginor positioning for outward spacing. - Reset default padding: Browsers apply default padding to certain elements (e.g.,
<ul>,<body>). Use a CSS reset or normalize to ensure consistency. - Test touch targets: Ensure interactive elements have at least 44x44px tap area (including padding) for mobile accessibility.
- Combine with other properties: Padding works alongside
border,background-clip, andoutlinefor advanced styling. - Use logical properties for internationalization: For multilingual sites, consider
padding-inlineandpadding-blockwhich adapt to writing direction.
Global Box-Sizing Reset
/* Recommended global reset */
*,
*::before,
*::after {
box-sizing: border-box;
}
Logical Padding Example (CSS Logical Properties)
/* Adapts to left-to-right and right-to-left scripts */
.rtl-friendly {
padding-inline: 20px 30px; /* left/right depending on direction */
padding-block: 10px 20px; /* top/bottom */
}
Conclusion
The CSS padding property is a core tool for controlling internal spacing in web layouts. Understanding its shorthand notation, longhand variants, value types, and interaction with the box model is essential for building clean, accessible, and responsive interfaces. By applying best practices such as consistent spacing scales, border-box sizing, and responsive units, developers can create designs that are both visually appealing and user-friendly. Mastering padding—alongside margins, borders, and box-sizing—gives you precise control over the layout and ensures your content always has the right amount of breathing room.