Margin

Utilities for controlling an element’s margin.

Class reference

ClassProperties
.m-0margin: 0;
.m-1margin: 0.25rem;
.m-2margin: 0.5rem;
.m-3margin: 0.75rem;
.m-4margin: 1rem;
.m-5margin: 1.25rem;
.m-6margin: 1.5rem;
.m-8margin: 2rem;
.m-10margin: 2.5rem;
.m-12margin: 3rem;
.m-16margin: 4rem;
.m-20margin: 5rem;
.m-24margin: 6rem;
.m-32margin: 8rem;
.m-40margin: 10rem;
.m-48margin: 12rem;
.m-56margin: 14rem;
.m-64margin: 16rem;
.m-automargin: auto;
.m-pxmargin: 1px;
.-m-1margin: -0.25rem;
.-m-2margin: -0.5rem;
.-m-3margin: -0.75rem;
.-m-4margin: -1rem;
.-m-5margin: -1.25rem;
.-m-6margin: -1.5rem;
.-m-8margin: -2rem;
.-m-10margin: -2.5rem;
.-m-12margin: -3rem;
.-m-16margin: -4rem;
.-m-20margin: -5rem;
.-m-24margin: -6rem;
.-m-32margin: -8rem;
.-m-40margin: -10rem;
.-m-48margin: -12rem;
.-m-56margin: -14rem;
.-m-64margin: -16rem;
.-m-pxmargin: -1px;
.my-0margin-top: 0;margin-bottom: 0;
.my-1margin-top: 0.25rem;margin-bottom: 0.25rem;
.my-2margin-top: 0.5rem;margin-bottom: 0.5rem;
.my-3margin-top: 0.75rem;margin-bottom: 0.75rem;
.my-4margin-top: 1rem;margin-bottom: 1rem;
.my-5margin-top: 1.25rem;margin-bottom: 1.25rem;
.my-6margin-top: 1.5rem;margin-bottom: 1.5rem;
.my-8margin-top: 2rem;margin-bottom: 2rem;
.my-10margin-top: 2.5rem;margin-bottom: 2.5rem;
.my-12margin-top: 3rem;margin-bottom: 3rem;
.my-16margin-top: 4rem;margin-bottom: 4rem;
.my-20margin-top: 5rem;margin-bottom: 5rem;
.my-24margin-top: 6rem;margin-bottom: 6rem;
.my-32margin-top: 8rem;margin-bottom: 8rem;
.my-40margin-top: 10rem;margin-bottom: 10rem;
.my-48margin-top: 12rem;margin-bottom: 12rem;
.my-56margin-top: 14rem;margin-bottom: 14rem;
.my-64margin-top: 16rem;margin-bottom: 16rem;
.my-automargin-top: auto;margin-bottom: auto;
.my-pxmargin-top: 1px;margin-bottom: 1px;
.-my-1margin-top: -0.25rem;margin-bottom: -0.25rem;
.-my-2margin-top: -0.5rem;margin-bottom: -0.5rem;
.-my-3margin-top: -0.75rem;margin-bottom: -0.75rem;
.-my-4margin-top: -1rem;margin-bottom: -1rem;
.-my-5margin-top: -1.25rem;margin-bottom: -1.25rem;
.-my-6margin-top: -1.5rem;margin-bottom: -1.5rem;
.-my-8margin-top: -2rem;margin-bottom: -2rem;
.-my-10margin-top: -2.5rem;margin-bottom: -2.5rem;
.-my-12margin-top: -3rem;margin-bottom: -3rem;
.-my-16margin-top: -4rem;margin-bottom: -4rem;
.-my-20margin-top: -5rem;margin-bottom: -5rem;
.-my-24margin-top: -6rem;margin-bottom: -6rem;
.-my-32margin-top: -8rem;margin-bottom: -8rem;
.-my-40margin-top: -10rem;margin-bottom: -10rem;
.-my-48margin-top: -12rem;margin-bottom: -12rem;
.-my-56margin-top: -14rem;margin-bottom: -14rem;
.-my-64margin-top: -16rem;margin-bottom: -16rem;
.-my-pxmargin-top: -1px;margin-bottom: -1px;
.mx-0margin-right: 0;margin-left: 0;
.mx-1margin-right: 0.25rem;margin-left: 0.25rem;
.mx-2margin-right: 0.5rem;margin-left: 0.5rem;
.mx-3margin-right: 0.75rem;margin-left: 0.75rem;
.mx-4margin-right: 1rem;margin-left: 1rem;
.mx-5margin-right: 1.25rem;margin-left: 1.25rem;
.mx-6margin-right: 1.5rem;margin-left: 1.5rem;
.mx-8margin-right: 2rem;margin-left: 2rem;
.mx-10margin-right: 2.5rem;margin-left: 2.5rem;
.mx-12margin-right: 3rem;margin-left: 3rem;
.mx-16margin-right: 4rem;margin-left: 4rem;
.mx-20margin-right: 5rem;margin-left: 5rem;
.mx-24margin-right: 6rem;margin-left: 6rem;
.mx-32margin-right: 8rem;margin-left: 8rem;
.mx-40margin-right: 10rem;margin-left: 10rem;
.mx-48margin-right: 12rem;margin-left: 12rem;
.mx-56margin-right: 14rem;margin-left: 14rem;
.mx-64margin-right: 16rem;margin-left: 16rem;
.mx-automargin-right: auto;margin-left: auto;
.mx-pxmargin-right: 1px;margin-left: 1px;
.-mx-1margin-right: -0.25rem;margin-left: -0.25rem;
.-mx-2margin-right: -0.5rem;margin-left: -0.5rem;
.-mx-3margin-right: -0.75rem;margin-left: -0.75rem;
.-mx-4margin-right: -1rem;margin-left: -1rem;
.-mx-5margin-right: -1.25rem;margin-left: -1.25rem;
.-mx-6margin-right: -1.5rem;margin-left: -1.5rem;
.-mx-8margin-right: -2rem;margin-left: -2rem;
.-mx-10margin-right: -2.5rem;margin-left: -2.5rem;
.-mx-12margin-right: -3rem;margin-left: -3rem;
.-mx-16margin-right: -4rem;margin-left: -4rem;
.-mx-20margin-right: -5rem;margin-left: -5rem;
.-mx-24margin-right: -6rem;margin-left: -6rem;
.-mx-32margin-right: -8rem;margin-left: -8rem;
.-mx-40margin-right: -10rem;margin-left: -10rem;
.-mx-48margin-right: -12rem;margin-left: -12rem;
.-mx-56margin-right: -14rem;margin-left: -14rem;
.-mx-64margin-right: -16rem;margin-left: -16rem;
.-mx-pxmargin-right: -1px;margin-left: -1px;
.mt-0margin-top: 0;
.mt-1margin-top: 0.25rem;
.mt-2margin-top: 0.5rem;
.mt-3margin-top: 0.75rem;
.mt-4margin-top: 1rem;
.mt-5margin-top: 1.25rem;
.mt-6margin-top: 1.5rem;
.mt-8margin-top: 2rem;
.mt-10margin-top: 2.5rem;
.mt-12margin-top: 3rem;
.mt-16margin-top: 4rem;
.mt-20margin-top: 5rem;
.mt-24margin-top: 6rem;
.mt-32margin-top: 8rem;
.mt-40margin-top: 10rem;
.mt-48margin-top: 12rem;
.mt-56margin-top: 14rem;
.mt-64margin-top: 16rem;
.mt-automargin-top: auto;
.mt-pxmargin-top: 1px;
.-mt-1margin-top: -0.25rem;
.-mt-2margin-top: -0.5rem;
.-mt-3margin-top: -0.75rem;
.-mt-4margin-top: -1rem;
.-mt-5margin-top: -1.25rem;
.-mt-6margin-top: -1.5rem;
.-mt-8margin-top: -2rem;
.-mt-10margin-top: -2.5rem;
.-mt-12margin-top: -3rem;
.-mt-16margin-top: -4rem;
.-mt-20margin-top: -5rem;
.-mt-24margin-top: -6rem;
.-mt-32margin-top: -8rem;
.-mt-40margin-top: -10rem;
.-mt-48margin-top: -12rem;
.-mt-56margin-top: -14rem;
.-mt-64margin-top: -16rem;
.-mt-pxmargin-top: -1px;
.mr-0margin-right: 0;
.mr-1margin-right: 0.25rem;
.mr-2margin-right: 0.5rem;
.mr-3margin-right: 0.75rem;
.mr-4margin-right: 1rem;
.mr-5margin-right: 1.25rem;
.mr-6margin-right: 1.5rem;
.mr-8margin-right: 2rem;
.mr-10margin-right: 2.5rem;
.mr-12margin-right: 3rem;
.mr-16margin-right: 4rem;
.mr-20margin-right: 5rem;
.mr-24margin-right: 6rem;
.mr-32margin-right: 8rem;
.mr-40margin-right: 10rem;
.mr-48margin-right: 12rem;
.mr-56margin-right: 14rem;
.mr-64margin-right: 16rem;
.mr-automargin-right: auto;
.mr-pxmargin-right: 1px;
.-mr-1margin-right: -0.25rem;
.-mr-2margin-right: -0.5rem;
.-mr-3margin-right: -0.75rem;
.-mr-4margin-right: -1rem;
.-mr-5margin-right: -1.25rem;
.-mr-6margin-right: -1.5rem;
.-mr-8margin-right: -2rem;
.-mr-10margin-right: -2.5rem;
.-mr-12margin-right: -3rem;
.-mr-16margin-right: -4rem;
.-mr-20margin-right: -5rem;
.-mr-24margin-right: -6rem;
.-mr-32margin-right: -8rem;
.-mr-40margin-right: -10rem;
.-mr-48margin-right: -12rem;
.-mr-56margin-right: -14rem;
.-mr-64margin-right: -16rem;
.-mr-pxmargin-right: -1px;
.mb-0margin-bottom: 0;
.mb-1margin-bottom: 0.25rem;
.mb-2margin-bottom: 0.5rem;
.mb-3margin-bottom: 0.75rem;
.mb-4margin-bottom: 1rem;
.mb-5margin-bottom: 1.25rem;
.mb-6margin-bottom: 1.5rem;
.mb-8margin-bottom: 2rem;
.mb-10margin-bottom: 2.5rem;
.mb-12margin-bottom: 3rem;
.mb-16margin-bottom: 4rem;
.mb-20margin-bottom: 5rem;
.mb-24margin-bottom: 6rem;
.mb-32margin-bottom: 8rem;
.mb-40margin-bottom: 10rem;
.mb-48margin-bottom: 12rem;
.mb-56margin-bottom: 14rem;
.mb-64margin-bottom: 16rem;
.mb-automargin-bottom: auto;
.mb-pxmargin-bottom: 1px;
.-mb-1margin-bottom: -0.25rem;
.-mb-2margin-bottom: -0.5rem;
.-mb-3margin-bottom: -0.75rem;
.-mb-4margin-bottom: -1rem;
.-mb-5margin-bottom: -1.25rem;
.-mb-6margin-bottom: -1.5rem;
.-mb-8margin-bottom: -2rem;
.-mb-10margin-bottom: -2.5rem;
.-mb-12margin-bottom: -3rem;
.-mb-16margin-bottom: -4rem;
.-mb-20margin-bottom: -5rem;
.-mb-24margin-bottom: -6rem;
.-mb-32margin-bottom: -8rem;
.-mb-40margin-bottom: -10rem;
.-mb-48margin-bottom: -12rem;
.-mb-56margin-bottom: -14rem;
.-mb-64margin-bottom: -16rem;
.-mb-pxmargin-bottom: -1px;
.ml-0margin-left: 0;
.ml-1margin-left: 0.25rem;
.ml-2margin-left: 0.5rem;
.ml-3margin-left: 0.75rem;
.ml-4margin-left: 1rem;
.ml-5margin-left: 1.25rem;
.ml-6margin-left: 1.5rem;
.ml-8margin-left: 2rem;
.ml-10margin-left: 2.5rem;
.ml-12margin-left: 3rem;
.ml-16margin-left: 4rem;
.ml-20margin-left: 5rem;
.ml-24margin-left: 6rem;
.ml-32margin-left: 8rem;
.ml-40margin-left: 10rem;
.ml-48margin-left: 12rem;
.ml-56margin-left: 14rem;
.ml-64margin-left: 16rem;
.ml-automargin-left: auto;
.ml-pxmargin-left: 1px;
.-ml-1margin-left: -0.25rem;
.-ml-2margin-left: -0.5rem;
.-ml-3margin-left: -0.75rem;
.-ml-4margin-left: -1rem;
.-ml-5margin-left: -1.25rem;
.-ml-6margin-left: -1.5rem;
.-ml-8margin-left: -2rem;
.-ml-10margin-left: -2.5rem;
.-ml-12margin-left: -3rem;
.-ml-16margin-left: -4rem;
.-ml-20margin-left: -5rem;
.-ml-24margin-left: -6rem;
.-ml-32margin-left: -8rem;
.-ml-40margin-left: -10rem;
.-ml-48margin-left: -12rem;
.-ml-56margin-left: -14rem;
.-ml-64margin-left: -16rem;
.-ml-pxmargin-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.

Margin - 图1

  1. <div class="bg-gray-400"><span class="mt-8 bg-yellow-200">Target</span></div>
  2. <div class="bg-gray-400"><span class="mr-8 bg-yellow-200">Target</span></div>
  3. <div class="bg-gray-400"><span class="mb-8 bg-yellow-200">Target</span></div>
  4. <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.

Margin - 图2

  1. <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.

Margin - 图3

  1. <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.

Margin - 图4

  1. <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.

Margin - 图5

  1. <div class="bg-gray-400 h-16 w-32"></div>
  2. <div class="-mt-8 bg-yellow-200 mx-auto h-16 w-24 ...">
  3. -mt-8
  4. </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.

  1. <div class="bg-gray-400 ...">
  2. <span class="mt-8 sm:mr-6 md:mb-4 lg:ml-2 xl:m-0 bg-yellow-200">Target</span>
  3. </div>

Margin - 图6


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.

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. spacing: {
  5. + sm: '8px',
  6. + md: '16px',
  7. + lg: '24px',
  8. + xl: '48px',
  9. }
  10. }
  11. }

To customize only the margin values, use the theme.margin section of your tailwind.config.js file.

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. margin: {
  5. + sm: '8px',
  6. + md: '16px',
  7. + lg: '24px',
  8. + xl: '48px',
  9. }
  10. }
  11. }

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:

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. margin: {
  5. + '-72': '-18rem',
  6. }
  7. }
  8. }

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:

  1. // tailwind.config.js
  2. module.exports = {
  3. variants: {
  4. // ...
  5. - margin: ['responsive'],
  6. + margin: ['responsive', 'hover', 'focus'],
  7. }
  8. }

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:

  1. // tailwind.config.js
  2. module.exports = {
  3. corePlugins: {
  4. // ...
  5. + margin: false,
  6. }
  7. }