Skip to content

Margin

Use the spacing scale to set margins on every side, one side, or an axis. Margins can also be negative or auto.

Sass mixins

Pass $negative: true, or use a minus-* mixin, for negative margins.

MixinCSS properties
m($key, $negative: false)margin
mt($key, $negative: false)margin-top
mb($key, $negative: false)margin-bottom
ml($key, $negative: false)margin-left
mr($key, $negative: false)margin-right
mx($key, $negative: false)margin-left, margin-right
my($key, $negative: false)margin-top, margin-bottom
minus-m($key), minus-mt($key), minus-mb($key)Negative all, top, or bottom margin
minus-ml($key), minus-mr($key)Negative left or right margin
minus-mx($key), minus-my($key)Negative horizontal or vertical margin
m-auto, mx-auto, my-autoAuto margin on all, horizontal, or vertical sides
mt-auto, mb-auto, ml-auto, mr-autoAuto margin on one side

Responsive range mixins

Pass a base size followed by named breakpoint overrides, such as crayon.m-range(4, $md: 8, $lg: 12).

MixinPurpose
m-range($base, $breakpoint-range...)Responsive margin
mt-range($base, $breakpoint-range...)Responsive top margin
mb-range($base, $breakpoint-range...)Responsive bottom margin
ml-range($base, $breakpoint-range...)Responsive left margin
mr-range($base, $breakpoint-range...)Responsive right margin
mx-range($base, $breakpoint-range...)Responsive horizontal margin
my-range($base, $breakpoint-range...)Responsive vertical margin

Utility classes

ClassProperty
m-0, m-nonemargin: 0
mx-0, mx-nonemargin-left: 0; margin-right: 0
my-0, my-nonemargin-top: 0; margin-bottom: 0
mt-0, mt-nonemargin-top: 0
mr-0, mr-nonemargin-right: 0
mb-0, mb-nonemargin-bottom: 0
ml-0, ml-nonemargin-left: 0
m-{size}margin: {size}
mx-{size}margin-left: {size}; margin-right: {size}
my-{size}margin-top: {size}; margin-bottom: {size}
mt-{size}margin-top: {size}
mr-{size}margin-right: {size}
mb-{size}margin-bottom: {size}
ml-{size}margin-left: {size}
-m-{size}margin: -{size}
-mx-{size}margin-left: -{size}; margin-right: -{size}
-my-{size}margin-top: -{size}; margin-bottom: -{size}
-mt-{size}margin-top: -{size}
-mr-{size}margin-right: -{size}
-mb-{size}margin-bottom: -{size}
-ml-{size}margin-left: -{size}
m-automargin: auto
mx-auto, center, center-xmargin-left: auto; margin-right: auto
my-auto, center-ymargin-top: auto; margin-bottom: auto
mt-automargin-top: auto
mr-automargin-right: auto
mb-automargin-bottom: auto
ml-automargin-left: auto

Examples

Sass mixins

scss
.card {
  @include crayon.m(4);
  @include crayon.mx-auto;
}
sass
.card
  +crayon.m(4)
  +crayon.mx-auto

Utility classes

html
<div class="m-4">Margin on all sides</div>
<div class="mx-auto">Centred with auto margins</div>
<div class="center">The same horizontal centering using an alias</div>
<div class="center-y">Automatic vertical margins</div>
<div class="-mt-4">Negative top margin</div>

Sass functions

scss
.card {
  margin: crayon.size(4);
  margin-top: -(crayon.size(2));
}
sass
.card
  margin: crayon.size(4)
  margin-top: -(crayon.size(2))

CSS variables

css
.card {
  margin: var(--size-4);
  margin-top: calc(var(--size-2) * -1);
}