Tailwind Spacing

Padding, margin and space between.

✍️ 3 exercises + code challenge in this lesson
  • Padding: p-4, px-6 (left/right), py-2 (top/bottom), pt-8…
  • Margin: m-4, mx-auto (centre), mt-6, negative -mt-2
  • Space between children: space-y-4, space-x-2

The scale is based on 0.25rem: 1 = 4px, 4 = 16px, 8 = 32px.

Key Takeaways: Tailwind Spacing

Mastering Tailwind Spacing helps you write clean and reliable Tailwind CSS code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Tailwind Spacing Code Example

Tailwind CSS
<script src="https://cdn.tailwindcss.com"></script>

<div class="max-w-sm mx-auto mt-6 space-y-3">
  <div class="p-2 bg-cyan-100">p-2</div>
  <div class="px-8 py-4 bg-cyan-200">px-8 py-4</div>
  <div class="p-6 bg-cyan-300">p-6</div>
</div>
Free sign-in required · Google login available

Tailwind Spacing Practice Exercises

Check what you learned in “Tailwind Spacing”.

/ 3 solved
Fill in the blank

Centre a block horizontally.

<div class="max-w-md ">...</div>
✓ Correct! Not quite. Try again.
Multiple choice

How many pixels is p-4 by default?

✓ Correct! Not quite. Try again.
Code challenge

Give the box padding p-6 and margin-top mt-4.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services