Padding utilities add space inside an element, between its content and its border. This helps make things look neat and easy to read.
Padding utilities in Tailwind
Start learning this pattern below
Jump into concepts and practice - no test required
p-{size} // padding all sides
pt-{size} // padding top
pr-{size} // padding right
pb-{size} // padding bottom
pl-{size} // padding left
px-{size} // padding left and right
py-{size} // padding top and bottom{size} is a number or keyword like 0, 1, 2, 3, 4, 5, 6, 8, 10, etc., representing spacing steps.
Use px and py to add horizontal or vertical padding quickly.
p-4
pt-2
px-6
py-3
This page shows a white box with some text and a button. The paragraph uses py-2 and px-4 to add vertical and horizontal padding inside it. The button uses px-6 and py-2 to make the clickable area bigger and comfortable.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Padding Utilities Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="bg-gray-100 flex items-center justify-center min-h-screen"> <div class="bg-white border rounded shadow p-6 max-w-sm"> <h1 class="text-xl font-bold mb-4">Hello, Tailwind!</h1> <p class="py-2 px-4 bg-blue-100 rounded">This paragraph has vertical padding of 2 and horizontal padding of 4.</p> <button class="mt-4 bg-blue-500 text-white rounded px-6 py-2 hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-400"> Click me </button> </div> </body> </html>
Padding sizes in Tailwind are based on a scale, usually multiples of 0.25rem (4px). For example, p-4 equals 1rem (16px).
Use responsive prefixes like md:p-6 to change padding on different screen sizes.
Combine padding utilities with margin utilities to control spacing outside elements.
Padding utilities add space inside elements to improve layout and readability.
Use p-, pt-, pr-, pb-, pl-, px-, and py- to control padding on different sides.
They help create neat, comfortable designs without writing custom CSS.
Practice
p-4 do to an element?Solution
Step 1: Understand the
Thep-prefixp-prefix in Tailwind applies padding to all four sides of an element equally.Step 2: Interpret the number 4 in
The number 4 corresponds to 1rem (16px by default) padding size in Tailwind's spacing scale.p-4Final Answer:
Adds equal padding of size 1rem on all sides of the element -> Option AQuick Check:
p-4means padding all sides 1rem [OK]
- Confusing p-4 with padding only on one side
- Thinking p-4 means 4 pixels instead of 1rem
- Mixing p-4 with px-4 or py-4
Solution
Step 1: Identify the prefix for left padding
In Tailwind,pl-stands for padding-left, so it adds padding only to the left side.Step 2: Confirm the number usage
The number 6 applies a padding size of 1.5rem (24px) on the left side.Final Answer:
pl-6 -> Option DQuick Check:
pl-means padding-left only [OK]
- Choosing px-6 which adds padding left and right
- Confusing pt-6 (top padding) with left padding
- Using pb-6 which adds padding bottom
<div class="py-2 px-4">Hello</div>
Solution
Step 1: Understand
py-2andpx-4py-2adds padding top and bottom of 0.5rem,px-4adds padding left and right of 1rem.Step 2: Combine the effects
The div will have vertical padding 0.5rem and horizontal padding 1rem, creating comfortable space inside.Final Answer:
Adds vertical padding of 0.5rem and horizontal padding of 1rem inside the div -> Option CQuick Check:
py-2= vertical 0.5rem,px-4= horizontal 1rem [OK]
- Thinking py-2 adds padding on all sides
- Confusing px-4 with padding only on one side
- Assuming padding values are in pixels
<div class="pt-3 pl-5 pb-2 px-4">Content</div>
Solution
Step 1: Analyze horizontal padding classes
pl-5adds padding-left 1.25rem,px-4adds padding-left and right 1rem. These overlap and conflict on left padding.Step 2: Understand conflict effect
Becausepx-4sets left padding to 1rem, it overridespl-5on the left side, causing confusion or unexpected padding.Final Answer:
Using bothpl-5andpx-4causes conflicting horizontal padding -> Option BQuick Check:
Don't combine specific side and axis padding on same side [OK]
- Thinking pt-3 is invalid
- Believing pb-2 conflicts with px-4
- Assuming no conflict when combining pl- and px-
Solution
Step 1: Understand spacing scale for rem values
In Tailwind, 1rem = 4 units, so 2rem = 8 units, 1rem = 4 units.Step 2: Match padding classes to desired sizes
pt-8andpb-8add 2rem padding top and bottom;pl-4andpr-4add 1rem padding left and right.Step 3: Check other options
py-8 p-4conflicts becausep-4overrides vertical padding to 1rem;p-4 px-8results in top/bottom 1rem and left/right 2rem;py-4 px-8reverses sizes.Final Answer:
pt-8 pb-8 pl-4 pr-4 -> Option AQuick Check:
Use side-specific classes for precise padding [OK]
- Using p-8 overrides all sides with 2rem
- Mixing px and p classes causing conflicts
- Swapping vertical and horizontal padding values
