Ch 15 / 45
Chapter 15 Beginner 5 min read Tailwind CSS

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.

Advertisement
Google Ad Placeholder · 728×90 Leaderboard
What you'll learn
How padding differs from margin — inner vs outer spacing
Apply padding to all sides, axes, and individual sides with p-*, px-*, py-*
Understand Tailwind's 4px spacing scale and the formula behind every value
Use responsive padding prefixes — different spacing at each breakpoint
Apply RTL-safe logical padding with ps-* and pe-*
Use arbitrary values and real-world padding patterns for cards, buttons, and containers

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.

CSS Box Model — padding lives inside the border MARGIN — always transparent, outside the border border padding (background fills here) padding Content Area text, images, child elements pt-* pb-* pl-* pr-* Background colour extends into padding • Clicking padding triggers the element's event handler
Inside the Border
Padding is space inside the element. It grows the element's size while keeping content centred. The background colour always fills into padded space.
Background Fills
Unlike margin, padding is not transparent. A bg-blue-500 card with p-6 means the blue background extends 24px in from every edge.
Bigger Click Target
Padding on buttons and links enlarges the clickable hit area without changing the text size. px-6 py-3 on a small button makes it far easier to tap on mobile.
Never Collapses
Unlike vertical margin which collapses between block elements, padding never collapses — the space you set is always the space you get, in all layout contexts.
Rule of Thumb Use padding when you want space inside a component — between content and the component's boundary. Use margin when you want space between components. A simple test: if the background colour should cover the space, use padding. If the space should be empty, use margin.
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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.

Padding utility prefixes — which sides each class targets p-* all 4 sides px-* left + right py-* top + bottom pt-* top only pr-* right only pb-* bottom only pl-* left only Formula: class number × 4 = pixels • p-4 = 16px • p-8 = 32px • p-16 = 64px
p-*
padding: N — Sets equal padding on all four sides simultaneously. p-4 = 16px top, right, bottom, left. The most common pattern for card and section inner spacing.
px-*
padding-left + padding-right — Horizontal axis only (x). px-6 adds 24px of breathing room on the left and right. Common for container gutters and button horizontal padding.
py-*
padding-top + padding-bottom — Vertical axis only (y). py-4 adds 16px above and below the content. Common for list items, navigation links, and section spacing.
pt-* / pb-*
padding-top / padding-bottom — Individual top or bottom padding. pt-8 pushes content down from the top edge. Use when only one vertical side needs adjustment.
pr-* / pl-*
padding-right / padding-left — Individual right or left padding. pl-4 adds left indent. Used for icon offsets, indented text, and asymmetric button layouts.
p-0
padding: 0 — Removes all padding. Useful for resetting browser defaults on headings, paragraphs, and lists that inherit default padding.
p-px
padding: 1px — Exactly one pixel of padding. Useful for micro-spacing like keeping a border-only button from collapsing to zero height.
HTML — all padding prefixes in action
<!-- 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].

Tailwind 4px spacing scale — p-* class × 4 = pixels p-0 0px p-1 4px p-2 8px p-3 12px p-4 ★ 16px most common p-6 ★ 24px card padding p-8 ★ 32px section padding p-16 64px large hero sections Formula N × 4 = px p-1 → 1 × 4 = 4px p-4 → 4 × 4 = 16px p-8 → 8 × 4 = 32px p-[13px] → arbitrary value rem = px ÷ 16 (base font-size)
p-1
0.25rem
4px
p-2
0.5rem
8px
p-4
1rem
16px ★
p-6
1.5rem
24px ★
p-8
2rem
32px ★
p-[13px]
arbitrary
exactly 13px
HTML — spacing scale in practice
<!-- 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.

class="p-4 sm:p-6 md:p-8 lg:p-12 xl:p-16" — padding scales with screen width mobile < 640px p-4 16px sm: ≥ 640px sm:p-6 24px md: ≥ 768px md:p-8 32px lg: ≥ 1024px lg:p-12 48px xl: ≥ 1280px xl:p-16 64px Mobile-first: unprefixed = all sizes • prefixed = that size and wider
HTML — responsive padding patterns
<!-- 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>
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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.

ps-*
padding-inline-start — Maps to padding-left in LTR (English, French, Hindi) and padding-right in RTL (Arabic, Hebrew). The safe replacement for pl-* in multilingual apps.
pe-*
padding-inline-end — Maps to 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.
pl-* / pr-*
Physical properties — Always mean left and right regardless of document direction. Fine for decorative elements where direction doesn't matter, but avoid for text-adjacent spacing in multilingual apps.
dir="rtl"
HTML direction attribute — Add dir="rtl" to <html> or a container to switch layout direction. Logical property classes (ps-*, pe-*) automatically adapt. Physical ones (pl-*, pr-*) do not.
HTML — logical padding (RTL-safe)
<!-- 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>
Note Tailwind also has 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.
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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.

HTML — 6 real-world padding patterns
<!-- 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>
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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

Featured Course
Master Tailwind CSS — Spacing, Layout & Production UI
Go from spacing basics to building pixel-perfect dashboards and landing pages. Our structured course covers all Tailwind v4 utilities, custom themes, and 5 real-world projects with live mentoring.
45 Chapters 10+ Hours 5 Real Projects Certificate Live Mentoring
📏
⭐ 4.9 · 1,200+ students
Tailwind CSS Padding Cheat Sheet
Download our free one-page cheat sheet covering all Tailwind CSS padding utilities — p, px, py, pt, pr, pb, pl, logical ps and pe, the 4px spacing scale, and responsive patterns with visual examples.
p-* px-* py-* pt-* pb-* pl-* pr-* ps-* pe-* 4px Scale Responsive Arbitrary Values
Download Free Cheat Sheet
Chapter Summary
  • 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 sides pt-* 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-3 is 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-8 gives tight gutters on mobile and generous ones on desktop, without writing a single media query.
  • Logical properties — Prefer ps-* over pl-* and pe-* over pr-* 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-4 work.

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.

Question 1 of 4
Which Tailwind class adds exactly 16px of padding on all four sides?
Question 2 of 4
Which class adds padding only on the left and right (horizontal axis)?
Question 3 of 4
In Tailwind's spacing scale, how many pixels does p-8 produce?
Question 4 of 4
Which Tailwind class is the RTL-safe alternative to pl-4?

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