Margin In Tailwind CSS

Tailwind Margin allows you to add space around elements without having to write custom CSS. In this article, we will explore Tailwind margin and how it can be used to create effective layouts with examples.

Tailwind Margin Classes

Margin is a CSS property that creates space around an element.

It determines the amount of space between an element’s border and adjacent elements in the layout.

You can set the margin property for each side of the element individually (top, right, bottom, and left), or for all sides together.



Tailwind Margin Classes

Tailwind margin property can be set specifying a various classes, each with a specific value.

ClassesOverview
m-0This class is used to set the margin on all sides of an element to 0.
mx-0This class is used to set the horizontal margin of an element to 0.
my-0This class is used to set the vertical margin of an element to 0.
mt-0This class is used to set the top margin of an element to 0.
mr-0This class is used to set the right margin of an element to 0.
mb-0This class is used to set the bottom margin of an element to 0.
ml-0This class is used to set the left margin of an element to 0.
m-pxUsing this class ,the margin on all sides is set to 1px.
mx-pxUsing this class ,the horizontal margin is set to 1px.
my-pxUsing this class ,the vertical margin on is set to 1px.
mt-pxUsing this class ,the top margin is set to 1px.
mr-pxUsing this class ,the right margin is set to 1px.
mb-pxUsing this class ,the bottom margin is set to 1px.
ml-pxUsing this class ,the left margin is set to 1px.
m-0.5The margin on all sides is set to 0.125rem or 2px.
mx-0.5The horizontal margin is set to 0.125rem or 2px.
my-0.5The vertical margin is set to 0.125rem or 2px.
mt-0.5The top margin is set to 0.125rem or 2px.
mr-0.5The right margin is set to 0.125rem or 2px.
mb-0.5The bottom margin is set to 0.125rem or 2px.
ml-0.5The left margin is set to 0.125rem or 2px.
m-1The margin on all sides is set to 0.25rem or 4px.
mx-1The horizontal margin is set to 0.25rem or 4px.
my-1The vertical margin is set to 0.25rem or 4px.
mt-1The top margin is set to 0.25rem or 4px.
mr-1The right margin is set to 0.25rem or 4px.
mb-1The bottom margin is set to 0.25rem or 4px.
ml-1The left margin is set to 0.25rem or 4px.
m-1.5The margin on all sides is set to 0.375rem or 6px.
mx-1.5The horizontal margin is set to 0.375rem or 6px.
my-1.5The vertical margin is set to 0.375rem or 6px.
mt-1.5The top margin is set to 0.375rem or 6px.
mr-1.5The right margin is set to 0.375rem or 6px.
mb-1.5The bottom margin is set to 0.375rem or 6px.
ml-1.5The left margin is set to 0.375rem or 6px.
m-2The margin on all sides is set to 0.5rem or 8px.
mx-2The horizontal margin is set to 0.5rem or 8px.
my-2The vertical margin is set to 0.5rem or 8px.
mt-2The top margin is set to 0.5rem or 8px.
mr-2The right margin is set to 0.5rem or 8px.
mb-2The bottom margin is set to 0.5rem or 8px.
ml-2The left margin is set to 0.5rem or 8px.
m-2.5The margin on all sides is set to 0.625rem or 10px.
mx-2.5The horizontal margin is set to 0.625rem or 10px.
my-2.5The vertical margin is set to 0.625rem or 10px.
mt-2.5The top margin is set to 0.625rem or 10px.
mr-2.5The right margin is set to 0.625rem or 10px.
mb-2.5The bottom margin is set to 0.625rem or 10px.
ml-2.5The left margin is set to 0.625rem or 10px.
m-3The margin on all sides is equal to 0.75rem or 12px.
mx-3The horizontal margin is equal to 0.75rem or 12px.
my-3The vertical margin is equal to 0.75rem or 12px.
mt-3The top margin is equal to 0.75rem or 12px.
mr-3The right margin is equal to 0.75rem or 12px.
mb-3The bottom margin is equal to 0.75rem or 12px.
ml-3The left margin is equal to 0.75rem or 12px.
m-3.5The margin on all sides is specified as 0.875rem or 14px.
mx-3.5The horizontal margin is specified as 0.875rem or 14px.
my-3.5The vertical margin is specified as 0.875rem or 14px.
mt-3.5The top margin is specified as 0.875rem or 14px.
mr-3.5The right margin is specified as 0.875rem or 14px.
mb-3.5The bottom margin is specified as 0.875rem or 14px.
ml-3.5The left margin is specified as 0.875rem or 14px.
m-4The margin on all sides is defined as 1rem or 16px.
mx-4The horizontal margin is defined as 1rem or 16px.
my-4The vertical margin is defined as 1rem or 16px.
mt-4The top margin is defined as 1rem or 16px.
mr-4The right margin is defined as 1rem or 16px.
mb-4The bottom margin is defined as 1rem or 16px.
ml-4The left margin is defined as 1rem or 16px.
m-5The margin on all sides is set to 1.25rem or 20px.
mx-5The horizontal margin is set to 1.25rem or 20px.
my-5The vertical margin is set to 1.25rem or 20px.
mt-5The top margin is set to 1.25rem or 20px.
mr-5The right margin is set to 1.25rem or 20px.
mb-5The bottom margin is set to 1.25rem or 20px.
ml-5The left margin is set to 1.25rem or 20px.
m-6The margin on all sides is equal to 1.5rem or 24px.
mx-6The horizontal margin is equal to 1.5rem or 24px.
my-6The vertical margin is equal to 1.5rem or 24px.
mt-6The top margin is equal to 1.5rem or 24px.
mr-6The right margin is equal to 1.5rem or 24px.
mb-6The bottom margin is equal to 1.5rem or 24px.
ml-6The left margin is equal to 1.5rem or 24px.
m-7Margin on all sides is specified as 1.75rem or 28px.
mx-7Margin along the x-axis is specified as 1.75rem or 28px.
my-7Margin along the y-axis is specified as 1.75rem or 28px.
mt-7Margin on top is specified as 1.75rem or 28px.
mr-7Margin on right is specified as 1.75rem or 28px.
mb-7Margin on bottom is specified as 1.75rem or 28px.
ml-7Margin on left is specified as 1.75rem or 28px.
m-8Margin on all sides is defined as 2rem or 32px.
mx-8Margin along the x-axis is defined as 2rem or 32px.
my-8Margin along the y-axis is defined as 2rem or 32px.
mt-8Margin on top is defined as 2rem or 32px.
mr-8Margin on right is defined as 2rem or 32px.
mb-8Margin on bottom is defined as 2rem or 32px.
ml-8Margin on left is defined as 2rem or 32px.
m-9There is a 2.25rem or 36px margin on all sides.
mx-9There is a 2.25rem or 36px margin along the x-axis.
my-9There is a 2.25rem or 36px margin along the y-axis.
mt-9There is a 2.25rem or 36px margin on the top.
mr-9There is a 2.25rem or 36px margin on the right side.
mb-9There is a 2.25rem or 36px margin on the bottom.
ml-9There is a 2.25rem or 36px margin on the left side.
m-10The margin on all sides is equal to 2.5rem or 40px.
mx-10The margin along the x-axis is equal to 2.5rem or 40px.
my-10The margin along the y-axis is equal to 2.5rem or 40px.
mt-10The margin on the top is equal to 2.5rem or 40px.
mr-10The margin on the right is equal to 2.5rem or 40px.
mb-10The margin on the bottom is equal to 2.5rem or 40px.
ml-10The margin on the left is equal to 2.5rem or 40px.
m-11The margin on all sides is specified as 2.75rem or 44px.
mx-11The horizontal margin is specified as 2.75rem or 44px.
my-11The vertical margin is specified as 2.75rem or 44px.
mt-11The top margin is specified as 2.75rem or 44px.
mr-11The right margin is specified as 2.75rem or 44px.
mb-11The bottom margin is specified as 2.75rem or 44px.
ml-11The left margin is specified as 2.75rem or 44px.
m-12The margin on all sides is defined as 3rem or 48px.
mx-12The margin along the x-axis is defined as 3rem or 48px.
my-12The margin along the y-axis is defined as 3rem or 48px.
mt-12The margin on the top is defined as 3rem or 48px.
mr-12The margin on the right is defined as 3rem or 48px.
mb-12The margin on the bottom is defined as 3rem or 48px.
ml-12The margin on the left is defined as 3rem or 48px.
m-14This class applies a margin of 3.5rem or 56px on all sides.
mx-14This class applies a margin of 3.5rem or 56px along the x-axis.
my-14This class applies a margin of 3.5rem or 56px along the y-axis.
mt-14This class applies a margin of 3.5rem or 56px on the top.
mr-14This class applies a margin of 3.5rem or 56px on the right.
mb-14This class applies a margin of 3.5rem or 56px on the bottom.
ml-14This class applies a margin of 3.5rem or 56px on the left.
m-16A margin of 4rem or 64px is applied on all sides.
mx-16A margin of 4rem or 64px is applied along the x-axis.
my-16A margin of 4rem or 64px is applied along the y-axis.
mt-16A margin of 4rem or 64px is applied on the top.
mr-16A margin of 4rem or 64px is applied on the right.
mb-16A margin of 4rem or 64px is applied on the bottom.
ml-16A margin of 4rem or 64px is applied on the left.
m-20Used to apply a margin of 5rem or 80px on all sides.
mx-20Used to apply a margin of 5rem or 80px along the x-axis.
my-20Used to apply a margin of 5rem or 80px along the y-axis.
mt-20Used to apply a margin of 5rem or 80px on the top.
mr-20Used to apply a margin of 5rem or 80px on the right.
mb-20Used to apply a margin of 5rem or 80px on the bottom.
ml-20Used to apply a margin of 5rem or 80px on the left.
m-24This class is used to add a margin of 6rem or 96px to all sides.
mx-24This class is used to add a margin of 6rem or 96px along the x-axis..
my-24This class is used to add a margin of 6rem or 96px along the y-axis.
mt-24This class is used to add a margin of 6rem or 96px to the top.
mr-24This class is used to add a margin of 6rem or 96px to the right.
mb-24This class is used to add a margin of 6rem or 96px to the bottom.
ml-24This class is used to add a margin of 6rem or 96px to the left.
m-28Adds a margin of 7rem or 112px on all sides.
mx-28Adds a margin of 7rem or 112px along the x-axis.
my-28Adds a margin of 7rem or 112px along the y-axis.
mt-28Adds a margin of 7rem or 112px on the top.
mr-28Adds a margin of 7rem or 112px on the right.
mb-28Adds a margin of 7rem or 112px on the bottom.
ml-28Adds a margin of 7rem or 112px on the left.
m-32This class applies a margin of 8rem or 128px on all sides.
mx-32This class applies a margin of 8rem or 128px along the x-axis.
my-32This class applies a margin of 8rem or 128px along the y-axis.
mt-32This class applies a margin of 8rem or 128px on the top.
mr-32This class applies a margin of 8rem or 128px on the right.
mb-32This class applies a margin of 8rem or 128px on the bottom.
ml-32This class applies a margin of 8rem or 128px on the left.
m-36A margin of 9rem or 144px is applied on all sides.
mx-36A margin of 9rem or 144px is applied on all sides.
my-36A margin of 9rem or 144px is applied on all sides.
mt-36A margin of 9rem or 144px is applied on all sides.
mr-36A margin of 9rem or 144px is applied on all sides.
mb-36A margin of 9rem or 144px is applied on all sides.
ml-36A margin of 9rem or 144px is applied on all sides.
m-40Used to apply a margin of 10rem or 160px on all sides.
mx-40Used to apply a margin of 10rem or 160px on all sides.
my-40Used to apply a margin of 10rem or 160px on all sides.
mt-40Used to apply a margin of 10rem or 160px on all sides.
mr-40Used to apply a margin of 10rem or 160px on all sides.
mb-40Used to apply a margin of 10rem or 160px on all sides.
ml-40Used to apply a margin of 10rem or 160px on all sides.
m-44Using this class ,the margin on all sides is set to 11rem or 176px.
mx-44Using this class ,the horizontal margin is set to 11rem or 176px.
my-44Using this class ,the vertical margin is set to 11rem or 176px.
mt-44Using this class ,the margin on the top is set to 11rem or 176px.
mr-44Using this class ,the margin on the right is set to 11rem or 176px.
mb-44Using this class ,the margin on the bottom is set to 11rem or 176px.
ml-44Using this class ,the margin on the left is set to 11rem or 176px.
m-48The margin on all sides is defined as 12rem or 192px.
mx-48The margin along the x-axis is defined as 12rem or 192px.
my-48The margin along the y-axis is defined as 12rem or 192px.
mt-48The margin on the top is defined as 12rem or 192px.
mr-48The margin on the right is defined as 12rem or 192px.
mb-48The margin on the bottom is defined as 12rem or 192px.
ml-48The margin on the left is defined as 12rem or 192px.
m-52Margin on all sides is specified as 13rem or 208px.
mx-52Margin along the x-axis is specified as 13rem or 208px.
my-52Margin along the y-axis is specified as 13rem or 208px.
mt-52Margin on the top is specified as 13rem or 208px.
mr-52Margin on the right is specified as 13rem or 208px.
mb-52Margin on the bottom is specified as 13rem or 208px.
ml-52Margin on the left is specified as 13rem or 208px.
m-56The margin on all sides is set to 14rem or 224px.
mx-56The horizontal margin is set to 14rem or 224px.
my-56The vertical margin is set to 14rem or 224px.
mt-56The top margin is set to 14rem or 224px.
mr-56The right margin is set to 14rem or 224px.
mb-56The bottom margin is set to 14rem or 224px.
ml-56The left margin is set to 14rem or 224px.
m-60The margin on all sides is equal to 15rem or 240px.
mx-60The margin on all sides is equal to 15rem or 240px.
my-60The margin on all sides is equal to 15rem or 240px.
mt-60The margin on all sides is equal to 15rem or 240px.
mr-60The margin on all sides is equal to 15rem or 240px.
mb-60The margin on all sides is equal to 15rem or 240px.
ml-60The margin on all sides is equal to 15rem or 240px.
m-64Used to apply a margin of 16rem or 256px on all sides.
mx-64Used to apply a margin of 16rem or 256px along the x-axis.
my-64Used to apply a margin of 16rem or 256px along the y-axis.
mt-64Used to apply a margin of 16rem or 256px on the top.
mr-64Used to apply a margin of 16rem or 256px on the right.
mb-64Used to apply a margin of 16rem or 256px on the bottom.
ml-64Used to apply a margin of 16rem or 256px on the left.
m-72This class applies a margin of 18rem or 288px on all sides.
mx-72This class applies a margin of 18rem or 288px horizontally.
my-72This class applies a margin of 18rem or 288px vertically.
mt-72This class applies a margin of 18rem or 288px on the top.
mr-72This class applies a margin of 18rem or 288px on the right.
mb-72This class applies a margin of 18rem or 288px on the bottom.
ml-72This class applies a margin of 18rem or 288px on the left.
m-80A margin of 20rem or 320px is applied on all sides.
mx-80A margin of 20rem or 320px is applied along the x-axis.
my-80A margin of 20rem or 320px is applied along the y-axis.
mt-80A margin of 20rem or 320px is applied on the top.
mr-80A margin of 20rem or 320px is applied on the right.
mb-80A margin of 20rem or 320px is applied on the bottom.
ml-80A margin of 20rem or 320px is applied on the left.
m-96Adds a margin of 24rem or 384px on all sides.
mx-96Adds a margin of 24rem or 384px along the x-axis.
my-96Adds a margin of 24rem or 384px along the y-axis.
mt-96Adds a margin of 24rem or 384px on the top.
mr-96Adds a margin of 24rem or 384px on the right.
mb-96Adds a margin of 24rem or 384px on the bottom.
ml-96Adds a margin of 24rem or 384px on the left.
m-autoMargin on all sides is defined as auto.
mx-autoMargin along the x-axis is defined as auto.
my-autoMargin along the y-axis is defined as auto.
mt-autoMargin on the top is defined as auto.
mr-autoMargin on the right is defined as auto.
mb-autoMargin on the bottom is defined as auto.
ml-autoMargin on the left is defined as auto.

