Skip to content

Spacing and Sizing

This component provides utility classes for controlling margins and padding in Metro UI.

The spacing component defines CSS classes for adding or removing space around elements using margin and padding properties. These utilities follow a consistent naming convention and scale, making it easy to apply spacing in a standardized way throughout your application.

The spacing utilities follow this naming pattern:

{property}{sides}-{size}

Where:

  • {property} is either m (for margin) or p (for padding)
  • {sides} is either:
  • Empty (applies to all sides)
  • t (top)
  • r (right)
  • b (bottom)
  • l (left)
  • x (left and right)
  • y (top and bottom)
  • {size} is a number from 0 to 20, or auto for automatic margins

These classes set margins to auto, which is useful for centering elements:

ClassDescription
.mx-autoCenters an element horizontally (sets left and right margins to auto)
.my-autoCenters an element vertically (sets top and bottom margins to auto)
.ml-autoPushes an element to the right (sets left margin to auto)
.mr-autoPushes an element to the left (sets right margin to auto)
.mt-autoPushes an element to the bottom (sets top margin to auto)
.mb-autoPushes an element to the top (sets bottom margin to auto)

These classes remove spacing by setting margin or padding to zero:

ClassDescription
.m-0Removes margin on all sides
.mt-0Removes top margin
.mr-0Removes right margin
.mb-0Removes bottom margin
.ml-0Removes left margin
.mx-0Removes horizontal margins (left and right)
.my-0Removes vertical margins (top and bottom)
.p-0Removes padding on all sides
.pt-0Removes top padding
.pr-0Removes right padding
.pb-0Removes bottom padding
.pl-0Removes left padding
.px-0Removes horizontal padding (left and right)
.py-0Removes vertical padding (top and bottom)

These classes add spacing using a consistent scale. The actual size depends on the framework’s configuration variables (@mpStep and @mpUnit), but typically each step represents 4px or 0.25rem.

ClassDescription
.m-1 through .m-20Adds margin on all sides (from smallest to largest)
.mt-1 through .mt-20Adds top margin
.mr-1 through .mr-20Adds right margin
.mb-1 through .mb-20Adds bottom margin
.ml-1 through .ml-20Adds left margin
.mx-1 through .mx-20Adds horizontal margins (left and right)
.my-1 through .my-20Adds vertical margins (top and bottom)
ClassDescription
.p-1 through .p-20Adds padding on all sides (from smallest to largest)
.pt-1 through .pt-20Adds top padding
.pr-1 through .pr-20Adds right padding
.pb-1 through .pb-20Adds bottom padding
.pl-1 through .pl-20Adds left padding
.px-1 through .px-20Adds horizontal padding (left and right)
.py-1 through .py-20Adds vertical padding (top and bottom)

These classes add negative margins, which can be useful for overlapping elements or adjusting layouts:

ClassDescription
.mt-1-minus through .mt-20-minusAdds negative top margin
.mr-1-minus through .mr-20-minusAdds negative right margin
.mb-1-minus through .mb-20-minusAdds negative bottom margin
.ml-1-minus through .ml-20-minusAdds negative left margin

All spacing classes have responsive variants that apply at specific breakpoints. The responsive variants follow the naming pattern .{class}-{breakpoint}.

For example:

  • .mx-auto-md - Centers horizontally on medium screens and above
  • .p-4-lg - Adds padding level 4 on large screens and above
  • .mt-0-xl - Removes top margin on extra large screens and above

Where {breakpoint} can be: xs, sm, md, lg, xl, xxl, xxxl

<!-- Add margin to all sides -->
<div class="m-4">This element has margin level 4 on all sides</div>
<!-- Add different margins to different sides -->
<div class="mt-2 mb-4 mx-3">
This element has different margins on each side
</div>
<!-- Center an element horizontally -->
<div class="mx-auto" style="width: 200px;">
This element is centered horizontally
</div>
<!-- Push elements apart -->
<div class="d-flex">
<div class="mr-auto">Pushed to the left</div>
<div>Pushed to the right</div>
</div>
<!-- Negative margin to overlap elements -->
<div class="position-relative">
<div class="mt-4-minus">
This element overlaps the element above it
</div>
</div>
<!-- Add padding to all sides -->
<div class="p-4">This element has padding level 4 on all sides</div>
<!-- Add padding only horizontally -->
<div class="px-3">
This element has horizontal padding but no vertical padding
</div>
<!-- Add padding only vertically -->
<div class="py-5">
This element has vertical padding but no horizontal padding
</div>
<!-- Different padding on different sides -->
<div class="pt-2 pb-4 pl-3 pr-1">
This element has different padding on each side
</div>
<!-- Element with no margin on small screens, but margin on medium screens and above -->
<div class="m-0 m-3-md">
This element's margin changes based on screen size
</div>
<!-- Element with different padding at different breakpoints -->
<div class="p-2 p-3-sm p-4-md p-5-lg">
This element's padding increases as the screen gets larger
</div>
<!-- Centered on medium screens and above, but not on small screens -->
<div class="mx-0 mx-auto-md" style="width: 75%;">
This element is centered only on medium screens and above
</div>
  1. Consistent Spacing: Use these utilities instead of custom margins and paddings to maintain consistent spacing throughout your application.

  2. Responsive Spacing: Use responsive variants to adjust spacing based on screen size, typically increasing spacing on larger screens.

  3. Avoid Custom Styles: Instead of writing custom CSS for spacing, use these utility classes to keep your code clean and maintainable.

  4. Combine with Other Utilities: These spacing utilities work well with other Metro UI utilities:

  • Use with display utilities for responsive layouts
  • Use with flex utilities for spacing flex items
  • Use with position utilities for precise positioning
  1. Negative Margins: Use negative margins sparingly and only when necessary, as they can cause unexpected layout issues.
<div class="p-4">
<h3 class="mt-0 mb-3">Card Title</h3>
<p class="mb-4">Card content goes here...</p>
<button class="mt-2">Action</button>
</div>
<div class="container mx-auto px-2 px-4-md">
<!-- Content is centered with increasing horizontal padding on larger screens -->
</div>
<ul class="p-0">
<li class="mb-2">First item</li>
<li class="mb-2">Second item</li>
<li class="mb-2">Third item</li>
<li>Last item</li>
</ul>
<div class="mb-4">First section</div>
<div class="mb-4">Second section</div>
<div class="mb-4">Third section</div>
<div>Last section</div>