Margin
Utilities for controlling an element’s margin.
Class reference
Class | Properties |
---|---|
.m-0 | margin: 0; |
.m-1 | margin: 0.25rem; |
.m-2 | margin: 0.5rem; |
.m-3 | margin: 0.75rem; |
.m-4 | margin: 1rem; |
.m-5 | margin: 1.25rem; |
.m-6 | margin: 1.5rem; |
.m-8 | margin: 2rem; |
.m-10 | margin: 2.5rem; |
.m-12 | margin: 3rem; |
.m-16 | margin: 4rem; |
.m-20 | margin: 5rem; |
.m-24 | margin: 6rem; |
.m-32 | margin: 8rem; |
.m-40 | margin: 10rem; |
.m-48 | margin: 12rem; |
.m-56 | margin: 14rem; |
.m-64 | margin: 16rem; |
.m-auto | margin: auto; |
.m-px | margin: 1px; |
.-m-1 | margin: -0.25rem; |
.-m-2 | margin: -0.5rem; |
.-m-3 | margin: -0.75rem; |
.-m-4 | margin: -1rem; |
.-m-5 | margin: -1.25rem; |
.-m-6 | margin: -1.5rem; |
.-m-8 | margin: -2rem; |
.-m-10 | margin: -2.5rem; |
.-m-12 | margin: -3rem; |
.-m-16 | margin: -4rem; |
.-m-20 | margin: -5rem; |
.-m-24 | margin: -6rem; |
.-m-32 | margin: -8rem; |
.-m-40 | margin: -10rem; |
.-m-48 | margin: -12rem; |
.-m-56 | margin: -14rem; |
.-m-64 | margin: -16rem; |
.-m-px | margin: -1px; |
.my-0 | margin-top: 0; margin-bottom: 0; |
.mx-0 | margin-left: 0; margin-right: 0; |
.my-1 | margin-top: 0.25rem; margin-bottom: 0.25rem; |
.mx-1 | margin-left: 0.25rem; margin-right: 0.25rem; |
.my-2 | margin-top: 0.5rem; margin-bottom: 0.5rem; |
.mx-2 | margin-left: 0.5rem; margin-right: 0.5rem; |
.my-3 | margin-top: 0.75rem; margin-bottom: 0.75rem; |
.mx-3 | margin-left: 0.75rem; margin-right: 0.75rem; |
.my-4 | margin-top: 1rem; margin-bottom: 1rem; |
.mx-4 | margin-left: 1rem; margin-right: 1rem; |
.my-5 | margin-top: 1.25rem; margin-bottom: 1.25rem; |
.mx-5 | margin-left: 1.25rem; margin-right: 1.25rem; |
.my-6 | margin-top: 1.5rem; margin-bottom: 1.5rem; |
.mx-6 | margin-left: 1.5rem; margin-right: 1.5rem; |
.my-8 | margin-top: 2rem; margin-bottom: 2rem; |
.mx-8 | margin-left: 2rem; margin-right: 2rem; |
.my-10 | margin-top: 2.5rem; margin-bottom: 2.5rem; |
.mx-10 | margin-left: 2.5rem; margin-right: 2.5rem; |
.my-12 | margin-top: 3rem; margin-bottom: 3rem; |
.mx-12 | margin-left: 3rem; margin-right: 3rem; |
.my-16 | margin-top: 4rem; margin-bottom: 4rem; |
.mx-16 | margin-left: 4rem; margin-right: 4rem; |
.my-20 | margin-top: 5rem; margin-bottom: 5rem; |
.mx-20 | margin-left: 5rem; margin-right: 5rem; |
.my-24 | margin-top: 6rem; margin-bottom: 6rem; |
.mx-24 | margin-left: 6rem; margin-right: 6rem; |
.my-32 | margin-top: 8rem; margin-bottom: 8rem; |
.mx-32 | margin-left: 8rem; margin-right: 8rem; |
.my-40 | margin-top: 10rem; margin-bottom: 10rem; |
.mx-40 | margin-left: 10rem; margin-right: 10rem; |
.my-48 | margin-top: 12rem; margin-bottom: 12rem; |
.mx-48 | margin-left: 12rem; margin-right: 12rem; |
.my-56 | margin-top: 14rem; margin-bottom: 14rem; |
.mx-56 | margin-left: 14rem; margin-right: 14rem; |
.my-64 | margin-top: 16rem; margin-bottom: 16rem; |
.mx-64 | margin-left: 16rem; margin-right: 16rem; |
.my-auto | margin-top: auto; margin-bottom: auto; |
.mx-auto | margin-left: auto; margin-right: auto; |
.my-px | margin-top: 1px; margin-bottom: 1px; |
.mx-px | margin-left: 1px; margin-right: 1px; |
.-my-1 | margin-top: -0.25rem; margin-bottom: -0.25rem; |
.-mx-1 | margin-left: -0.25rem; margin-right: -0.25rem; |
.-my-2 | margin-top: -0.5rem; margin-bottom: -0.5rem; |
.-mx-2 | margin-left: -0.5rem; margin-right: -0.5rem; |
.-my-3 | margin-top: -0.75rem; margin-bottom: -0.75rem; |
.-mx-3 | margin-left: -0.75rem; margin-right: -0.75rem; |
.-my-4 | margin-top: -1rem; margin-bottom: -1rem; |
.-mx-4 | margin-left: -1rem; margin-right: -1rem; |
.-my-5 | margin-top: -1.25rem; margin-bottom: -1.25rem; |
.-mx-5 | margin-left: -1.25rem; margin-right: -1.25rem; |
.-my-6 | margin-top: -1.5rem; margin-bottom: -1.5rem; |
.-mx-6 | margin-left: -1.5rem; margin-right: -1.5rem; |
.-my-8 | margin-top: -2rem; margin-bottom: -2rem; |
.-mx-8 | margin-left: -2rem; margin-right: -2rem; |
.-my-10 | margin-top: -2.5rem; margin-bottom: -2.5rem; |
.-mx-10 | margin-left: -2.5rem; margin-right: -2.5rem; |
.-my-12 | margin-top: -3rem; margin-bottom: -3rem; |
.-mx-12 | margin-left: -3rem; margin-right: -3rem; |
.-my-16 | margin-top: -4rem; margin-bottom: -4rem; |
.-mx-16 | margin-left: -4rem; margin-right: -4rem; |
.-my-20 | margin-top: -5rem; margin-bottom: -5rem; |
.-mx-20 | margin-left: -5rem; margin-right: -5rem; |
.-my-24 | margin-top: -6rem; margin-bottom: -6rem; |
.-mx-24 | margin-left: -6rem; margin-right: -6rem; |
.-my-32 | margin-top: -8rem; margin-bottom: -8rem; |
.-mx-32 | margin-left: -8rem; margin-right: -8rem; |
.-my-40 | margin-top: -10rem; margin-bottom: -10rem; |
.-mx-40 | margin-left: -10rem; margin-right: -10rem; |
.-my-48 | margin-top: -12rem; margin-bottom: -12rem; |
.-mx-48 | margin-left: -12rem; margin-right: -12rem; |
.-my-56 | margin-top: -14rem; margin-bottom: -14rem; |
.-mx-56 | margin-left: -14rem; margin-right: -14rem; |
.-my-64 | margin-top: -16rem; margin-bottom: -16rem; |
.-mx-64 | margin-left: -16rem; margin-right: -16rem; |
.-my-px | margin-top: -1px; margin-bottom: -1px; |
.-mx-px | margin-left: -1px; margin-right: -1px; |
.mt-0 | margin-top: 0; |
.mr-0 | margin-right: 0; |
.mb-0 | margin-bottom: 0; |
.ml-0 | margin-left: 0; |
.mt-1 | margin-top: 0.25rem; |
.mr-1 | margin-right: 0.25rem; |
.mb-1 | margin-bottom: 0.25rem; |
.ml-1 | margin-left: 0.25rem; |
.mt-2 | margin-top: 0.5rem; |
.mr-2 | margin-right: 0.5rem; |
.mb-2 | margin-bottom: 0.5rem; |
.ml-2 | margin-left: 0.5rem; |
.mt-3 | margin-top: 0.75rem; |
.mr-3 | margin-right: 0.75rem; |
.mb-3 | margin-bottom: 0.75rem; |
.ml-3 | margin-left: 0.75rem; |
.mt-4 | margin-top: 1rem; |
.mr-4 | margin-right: 1rem; |
.mb-4 | margin-bottom: 1rem; |
.ml-4 | margin-left: 1rem; |
.mt-5 | margin-top: 1.25rem; |
.mr-5 | margin-right: 1.25rem; |
.mb-5 | margin-bottom: 1.25rem; |
.ml-5 | margin-left: 1.25rem; |
.mt-6 | margin-top: 1.5rem; |
.mr-6 | margin-right: 1.5rem; |
.mb-6 | margin-bottom: 1.5rem; |
.ml-6 | margin-left: 1.5rem; |
.mt-8 | margin-top: 2rem; |
.mr-8 | margin-right: 2rem; |
.mb-8 | margin-bottom: 2rem; |
.ml-8 | margin-left: 2rem; |
.mt-10 | margin-top: 2.5rem; |
.mr-10 | margin-right: 2.5rem; |
.mb-10 | margin-bottom: 2.5rem; |
.ml-10 | margin-left: 2.5rem; |
.mt-12 | margin-top: 3rem; |
.mr-12 | margin-right: 3rem; |
.mb-12 | margin-bottom: 3rem; |
.ml-12 | margin-left: 3rem; |
.mt-16 | margin-top: 4rem; |
.mr-16 | margin-right: 4rem; |
.mb-16 | margin-bottom: 4rem; |
.ml-16 | margin-left: 4rem; |
.mt-20 | margin-top: 5rem; |
.mr-20 | margin-right: 5rem; |
.mb-20 | margin-bottom: 5rem; |
.ml-20 | margin-left: 5rem; |
.mt-24 | margin-top: 6rem; |
.mr-24 | margin-right: 6rem; |
.mb-24 | margin-bottom: 6rem; |
.ml-24 | margin-left: 6rem; |
.mt-32 | margin-top: 8rem; |
.mr-32 | margin-right: 8rem; |
.mb-32 | margin-bottom: 8rem; |
.ml-32 | margin-left: 8rem; |
.mt-40 | margin-top: 10rem; |
.mr-40 | margin-right: 10rem; |
.mb-40 | margin-bottom: 10rem; |
.ml-40 | margin-left: 10rem; |
.mt-48 | margin-top: 12rem; |
.mr-48 | margin-right: 12rem; |
.mb-48 | margin-bottom: 12rem; |
.ml-48 | margin-left: 12rem; |
.mt-56 | margin-top: 14rem; |
.mr-56 | margin-right: 14rem; |
.mb-56 | margin-bottom: 14rem; |
.ml-56 | margin-left: 14rem; |
.mt-64 | margin-top: 16rem; |
.mr-64 | margin-right: 16rem; |
.mb-64 | margin-bottom: 16rem; |
.ml-64 | margin-left: 16rem; |
.mt-auto | margin-top: auto; |
.mr-auto | margin-right: auto; |
.mb-auto | margin-bottom: auto; |
.ml-auto | margin-left: auto; |
.mt-px | margin-top: 1px; |
.mr-px | margin-right: 1px; |
.mb-px | margin-bottom: 1px; |
.ml-px | margin-left: 1px; |
.-mt-1 | margin-top: -0.25rem; |
.-mr-1 | margin-right: -0.25rem; |
.-mb-1 | margin-bottom: -0.25rem; |
.-ml-1 | margin-left: -0.25rem; |
.-mt-2 | margin-top: -0.5rem; |
.-mr-2 | margin-right: -0.5rem; |
.-mb-2 | margin-bottom: -0.5rem; |
.-ml-2 | margin-left: -0.5rem; |
.-mt-3 | margin-top: -0.75rem; |
.-mr-3 | margin-right: -0.75rem; |
.-mb-3 | margin-bottom: -0.75rem; |
.-ml-3 | margin-left: -0.75rem; |
.-mt-4 | margin-top: -1rem; |
.-mr-4 | margin-right: -1rem; |
.-mb-4 | margin-bottom: -1rem; |
.-ml-4 | margin-left: -1rem; |
.-mt-5 | margin-top: -1.25rem; |
.-mr-5 | margin-right: -1.25rem; |
.-mb-5 | margin-bottom: -1.25rem; |
.-ml-5 | margin-left: -1.25rem; |
.-mt-6 | margin-top: -1.5rem; |
.-mr-6 | margin-right: -1.5rem; |
.-mb-6 | margin-bottom: -1.5rem; |
.-ml-6 | margin-left: -1.5rem; |
.-mt-8 | margin-top: -2rem; |
.-mr-8 | margin-right: -2rem; |
.-mb-8 | margin-bottom: -2rem; |
.-ml-8 | margin-left: -2rem; |
.-mt-10 | margin-top: -2.5rem; |
.-mr-10 | margin-right: -2.5rem; |
.-mb-10 | margin-bottom: -2.5rem; |
.-ml-10 | margin-left: -2.5rem; |
.-mt-12 | margin-top: -3rem; |
.-mr-12 | margin-right: -3rem; |
.-mb-12 | margin-bottom: -3rem; |
.-ml-12 | margin-left: -3rem; |
.-mt-16 | margin-top: -4rem; |
.-mr-16 | margin-right: -4rem; |
.-mb-16 | margin-bottom: -4rem; |
.-ml-16 | margin-left: -4rem; |
.-mt-20 | margin-top: -5rem; |
.-mr-20 | margin-right: -5rem; |
.-mb-20 | margin-bottom: -5rem; |
.-ml-20 | margin-left: -5rem; |
.-mt-24 | margin-top: -6rem; |
.-mr-24 | margin-right: -6rem; |
.-mb-24 | margin-bottom: -6rem; |
.-ml-24 | margin-left: -6rem; |
.-mt-32 | margin-top: -8rem; |
.-mr-32 | margin-right: -8rem; |
.-mb-32 | margin-bottom: -8rem; |
.-ml-32 | margin-left: -8rem; |
.-mt-40 | margin-top: -10rem; |
.-mr-40 | margin-right: -10rem; |
.-mb-40 | margin-bottom: -10rem; |
.-ml-40 | margin-left: -10rem; |
.-mt-48 | margin-top: -12rem; |
.-mr-48 | margin-right: -12rem; |
.-mb-48 | margin-bottom: -12rem; |
.-ml-48 | margin-left: -12rem; |
.-mt-56 | margin-top: -14rem; |
.-mr-56 | margin-right: -14rem; |
.-mb-56 | margin-bottom: -14rem; |
.-ml-56 | margin-left: -14rem; |
.-mt-64 | margin-top: -16rem; |
.-mr-64 | margin-right: -16rem; |
.-mb-64 | margin-bottom: -16rem; |
.-ml-64 | margin-left: -16rem; |
.-mt-px | margin-top: -1px; |
.-mr-px | margin-right: -1px; |
.-mb-px | margin-bottom: -1px; |
.-ml-px | margin-left: -1px; |
Add margin to a single side
Control the margin on one side of an element using the m{t|r|b|l}-{size}
utilities.
For example, mt-6
would add 1.5rem
of margin to the top of an element, mr-4
would add 1rem
of margin to the right of an element, mb-8
would add 2rem
of margin to the bottom of an element, and ml-2
would add 0.5rem
of margin to the left of an element.
<div class="bg-gray-400"><span class="mt-8 bg-yellow-200">Target</span></div>
<div class="bg-gray-400"><span class="mr-8 bg-yellow-200">Target</span></div>
<div class="bg-gray-400"><span class="mb-8 bg-yellow-200">Target</span></div>
<div class="bg-gray-400"><span class="ml-8 bg-yellow-200">Target</span></div>
Add horizontal margin
Control the horizontal margin of an element using the mx-{size}
utilities.
<div class="bg-gray-400"><span class="mx-8 bg-yellow-200">Target</span></div>
Add vertical margin
Control the vertical margin of an element using the my-{size}
utilities.
<div class="bg-gray-400"><span class="my-8 bg-yellow-200">Target</span></div>
Add margin to all sides
Control the margin on all sides of an element using the m-{size}
utilities.
<div class="bg-gray-400"><span class="m-8 bg-yellow-200">Target</span></div>
Negative margins
Control the negative margin of an element using the -m{side?}-{size}
utilities.
<div class="bg-gray-400 h-16 w-32"></div>
<div class="-mt-8 bg-yellow-200 mx-auto h-16 w-24 ...">
-mt-8
</div>
Responsive
To control the margin of an element at a specific breakpoint, add a {screen}:
prefix to any existing margin utility. For example, adding the class md:my-8
to an element would apply the my-8
utility at medium screen sizes and above.
For more information about Tailwind’s responsive design features, check out the Responsive Design documentation.
all
sm
md
lg
xl
<div class="bg-gray-400 ...">
<span class="mt-8 sm:mr-6 md:mb-4 lg:ml-2 xl:m-0 bg-yellow-200">Target</span>
</div>
Target
Customizing
Margin scale
By default Tailwind provides 20 margin utilities for each side and axis.
If you’d like to customize these values for padding, margin, width, and height all at once, use the theme.spacing
section of your tailwind.config.js
file.
// tailwind.config.js
module.exports = {
theme: {
spacing: {
+ sm: '8px',
+ md: '16px',
+ lg: '24px',
+ xl: '48px',
}
}
}
To customize only the margin values, use the theme.margin
section of your tailwind.config.js
file.
// tailwind.config.js
module.exports = {
theme: {
margin: {
+ sm: '8px',
+ md: '16px',
+ lg: '24px',
+ xl: '48px',
}
}
}
Learn more about customizing the default theme in the theme customization documentation.
Negative values
If you’d like to add additional negative margin classes (taking the form -m{side?}-{size}
), prefix the keys in your config file with a dash:
// tailwind.config.js
module.exports = {
theme: {
extend: {
margin: {
+ '-72': '-18rem',
}
}
}
}
Tailwind is smart enough to generate classes like -mx-72
when it sees the leading dash, not mx--72
like you might expect.
Responsive and pseudo-class variants
By default, only responsive variants are generated for margin utilities.
You can control which variants are generated for the margin utilities by modifying the margin
property in the variants
section of your tailwind.config.js
file.
For example, this config will also generate hover and focus variants:
// tailwind.config.js
module.exports = {
variants: {
// ...
- margin: ['responsive'],
+ margin: ['responsive', 'hover', 'focus'],
}
}
Disabling
If you don’t plan to use the margin utilities in your project, you can disable them entirely by setting the margin
property to false
in the corePlugins
section of your config file:
// tailwind.config.js
module.exports = {
corePlugins: {
// ...
+ margin: false,
}
}