Syntax

<element class="m-0">...</element>
Note: One can use a negative margin value by prefixing the class name with a dash such as -mt-8.

Tailwind margin is simple and straightforward.

To add a margin to an element, simply add tailwind margin class to the element’s HTML tag.

The example below demonstrates some Tailwind margin classes explained above:

Tailwind Margin Example: 1 

<!DOCTYPE html> <html><head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="text-center"> <h1 class="text-gray-600 text-2xl font-semibold"> MrExamples </h1> <p class="font-semibold mt-4">Margin Class in Tailwind CSS</p> <div class="my-2 bg-gray-200 flex font-medium rounded"> <div class="m-4 border-2 bg-gray-400 w-16 h-16"> m-4 </div> <div class="m-8 border-2 bg-gray-400 w-16 h-16"> m-6 </div> <div class="m-12 border-2 bg-gray-400 w-16 h-16"> m-10 </div> <div class="m-16 border-2 bg-gray-400 w-16 h-16"> m-12 </div> </div> </body> </html>
In below example, we have illustrated the working of more Tailwind margin classes:

Tailwind Margin Example: 2 

<!DOCTYPE html> <html><head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="text-center"> <h1 class="text-gray-600 text-2xl font-semibold"> MrExamples </h1> <p class="font-semibold mt-4">Margin Class in Tailwind CSS</p> <div class="my-2 bg-gray-200 font-medium rounded p-2 w-1/2 mx-auto"> <div class="m-0 border-2 bg-gray-400 w-16 h-16"> m-0 </div> <div class="mt-8 border-2 bg-gray-400 w-16 h-16"> mt-8 </div> <div class="ml-10 border-2 bg-gray-400 w-16 h-16"> ml-10 </div> <div class="my-4 border-2 bg-gray-400 w-16 h-16"> my-4 </div> <div class="mx-6 border-2 bg-gray-400 w-16 h-16"> mx-6 </div> </div> </body> </html>
For this example, let’s consider the usage of some additional tailwind margin property classes:

Tailwind Margin Example: 3 

<!DOCTYPE html> <html><head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="text-center"> <h1 class="text-gray-600 text-2xl font-semibold"> MrExamples </h1> <p class="font-semibold mt-4">Margin Class in Tailwind CSS</p> <div class="my-2 bg-gray-200 flex flex-col items-end font-medium rounded p-2 w-1/2 mx-auto"> <div class="mx-auto border-2 bg-gray-400 w-16 h-16"> mx-auto </div> <div class="mr-8 border-2 bg-gray-400 w-16 h-16"> mr-8 </div> <div class="m-5 border-2 bg-gray-400 w-16 h-16"> m-5 </div> <div class="mx-2 border-2 bg-gray-400 w-16 h-16"> mx-2 </div> <div class="mb-10 border-2 bg-gray-400 w-16 h-16"> mb-10 </div> </div> </body> </html>

Conclusion

Tailwind margin is a powerful and flexible feature that allows you to quickly and easily add space around the elements.

Whether you are creating a simple website or a complex web application, Tailwind margin can help you create effective layouts and designs.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *