Margin
Utilities for controlling an element's margin.
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.
mt-8
mr-8
mb-8
ml-8
<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.
mx-8
<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.
my-8
<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.
m-8
<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.
<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>
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,
}
}