CSS Padding
Padding is the breathing room inside every element — the space between the content and the border. Getting padding right is what transforms cramped, unreadable interfaces into comfortable, professional designs.
box-sizing — how
they interactpadding-block, padding-inlineWhat is CSS Padding?
Padding is the transparent space between an element's
content and its border. It lives inside the border and takes the
element's background-color. Clicking anywhere inside the padding area
triggers events on the element — it is fully part of the clickable/interactive area.
(text, image, child elements)
↑ Green dashed area = padding (inside the border)
background: blue, the blue
fills both the content area and the padding. This is why padding makes a button feel more clickable —
the whole coloured area responds.box-sizing: content-box (default),
padding adds to the element's total width. With box-sizing: border-box, padding is carved out of the declared width.
Always use border-box.Individual Padding Properties
Each side of the element can be padded independently using four longhand properties:
.box {
padding-top: 16px; /* space above the content */
padding-right: 24px; /* space to the right of content */
padding-bottom: 16px; /* space below the content */
padding-left: 24px; /* space to the left of content */
}
/* Accepted value types */
.units {
padding-top: 0; /* unitless zero — no unit needed */
padding-right: 1rem; /* relative to root font-size (16px) */
padding-bottom: 5%; /* % of parent WIDTH (not height!) */
padding-left: 1.5em; /* relative to element font-size */
}
/* ✗ INVALID — padding cannot be negative */
.bad { padding-top: -10px; } /* ignored by browser */
Padding Shorthand — 1 to 4 Values
The padding shorthand uses the same 1–4 value
clockwise pattern as margin — TRouBLe: Top → Right →
Bottom → Left.
| Shorthand | Equivalent to | Pattern |
|---|---|---|
| padding: 20px; | All four sides = 20px | 1 value → all sides |
| padding: 10px 20px; | Top/bottom = 10px · Left/right = 20px | 2 values → vertical / horizontal |
| padding: 5px 10px 15px; | Top = 5px · Left/right = 10px · Bottom = 15px | 3 values → top / sides / bottom |
| padding: 5px 10px 15px 20px; | Top=5 · Right=10 · Bottom=15 · Left=20 | 4 values → clockwise TRouBLe |
/* Common shorthand patterns */
.card { padding: 24px; } /* equal all sides */
.btn { padding: 10px 20px; } /* shorter top/bottom, wider sides */
h2 { padding: 0 0 16px; } /* only bottom padding */
.nav-link { padding: 12px 16px; } /* standard nav item */
.input { padding: 10px 14px; } /* form field feel */
.section { padding: 64px 24px; } /* tall section with side gutters */
.tooltip { padding: 6px 12px; } /* compact tooltip */
/* Override one side after shorthand */
.sidebar-item {
padding: 12px 16px;
padding-left: 24px; /* deeper left indent */
}
/* Remove all padding (common reset) */
ul { padding: 0; }
p { padding: 0; }
Padding vs Margin — Side by Side
These two properties are easy to confuse. The visual below shows the key difference — notice how the green background fills the padding area on the left, but the yellow spacing on the right is always transparent:
Background fills here ↑
No background outside →
| Feature | Padding | Margin |
|---|---|---|
| Position | Inside the border | Outside the border |
| Background colour | Yes — shows element background | No — always transparent |
| Mouse events | Triggers on element | Does not trigger |
| Can be negative? | No — invalid | Yes |
| Can be auto? | No | Yes (for centering) |
| Collapse? | Never collapses | Vertical margins collapse |
| Affected by box-sizing? | Yes | No |
Padding & box-sizing
The interaction between padding and box-sizing is
one of the most important concepts in CSS layout:
/* ── content-box (DEFAULT — causes surprises) ── */
.content-box {
box-sizing: content-box;
width: 300px;
padding: 24px;
/* Total rendered width = 300 + 24×2 = 348px ← WIDER than declared! */
}
/* ── border-box (RECOMMENDED) ── */
.border-box {
box-sizing: border-box;
width: 300px;
padding: 24px;
/* Total rendered width = 300px ← exactly what you declared */
/* Content area = 300 - 24×2 = 252px */
}
/* ── Always add this universal reset ── */
*, *::before, *::after {
box-sizing: border-box;
}
/* ── Padding on full-width elements is safe with border-box ── */
input {
width: 100%;
padding: 10px 14px; /* won't overflow parent with border-box */
}
width: 100% and adding padding — it overflows the
parent container with content-box! With border-box, width: 100% always stays
inside the container regardless of padding. Always use the universal border-box reset.
Percentage Padding & the Aspect Ratio Trick
A quirk of CSS: percentage values for padding on all four sides — including top and bottom — are calculated relative to the parent element's WIDTH, not its height. This allows you to create perfectly proportional responsive boxes using only CSS.
/* Classic padding-bottom aspect ratio technique */
.video-wrapper {
width: 100%;
height: 0; /* collapse height */
padding-bottom: 56.25%; /* 9/16 = 56.25% → 16:9 ratio */
position: relative;
}
.video-wrapper iframe {
position: absolute;
top: 0; left: 0; width: 100%; height: 100%;
}
/* Common ratio formulas */
/* 1:1 square → padding-bottom: 100% */
/* 16:9 widescreen → padding-bottom: 56.25% */
/* 4:3 traditional → padding-bottom: 75% */
/* 3:2 photo → padding-bottom: 66.67% */
/* 2:3 portrait → padding-bottom: 150% */
/* ⚡ Modern alternative — CSS aspect-ratio property (2021+) */
.modern-video {
width: 100%;
aspect-ratio: 16 / 9; /* cleaner, no padding trick needed */
}
aspect-ratio property (supported in all modern browsers since 2021). Use
aspect-ratio: 16/9 for new projects. The padding trick is still useful
for supporting older browsers or systems that don't support aspect-ratio.
Real-World Padding Patterns
Padding decisions directly impact how polished a UI feels. Here are the most common patterns you'll use in real projects — all rendered live:
/* ── Buttons ── */
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-md { padding: 10px 20px; font-size: 14px; }
.btn-lg { padding: 14px 28px; font-size: 16px; }
.btn-icon{ padding: 10px; } /* square icon button */
/* ── Card with distinct sections ── */
.card-header { padding: 12px 16px; border-bottom: 1px solid #e5e7eb; }
.card-body { padding: 16px; }
.card-footer { padding: 10px 16px; border-top: 1px solid #e5e7eb; }
/* ── Navigation links ── */
.nav-link { padding: 10px 18px; }
.nav-link.active{ padding-left: 22px; } /* extra left indent for active */
/* ── Form inputs ── */
input, textarea, select {
width: 100%;
padding: 10px 14px;
box-sizing: border-box; /* critical — stay inside container */
}
/* ── Section layout ── */
.section {
padding: clamp(32px, 6vw, 96px) clamp(16px, 4vw, 40px);
/* responsive vertical/horizontal padding */
}
Logical Properties — padding-block & padding-inline
Logical properties replace physical direction names with writing-mode-aware names that work correctly in all text directions — LTR (left-to-right) and RTL (right-to-left Arabic/Hebrew/etc.).
| Logical Property | Physical Equivalent (LTR) | Meaning |
|---|---|---|
| padding-block-start | padding-top | Start of block axis (top in LTR) |
| padding-block-end | padding-bottom | End of block axis (bottom in LTR) |
| padding-inline-start | padding-left | Start of inline axis (left in LTR, right in RTL) |
| padding-inline-end | padding-right | End of inline axis (right in LTR, left in RTL) |
| padding-block | padding-top + padding-bottom | Shorthand for both block-axis sides |
| padding-inline | padding-left + padding-right | Shorthand for both inline-axis sides |
/* Logical shorthand */
.card {
padding-block: 24px; /* top and bottom */
padding-inline: 20px; /* left and right (or right/left in RTL) */
}
/* Equivalent to: padding: 24px 20px; in LTR */
/* Individual logical sides */
.nav-link {
padding-block: 10px;
padding-inline-start: 20px; /* extra indent at reading start */
padding-inline-end: 14px;
}
/* In RTL, padding-inline-start automatically becomes right-side padding */
/* No media queries or direction checks needed */
Try It Yourself
Experiment with different padding values on buttons, cards and inputs. Try removing
box-sizing: border-box from the input to see the overflow bug in action.
Quick Check — Chapter 12
Position: Padding is inside the border; margin is outside.
Background: Padding shows the element's background colour; margin is always transparent.
Events: Clicking in the padding area triggers events on the element; clicking in margin does not.
Negative values: Padding cannot be negative; margin can.
Auto value: Padding does not support
auto; margin
does (for horizontal centering).Collapse: Padding never collapses; vertical margins collapse.
box-sizing: Padding is affected by
box-sizing;
margin is not.
First value → top and bottom padding
Second value → left and right padding
So
padding: 10px 20px means: top=10px, right=20px, bottom=10px,
left=20px.The full shorthand pattern for all four values is clockwise from the top — TRouBLe: Top, Right, Bottom, Left. Two values means vertical/horizontal. Three values means top/sides/bottom. One value means all four sides.
box-sizing: content-box, adding padding to an
element makes it wider than the declared width. For example, width: 300px; padding: 24px renders at 348px total (300 + 24 +
24).With
box-sizing: border-box, the declared width
includes the padding. A 300px element with 24px padding stays exactly 300px — the
content area shrinks to 252px instead.The most common mistake this prevents: setting
width: 100% and
adding padding on an input — without border-box the input overflows
its container. Always add *, *::before, *::after { box-sizing: border-box; } as your first CSS
rule.
padding-top and padding-bottom — are calculated relative to the parent element's
width, not its height. This is a CSS quirk but an intentional one.The most famous use is the aspect ratio trick: set
width: 100%, height: 0 and padding-bottom: 56.25% (which is 9/16 = 56.25%) to create a perfectly
proportional 16:9 responsive box that scales with its container width.Modern CSS now has the
aspect-ratio property which is cleaner: aspect-ratio: 16/9. But the padding trick is still useful for older
browser support.
padding-block — sets both padding-top and padding-bottom (the
block axis, which runs vertically in LTR).padding-inline — sets both padding-left and padding-right (the
inline axis). In RTL text, this automatically becomes right/left instead.Individual sides:
padding-block-start (= top in LTR), padding-block-end (= bottom), padding-inline-start (= left in LTR, right in RTL), padding-inline-end.They're especially useful when building internationalised layouts that need to support both LTR and RTL languages without separate CSS.
width: 200px; padding: 20px
with box-sizing: border-box. What is the content area width?padding-bottom trick?padding-left + padding-right?- Padding is the space inside the border between content and the border edge. It always shows the element's background colour and responds to mouse events.
- Padding cannot be negative or
auto— both are invalid and ignored. - Four individual properties:
padding-top/right/bottom/left. The shorthand follows the same TRouBLe clockwise pattern asmargin. - With
box-sizing: border-box, padding is carved from the declared width. Withcontent-box(default), padding is added on top. Always use the universalborder-boxreset. - Percentage padding — all sides including top/bottom — is calculated relative to the
parent's width. This enables the classic aspect-ratio trick (
padding-bottom: 56.25%for 16:9). - Modern alternative:
aspect-ratio: 16/9is cleaner and supported in all modern browsers (2021+). - Real-world patterns: button sizes (6/10/14px vertical), card sections, form inputs always need
box-sizing: border-box, and nav links use consistentpadding-block/padding-inline. - Logical properties:
padding-block(top + bottom),padding-inline(left + right, RTL-aware).
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.