Spacing

Tailwind CSS mx-auto Class

The mx-auto utility class generates the following CSS when applied to an element.

CSS Output

CSS
.mx-auto {
  margin-left: auto;
  margin-right: auto;
}

Variants

Use these variant prefixes to apply mx-auto conditionally:

responsive:mx-autohover:mx-autofirst:mx-autolast:mx-auto

Use It

HTML
<div class="mx-auto max-w-md p-4">
  Horizontally centered container
</div>

Understanding mx-auto

The Tailwind CSS mx-auto utility applies margin-left: auto; to an element when added to its class attribute. It sets left and right margins to auto, centering a block-level element horizontally. The most common way to center fixed-width containers.

This utility is part of Tailwind's Spacing module, designed for managing margins and padding to establish consistent whitespace and visual rhythm. In Tailwind's utility-first workflow, you add mx-auto directly to your HTML elements rather than writing custom CSS. This approach accelerates development and keeps styles co-located with your markup, making it easy to see exactly how each element is styled at a glance.

Common responsive variants include sm:mx-auto, md:mx-auto, lg:mx-auto, and xl:mx-auto, allowing different behavior at each breakpoint. State variants like hover:mx-auto and focus:mx-auto enable interactive styling without any JavaScript. You can also combine multiple variants for fine-grained control over when the utility applies.

This class works well alongside `m-auto`, `my-4`, `container`, `w-full` to build complete, production-ready interfaces. Tailwind's tree-shaking ensures only utilities you actually use appear in your final CSS bundle, keeping file sizes minimal. Browser support for the underlying CSS is excellent across Chrome, Firefox, Safari, and Edge.

Related Classes

Explore More Tailwind Classes

Browse our complete reference of 321 Tailwind CSS utility classes with CSS output, variants, and examples.