Tailwind CSS Tutorial menu

Tailwind CSS Spacing and Sizing

Spacing and sizing classes use the same number scale, so space and size in your design always fit together.

Padding

Padding classes start with p. A second letter picks the sides.

ClassSides
p-4All sides
px-4Left and right (x axis)
py-4Top and bottom (y axis)
pt-4, pr-4, pb-4, pl-4Top, right, bottom, left

Margin

Margin classes work the same way with m: m-4, mx-4, my-4, mt-4 and so on. mx-auto centres a block that has a width.

Example

html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="bg-gray-100">

<div class="bg-white max-w-md mx-auto mt-10 p-8 rounded-lg shadow-md">
  <h2 class="text-xl font-bold mb-4">Student Login</h2>
  <input class="w-full border border-gray-300 rounded-md px-3 py-2 mb-3" placeholder="Roll number">
  <input class="w-full border border-gray-300 rounded-md px-3 py-2 mb-5" type="password" placeholder="Password">
  <button class="w-full bg-cyan-600 text-white py-2 rounded-md">Log in</button>
</div>

</body>
</html>
Try it Yourself »

Space Between Children

space-y-4 adds space between stacked children, and space-x-4 adds it between children in a row. It does not add space before the first child or after the last.

Example

html
<ul class="space-y-2">
  <li>Ali</li>
  <li>Ayesha</li>
  <li>Bilal</li>
</ul>

Inside flex and grid layouts, gap-4 does the same job and is usually the better choice.

Width

ClassWidth
w-164rem (64px), from the spacing scale
w-1/2, w-1/3, w-2/3Fractions of the parent
w-full100% of the parent
w-screenThe full screen width
w-autoThe browser decides
w-fitAs wide as the content

Height

Height uses the same values with h-: h-16, h-full, h-screen. min-h-screen is very common; it makes a page at least as tall as the screen.

Maximum Width

max-w-* classes stop content getting too wide on big screens.

ClassMax width
max-w-sm24rem (384px)
max-w-md28rem (448px)
max-w-xl36rem (576px)
max-w-4xl56rem (896px)
max-w-proseAbout 65 characters, good for text

Example

html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="p-6 space-y-3">

<div class="w-1/4 bg-cyan-200 p-2">w-1/4</div>
<div class="w-1/2 bg-cyan-300 p-2">w-1/2</div>
<div class="w-3/4 bg-cyan-400 p-2">w-3/4</div>
<div class="w-full bg-cyan-500 p-2 text-white">w-full</div>
<div class="w-32 h-32 bg-amber-300 rounded-full flex items-center justify-center">w-32 h-32</div>

</body>
</html>
Try it Yourself »

Size: Width and Height Together

size-12 sets both width and height to 3rem. It is a quick way to make squares and circles, such as avatars and icons.

Think in the scale: 2, 4, 6, 8, 12, 16. If you always pick from these few numbers, your page will have a calm, even rhythm.

Exercise

Exercise

Which class adds padding only on the left and right?

Learn Tailwind CSS with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp