0
0
Tailwindmarkup~5 mins

Padding utilities in Tailwind

Choose your learning style9 modes available
Introduction

Padding utilities add space inside an element, between its content and its border. This helps make things look neat and easy to read.

To create space inside buttons so text doesn't touch edges.
To add breathing room inside cards or boxes for better layout.
To separate text from images inside a container.
To make clickable areas bigger and easier to tap on mobile.
To adjust spacing quickly without writing custom CSS.
Syntax
Tailwind
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.

Examples
Adds padding of size 4 on all four sides of the element.
Tailwind
p-4
Adds padding only on the top side with size 2.
Tailwind
pt-2
Adds padding on left and right sides with size 6.
Tailwind
px-6
Adds padding on top and bottom sides with size 3.
Tailwind
py-3
Sample Program

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.

Tailwind
<!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>
OutputSuccess
Important Notes

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.

Summary

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.