Spacing utilities

Responsive utility classes which can be used to set the margin, padding, and gap of and between elements.

Margin and padding #

Assign margin or padding values to an element or a subset of its sides using shorthand classes. Includes support for all and individual properties (including horizontal and vertical). Default values range from 0.25rem / 4px to 3rem / 48px.

Margin and padding Notation #

The class names for the margin and padding utilities come in the following formats:

  • .{property}{sides}-{size}
  • .{property}{sides}-{breakpoint}-{size}

The {breakpoint} can be sm, md, lg, xl, or xxl. If the breakpoint is not provided, the element will be affected on all screen sizes (including on extra small screens). On the other hand, if it is provided, the element will be affected only for that breakpoint and up.

{property}

The {property} can be one of the following:

{property} Description
m Sets margin property
p Sets padding property

{sides}

The {sides} can be one of the following:

{sides} Description
Blank Sets margin or padding property on all 4 sides
t Sets margin-top or padding-top property
b Sets margin-bottom or padding-bottom property
s (start) Sets margin-left or padding-left property (inverted in RTL)
e (end) Sets margin-right or padding-right property (inverted in RTL)

{size}

The {size} can be one of the following:

{size} Description
0 Sets margin or padding to 0
1 Sets margin or padding to var(--bs-spacer-1) / 0.25rem / 4px
2 Sets margin or padding to var(--bs-spacer-2) / 0.5rem / 8px
3 Sets margin or padding to var(--bs-spacer-3) / 1rem / 16px
4 Sets margin or padding to var(--bs-spacer-4) / 1.5rem / 24px
5 Sets margin or padding to var(--bs-spacer-5) / 3rem / 48px
auto Sets margin or auto

Margin and padding Examples #

The following table shows some examples of how the spacing utility classes can be used:

Class Description
.m-0 Sets margin: 0
.mx-auto Sets margin-left: auto and margin-right: auto
.ms-auto Sets margin-left: auto (inverted in RTL)
.m-sm-2 Sets margin: var(--bs-spacer-2) (only small screens and up)
.p-1 Sets padding: var(--bs-spacer-1)
.pt-md-5 Sets padding-top: var(--bs-spacer-5) (only medium screens and up)
.pe-3 Sets padding-right: var(--bs-spacer-3) (inverted in RTL)
.py-4 Sets padding-top: var(--bs-spacer-4) and padding-bottom: var(--bs-spacer-4)

As the table above shows, the different utility classes available can be used to control the spacing to a great degree. Moreover, these classes are also sometimes useful for controlling the positioning of elements. For example, the .mx-auto class can be used to horizontally center block-level elements (see below), while the .ms-auto class can be used to push an element to the right (left in RTL) using flexbox.

Margin and padding Horizontal centering #

The .mx-auto class can be used for horizontally centering block-level elements with fixed widths.

Centered
HTML
<!-- Horizontally centered -->
<div class="mx-auto specific-w-100">Centered</div>

Margin and padding Negative margin #

In CSS, margin can have negative values, and Halfmoon comes with utility classes which can be used to set negative margins. These utilities come in the following formats:

  • .m{sides}-{n1|n2|n3|n4|n5}
  • .m{sides}-{breakpoint}-{n1|n2|n3|n4|n5}

In other words, the {size} must be prefixed with the letter n (for negative). These classes are sometimes very useful because they can be used to make sure some elements take up more than their generally allotted space.

.mx-n4
HTML
<!-- Negative margin -->
<div class="p-4">
  <div class="mx-n4">
    ...
  </div>
</div>

In the above example, the parent container has the .p-4 class which sets its padding to var(--bs-spacer-4) / 1.5rem / 24px. However, its child box still takes up the full width (of the parent) because of the negative horizontal margins set using the .mx-n4 class.

Gap #

If an element has the display: grid or display: flex property, we can use gap utilities on it to space out the children elements. This can save on having to add margin utilities to individual children of a grid or flex container. The gap utilities come in the following formats:

  • .gap-{size}
  • .gap-{breakpoint}-{size}
  • .row-gap-{size}
  • .row-gap-{breakpoint}-{size}
  • .column-gap-{size}
  • .column-gap-{breakpoint}-{size}

The {size} can be any number from 0 to 5 (same as padding).

Up next
Text utilities

Help us grow

Our main goal is to make Halfmoon the go-to framework for building websites, dashboards and tools. If you believe in our mission, consider becoming a sponsor and help us grow.

You can email us directly if you have any queries. We are always happy to answer.

Subscribe for updates

We will notify you when the framework gets a substantial update. No spam ever.

Follow us on Twitter so that you can stay updated that way.