Padding in Tailwind CSS
Padding is the space inside an element — between
the content and its border. Unlike margin (which pushes elements
apart), padding expands the element itself. Its background colour
fills into the padded area, and it increases the clickable region
of buttons and links. Tailwind maps every padding direction to a
single intuitive class:
p-* for all sides,
px-* /
py-* for axes, and
pt-* /
pr-* /
pb-* /
pl-* for individual sides —
all built on the same 4px spacing scale.
p-*, px-*, py-*
ps-* and
pe-*
What is Padding? — Inside vs Outside
The CSS box model places every element inside four layers: content, padding, border, and margin. Padding lives between the content and the border — it is inside the element. This means the element's background colour fills into the padded area, and clicking anywhere in the padding still triggers the element's click handler.
bg-blue-500 card with
p-6 means the blue
background extends 24px in from every edge.
px-6 py-3 on a small
button makes it far easier to tap on mobile.
All Padding Utilities — Complete Prefix Reference
Tailwind provides 8 padding prefixes. They all follow the same
pattern:
p = padding, then the axis or
direction shorthand, then the spacing scale value. The values map
directly to the 4px scale — multiply the number by 4 to get
pixels: p-4 = 16px,
p-8 = 32px,
p-16 = 64px.
p-4 = 16px top, right,
bottom, left. The most common pattern for card and section
inner spacing.
px-6 adds 24px of breathing room
on the left and right. Common for container gutters and button
horizontal padding.
py-4 adds 16px above and below the
content. Common for list items, navigation links, and section
spacing.
pt-8 pushes content down from
the top edge. Use when only one vertical side needs
adjustment.
pl-4 adds left indent.
Used for icon offsets, indented text, and asymmetric button
layouts.
<!-- p-* : equal padding on all 4 sides -->
<div class="p-6 bg-blue-900 rounded-xl">
24px space on every edge
</div>
<!-- px-* / py-* : axis shorthand -->
<button class="px-6 py-3 bg-cyan-500 text-white rounded-lg">
24px left/right, 12px top/bottom
</button>
<!-- pt-* / pb-* : individual vertical sides -->
<section class="pt-16 pb-12 px-4">
64px top, 48px bottom, 16px horizontal
</section>
<!-- pl-* for icon + text alignment -->
<div class="flex items-center pl-10 py-3 bg-gray-800 rounded-lg relative">
<iconify-icon icon="solar:search-linear" class="absolute left-3"></iconify-icon>
<input type="text" placeholder="Search..." class="bg-transparent outline-none w-full">
</div>
<!-- p-0 : remove default browser padding -->
<ul class="p-0 list-none">
<li class="px-4 py-2 border-b border-gray-700">Clean list item</li>
</ul>
The 4px Spacing Scale — The Formula Behind Every Value
Every padding (and margin) value in Tailwind follows a single
predictable rule:
class number × 4 = pixels. This is the 4px
spacing scale. Once you learn it, you can guess any value
instantly. p-1 = 4px,
p-4 = 16px,
p-8 = 32px,
p-16 = 64px. Need something
off-scale? Use an arbitrary value:
p-[13px].
<!-- Spacing scale quick reference -->
p-1 = 4px | p-2 = 8px | p-3 = 12px
p-4 = 16px | p-5 = 20px | p-6 = 24px
p-8 = 32px | p-10 = 40px | p-12 = 48px
p-16 = 64px | p-20 = 80px | p-24 = 96px
<!-- Arbitrary value for pixel-perfect alignment -->
<div class="p-[13px]">Exactly 13px padding</div>
<div class="px-[1.75rem]">28px horizontal padding</div>
Responsive Padding — Different Spacing at Every Breakpoint
Every padding class accepts responsive breakpoint prefixes.
Tailwind is mobile-first: an unprefixed class
applies from 0px up. A prefixed class like
md:p-8 applies
at and above the medium breakpoint (768px). Stack
multiple prefixes to step the spacing up as the screen grows — a
pattern used on every well-built page section.
<!-- Hero section: small padding mobile, large on desktop -->
<section class="px-4 py-12 sm:px-6 sm:py-16 lg:px-8 lg:py-24">
Scales from compact (mobile) to spacious (desktop)
</section>
<!-- Container: tight padding on mobile, generous on desktop -->
<div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8">
Page content
</div>
<!-- Card: uniform on mobile, more room on larger screens -->
<div class="p-4 sm:p-6 lg:p-8 bg-white rounded-2xl shadow-md">
Card content
</div>
<!-- Button: compact on mobile, padded on desktop -->
<button class="px-4 py-2 sm:px-6 sm:py-3 bg-cyan-500 text-white rounded-xl font-semibold">
Enroll Now
</button>
Logical Properties — ps-* & pe-* for RTL Layouts
Physical properties like
pl-* and
pr-*
always mean left and right — they don't flip in right-to-left
(RTL) layouts (Arabic, Hebrew, Urdu).
Logical properties solve this:
ps-* =
padding-inline-start (left in LTR, right in RTL) and
pe-* =
padding-inline-end (right in LTR, left in RTL). If your
site needs to support multiple language directions, prefer
ps-* over
pl-* everywhere.
padding-left in LTR (English, French, Hindi) and
padding-right in RTL (Arabic, Hebrew). The safe
replacement for pl-* in multilingual apps.
padding-right in LTR and
padding-left in RTL. The safe replacement for
pr-*. Use pe-2 instead of
pr-2 for icon spacing after text in multilingual
UIs.
dir="rtl" to <html> or a
container to switch layout direction. Logical property classes
(ps-*, pe-*) automatically adapt.
Physical ones (pl-*, pr-*) do not.
<!-- Icon + label: ps flips automatically in RTL -->
<button class="flex items-center gap-2 px-4 py-2 bg-cyan-600 text-white rounded-lg">
<iconify-icon icon="solar:arrow-right-linear"></iconify-icon>
<span class="ps-1">Next Chapter</span>
<!-- ps-1 = 4px left in LTR, 4px right in RTL -->
</button>
<!-- Navigation item with logical indent -->
<nav dir="rtl">
<ul class="p-0 list-none">
<li class="ps-4 pe-2 py-3 border-b border-gray-700">
الصفحة الرئيسية
<!-- ps-4 = padding-right: 16px in RTL (the "start" side) -->
</li>
</ul>
</nav>
pbs-* (padding-block-start,
equivalent to pt-* in horizontal writing modes) and
pbe-* (padding-block-end, equivalent to
pb-*). These are rarely needed in standard web
layouts but are useful for vertical text modes.
Real-World Padding Patterns
Padding is the most frequently written utility in any Tailwind project. These are the patterns you'll use on every page — card inner spacing, button sizing, container gutters, form fields, and hero sections.
<!-- 1. Card with generous inner padding -->
<div class="p-6 bg-white dark:bg-gray-800 rounded-2xl shadow-md">
<h3 class="text-lg font-bold mb-2">Course Title</h3>
<p class="text-gray-500 text-sm">Description text</p>
</div>
<!-- 2. Primary button (thumb-friendly on mobile) -->
<button class="px-6 py-3 bg-cyan-500 hover:bg-cyan-600 text-white
font-semibold rounded-xl transition-colors">
Enroll Now
</button>
<!-- 3. Small badge / tag -->
<span class="px-2.5 py-0.5 bg-green-100 text-green-800
text-xs font-medium rounded-full">
Beginner
</span>
<!-- 4. Responsive page container -->
<div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
Page content — tighter gutter on mobile, wider on desktop
</div>
<!-- 5. Input field with icon inside -->
<div class="relative">
<iconify-icon icon="solar:user-linear"
class="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400"></iconify-icon>
<input type="text"
class="pl-10 pr-4 py-2.5 border border-gray-300 rounded-lg
w-full outline-none focus:ring-2 focus:ring-cyan-400"
placeholder="Your name">
</div>
<!-- 6. Hero section with asymmetric padding -->
<section class="pt-20 pb-12 px-4 sm:pt-28 sm:pb-20 lg:pt-36 lg:pb-28">
<h1 class="text-4xl font-bold">Learn Tailwind CSS</h1>
</section>
Quick Reference — All Padding Utilities
| Class | CSS Property | Notes |
|---|---|---|
p-0 |
padding: 0 |
Remove all padding |
p-px |
padding: 1px |
1px on all sides |
p-0.5 |
padding: 0.125rem |
2px — micro spacing |
p-1 … p-96 |
padding: N × 0.25rem |
4px scale ★ |
p-[13px] |
Custom value | Arbitrary: any CSS length |
px-* |
padding-left + padding-right |
Horizontal axis ★ |
py-* |
padding-top + padding-bottom |
Vertical axis ★ |
pt-* |
padding-top |
Top only ★ |
pr-* |
padding-right |
Right only |
pb-* |
padding-bottom |
Bottom only ★ |
pl-* |
padding-left |
Left only ★ |
ps-* |
padding-inline-start |
RTL-safe start (replaces pl-*) ★ |
pe-* |
padding-inline-end |
RTL-safe end (replaces pr-*) |
sm:p-* |
At ≥640px | Responsive prefix ★ |
md:p-* |
At ≥768px | Responsive prefix ★ |
lg:p-* |
At ≥1024px | Responsive prefix ★ |
xl:p-* |
At ≥1280px | Responsive prefix |
2xl:p-* |
At ≥1536px | Responsive prefix |
★ = most commonly used in real projects
- Padding is inner space — it lives between the content and the border. Unlike margin, the element's background colour fills into padded area and clicks inside the padding trigger the element's event handler.
-
8 prefixes, 1 formula —
p-*(all),px-*(horizontal),py-*(vertical), plus individual sidespt-*pr-*pb-*pl-*. Formula: class number × 4 = pixels. -
The 4px scale —
p-1 = 4px,p-4 = 16px,p-6 = 24px,p-8 = 32px. The most common values in real projects are p-2, p-3, p-4, p-6, and p-8. -
Axis shorthand —
px-6 py-3is the standard button padding pattern: 24px horizontal, 12px vertical. It's shorter and cleaner than writing four separate sides. -
Responsive padding — Prefix any class with a
breakpoint:
px-4 sm:px-6 lg:px-8gives tight gutters on mobile and generous ones on desktop, without writing a single media query. -
Logical properties — Prefer
ps-*overpl-*andpe-*overpr-*in multilingual apps. They automatically flip in RTL layouts without any extra code. -
Arbitrary values — When the scale doesn't fit:
p-[13px],px-[1.75rem],pt-[5%]. Any valid CSS length works inside the square brackets. -
Next chapter covers Margin — the outer spacing
utility. Learn how
m-*,mx-auto,mt-auto, and negative margins like-mt-4work.
Quick Check — Questions & Answers
Test your understanding of this chapter. Click a question to reveal the answer.
Padding is space inside the element, between the content and the border. The element's background colour extends into padding. Clicking in the padded area triggers the element's events.
Margin is space outside the element, between it and surrounding elements. Margin is always transparent — the background doesn't fill it. Clicking in margin does nothing.
The quick rule: if the background should cover the space, use padding. If the space should be invisible between elements, use margin.
It depends on the box-sizing property. By
default, Tailwind (via its Preflight reset) applies
box-sizing: border-box to all elements. This
means padding is included in the declared
width.
Example: a w-64 (256px wide) div with
px-6 (24px each side) will still be exactly
256px wide — the content area shrinks to 208px to
accommodate the padding. The total size doesn't grow.
Without border-box (old CSS default), padding
would be added on top of the declared width, making layout
calculations much harder. This is why Tailwind's reset is
so useful.
Use px-* whenever you want the
same value on both the left and right
sides. It's shorter and reduces the chance of accidentally
using different values. The most common patterns are
px-4 for mobile containers and
px-6 py-3 for buttons.
Use individual pl-* and
pr-* only when you need
different values on each side — for
example, a search input with pl-10 (to make
room for an icon) and pr-4 (normal right
padding).
ps-* sets
padding-inline-start and
pe-* sets padding-inline-end.
These are logical properties that adapt
to the document's text direction.
In a left-to-right layout (English, French, Hindi):
ps-4 = left padding, pe-4 =
right padding — exactly like pl-4 and
pr-4.
In a right-to-left layout (Arabic, Hebrew, set with
dir="rtl"): ps-4 =
right padding (the start side in RTL),
pe-4 = left padding — they flip
automatically.
Use them in any project that supports multiple languages.
If you only build LTR sites, pl-* /
pr-* are fine.
Stack responsive prefixes on the class. Tailwind is mobile-first, so the unprefixed class applies from 0px up, and prefixed classes kick in at that breakpoint and above:
<section class="px-4 py-8 sm:px-6 sm:py-12 lg:px-8
lg:py-20">
This gives px-4 py-8 on mobile,
px-6 py-12 from 640px, and
px-8 py-20 from 1024px. No media queries, no
CSS file, just class names. The breakpoints are: sm
(640px), md (768px), lg (1024px), xl (1280px), 2xl
(1536px).
MCQ Quiz — Test Yourself
Select the best answer for each question. Correct answers are revealed after you click.
p-8
produce?
pl-4?
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.