Tailwind CSS space-x-2 Class
The space-x-2 utility class generates the following CSS when applied to an element.
CSS Output
.space-x-2 {
/* > :not([hidden]) ~ :not([hidden]) */
margin-left: 0.5rem; /* 8px */
}Variants
Use these variant prefixes to apply space-x-2 conditionally:
Use It
<div class="flex space-x-2">
<button class="px-3 py-1 bg-blue-500 text-white rounded">One</button>
<button class="px-3 py-1 bg-blue-500 text-white rounded">Two</button>
<button class="px-3 py-1 bg-blue-500 text-white rounded">Three</button>
</div>Understanding space-x-2
The Tailwind CSS space-x-2 utility applies /* > :not([hidden]) ~ :not([hidden]) */ to an element when added to its class attribute. It adds 8px of horizontal space between child elements using the lobotomized owl selector. A convenient alternative to gap for non-flex/grid 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 space-x-2 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:space-x-2, md:space-x-2, lg:space-x-2, and xl:space-x-2, allowing different behavior at each breakpoint. State variants like hover:space-x-2 and focus:space-x-2 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 `space-x-4`, `space-y-2`, `gap-2` 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.