Margin

Utilities for controlling an element's margin.

@include('_partials.class-table', [ 'scroll' => true, 'rows' => collect([ ['m', ['margin']], ['my', ['margin-top', 'margin-bottom']], ['mx', ['margin-right', 'margin-left']], ['mt', ['margin-top']], ['mr', ['margin-right']], ['mb', ['margin-bottom']], ['ml', ['margin-left']], ])->flatMap(function ($side) use ($page) { return $page->config['theme']['margin']->map(function ($value, $name) use ($side) { $class = starts_with($name, '-') ? ".-{$side[0]}-".substr($name, 1) : ".{$side[0]}-{$name}"; $code = collect($side[1])->map(function ($property) use ($value) { return "{$property}: {$value};"; })->implode("\n"); return [$class, $code]; })->values(); }) ])

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.

@component('_partials.code-sample')

mt-8

Target

mr-8

Target

mb-8

Target

ml-8

Target
@slot('code')
Target
Target
Target
Target
@endslot @endcomponent

Add horizontal margin

Control the horizontal margin of an element using the mx-{size} utilities.

@component('_partials.code-sample')

mx-8

Target
@slot('code')
Target
@endslot @endcomponent

Add vertical margin

Control the vertical margin of an element using the my-{size} utilities.

@component('_partials.code-sample')

my-8

Target
@slot('code')
Target
@endslot @endcomponent

Add margin to all sides

Control the margin on all sides of an element using the m-{size} utilities.

@component('_partials.code-sample')

m-8

Target
@slot('code')
Target
@endslot @endcomponent

Negative margins

Control the negative margin of an element using the -m{side?}-{size} utilities.

@component('_partials.code-sample')

-mt-8
@slot('code')
-mt-8
@endslot @endcomponent

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.

@component('_partials.responsive-code-sample', ['class' => 'text-center']) @slot('none')

Target
@endslot

@slot('sm')

Target
@endslot

@slot('md')

Target
@endslot

@slot('lg')

Target
@endslot

@slot('xl')

Target
@endslot

@slot('code')

Target
@endslot @endcomponent

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.

@component('_partials.customized-config', ['key' => 'theme.spacing'])

  • sm: '8px',
  • md: '16px',
  • lg: '24px',
  • xl: '48px', @endcomponent

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

@component('_partials.customized-config', ['key' => 'theme.margin'])

  • sm: '8px',
  • md: '16px',
  • lg: '24px',
  • xl: '48px', @endcomponent

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:

@component('_partials.customized-config', ['key' => 'theme.margin'])

  • '-72': '-18rem', @endcomponent

Tailwind is smart enough to generate classes like -mx-72 when it sees the leading dash, not mx--72 like you might expect.

@include('_partials.variants-and-disabling', [ 'utility' => [ 'name' => 'margin', 'property' => 'margin', ], 'variants' => [ 'responsive', ], ])

Tailwind UI is now in early access!