Tutorials CSS Padding
Ch 12 / 60
Chapter 12 Beginner 5 min read CSS

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.

What you'll learn
All four padding properties and the shorthand
Padding vs margin — key differences
Percentage padding and the aspect ratio trick
Real-world patterns: buttons, cards, forms, navbars
Padding and box-sizing — how they interact
Logical properties: padding-block, padding-inline

What 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.

padding-top padding-right padding-bottom padding-left
Content Area
(text, image, child elements)

↑ Green dashed area = padding (inside the border)

Inside the border
Padding sits between the content and the border. The element's background colour fills the padding area — it looks like part of the element's body.
Background carries through
If you set 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.
Cannot be negative
Unlike margin, padding values must be zero or positive. Negative padding is invalid and ignored by the browser.
Affected by box-sizing
With 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:

CSS
.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
CSS — Shorthand examples
/* 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:

padding: 20px
Content
Background fills here ↑
margin: 20px
↑ margin-top (transparent)
Content
No background outside →
↓ margin-bottom (transparent)
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:

CSS — box-sizing interaction
/* ── 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 */
}
The most common beginner mistake
Setting an input to 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.

1:1
padding-bottom: 100%
16:9
padding-bottom: 56.25%
4:3
padding-bottom: 75%
2:3
padding-bottom: 150%
CSS — Aspect ratio with padding-bottom trick
/* 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 */
}
Modern CSS has aspect-ratio
The padding-bottom hack was the go-to solution for years, but CSS now has the 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:

Button sizes
Small — 6px 12px Medium — 10px 20px Large — 14px 28px
Card with sections
Card Title
Body uses 14px 16px padding. Enough breathing room without being spacious.
Sidebar navigation
Input field sizes
CSS — Patterns code
/* ── 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
CSS — Logical padding properties
/* 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 */
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
One printable page covering every CSS3 property including all box model, padding, margin and layout properties.
Download PDF

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.

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 12

Questions & Answers
The seven key differences:

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.
The 2-value shorthand sets:

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.
With the default 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.
All percentage padding values — including 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.
Logical properties replace physical direction names with writing-mode-aware names:

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.
Multiple Choice Questions
Question 1 of 5
Where is CSS padding located relative to the border?
Question 2 of 5
Which statement about padding is TRUE?
Question 3 of 5
An element has width: 200px; padding: 20px with box-sizing: border-box. What is the content area width?
Question 4 of 5
What percentage value creates a 16:9 aspect ratio using the padding-bottom trick?
Question 5 of 5
Which logical property is the shorthand for padding-left + padding-right?
Chapter Summary
  • 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 as margin.
  • With box-sizing: border-box, padding is carved from the declared width. With content-box (default), padding is added on top. Always use the universal border-box reset.
  • 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/9 is 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 consistent padding-block / padding-inline.
  • Logical properties: padding-block (top + bottom), padding-inline (left + right, RTL-aware).
~5 min read
Beginner level
Chapter 12 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel