Margin
Add margin to a single side
Control the margin on one side of an element using the 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.
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>
Control the margin on all sides of an element using the m-{size}
utilities.
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>
-mt-8
</div>
For more information about Tailwind’s responsive design features, check out the documentation.
all
sm
md
lg
xl
<span class="mt-8 sm:mr-6 md:mb-4 lg:ml-2 xl:m-0 bg-yellow-200">Target</span>
</div>
Customizing
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.
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 .
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: {
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.
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:
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,
}