Margin utilities help you add space outside elements easily. This keeps your design neat and readable.
Margin utilities in Tailwind
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Tailwind
m-{size} // sets margin on all sides
mt-{size} // margin-top
mr-{size} // margin-right
mb-{size} // margin-bottom
ml-{size} // margin-left
mx-{size} // margin-left and margin-right
my-{size} // margin-top and margin-bottomReplace {size} with numbers like 0, 1, 2, 4, 8, etc., which correspond to spacing values.
Use mx and my to set horizontal or vertical margins quickly.
Examples
Tailwind
m-4
Tailwind
mt-2
Tailwind
mx-6
Tailwind
mb-0
Sample Program
This page shows four colored boxes with different margin utilities applied. You can see how space changes around each box.
Tailwind
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Margin Utilities Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <div class="bg-blue-200 p-4 m-4">Box with m-4 (margin all sides)</div> <div class="bg-green-200 p-4 mt-2">Box with mt-2 (margin top only)</div> <div class="bg-red-200 p-4 mx-6">Box with mx-6 (margin left and right)</div> <div class="bg-yellow-200 p-4 mb-0">Box with mb-0 (no bottom margin)</div> </body> </html>
Important Notes
Tailwind margin sizes are based on a scale, usually multiples of 0.25rem (4px).
Negative margins are possible using -m-4 to pull elements closer.
Use browser DevTools to inspect margin spacing visually.
Summary
Margin utilities add space outside elements quickly.
Use m, mt, mr, mb, ml, mx, and my for different sides.
Sizes control how much space is added, making layout easy and consistent.
Practice
1. What does the Tailwind CSS class
m-4 do to an element?easy
Solution
Step 1: Understand Tailwind margin scale
In Tailwind,m-4means margin with a spacing scale value of 4, which equals 1rem (16px by default).Step 2: Differentiate margin and padding
The prefixm-sets margin, not padding. Padding usesp-.Final Answer:
Adds margin of 1rem on all sides of the element -> Option AQuick Check:
m-4 = margin 1rem all sides [OK]
Hint: m-4 means margin with spacing scale 4 = 1rem all sides [OK]
Common Mistakes:
- Confusing margin (m-) with padding (p-)
- Thinking m-4 means 4px instead of 1rem
- Assuming it applies only to one side
2. Which Tailwind class correctly adds a margin of 2rem only to the top of an element?
easy
Solution
Step 1: Recall Tailwind margin top syntax
Tailwind usesmt-prefix for margin-top, followed by the spacing scale number.Step 2: Identify correct class format
mt-8is valid and means margin-top of 2rem. Other options are invalid syntax.Final Answer:
mt-8 -> Option CQuick Check:
mt-8 = margin-top 2rem [OK]
Hint: Use mt-8 for margin-top 2rem, no extra dashes [OK]
Common Mistakes:
- Using incorrect dash order like m-t-8
- Writing full words like margin-top-8
- Mixing property and value order
3. What margin will the element have if it uses the class
mx-6 my-2 in Tailwind CSS?medium
Solution
Step 1: Decode mx-6 and my-2 classes
mx-6sets horizontal margins (left and right) to spacing scale 6 = 1.5rem.my-2sets vertical margins (top and bottom) to spacing scale 2 = 0.5rem.Step 2: Combine horizontal and vertical margins
So left and right margins are 1.5rem, top and bottom margins are 0.5rem.Final Answer:
Margin-left and margin-right 1.5rem; margin-top and margin-bottom 0.5rem -> Option AQuick Check:
mx-6 = 1.5rem horizontal, my-2 = 0.5rem vertical [OK]
Hint: mx sets horizontal, my sets vertical margins with scale values [OK]
Common Mistakes:
- Mixing horizontal and vertical values
- Assuming mx-6 means 6rem margin
- Thinking mx-6 and my-2 override each other
4. You want to add margin only to the bottom of an element using Tailwind CSS, but your code
mb8 does not work. What is the correct fix?medium
Solution
Step 1: Identify Tailwind margin bottom syntax
Tailwind margin bottom classes require a dash between prefix and scale number, likemb-8.Step 2: Correct the missing dash error
The codemb8is invalid because it lacks the dash. Adding the dash fixes the syntax.Final Answer:
Changemb8tomb-8-> Option DQuick Check:
Margin bottom class needs dash: mb-8 [OK]
Hint: Always use dash between property and value in Tailwind classes [OK]
Common Mistakes:
- Omitting dash between prefix and number
- Using invalid class formats like m-b-8
- Trying full CSS property names as classes
5. You want to create a responsive layout where an element has no margin on small screens, but margin of 3rem on the left and right on medium screens and above. Which Tailwind classes achieve this?
hard
Solution
Step 1: Understand responsive prefix usage
In Tailwind,md:prefix applies styles at medium screen sizes and above.Step 2: Apply zero margin on all sides for small screens
m-0sets margin 0 on all sides for small screens.Step 3: Apply horizontal margin 3rem on medium screens
mx-12sets horizontal margin to spacing scale 12 = 3rem. Usingmd:mx-12applies this only on medium+ screens.Final Answer:
m-0 md:mx-12 -> Option BQuick Check:
m-0 no margin small, md:mx-12 horizontal 3rem medium+ [OK]
Hint: Use m-0 for small, md:mx-12 for medium+ horizontal margin [OK]
Common Mistakes:
- Using invalid units like 3rem directly in class
- Applying margin on all sides instead of horizontal only
- Mixing up responsive prefixes and margin directions
