Tailwind CSS Space Between
Spacing plays a vital role in creating visually structured and accessible web layouts. The space-* utilities in Tailwind CSS allow you to manage horizontal and vertical gaps between sibling elements effortlessly.
In this guide, we'll explore how to use the space-* utilities effectively, understand their constraints, apply them based on states or breakpoints, and even customize them for specific projects.
| Class | Properties | Example |
|---|---|---|
space-x-0 | margin-left: 0px; | <div className="space-x-0"></div> |
space-y-0 | margin-top: 0px; | <div className="space-y-0"></div> |
space-x-0.5 | margin-left: 0.125rem; | <div className="space-x-0.5"></div> |
space-y-0.5 | margin-top: 0.125rem; | <div className="space-y-0.5"></div> |
space-x-1 | margin-left: 0.25rem; | <div className="space-x-1"></div> |
space-y-1 | margin-top: 0.25rem; | <div className="space-y-1"></div> |
space-x-1.5 | margin-left: 0.375rem; | <div className="space-x-1.5"></div> |
space-y-1.5 | margin-top: 0.375rem; | <div className="space-y-1.5"></div> |
space-x-2 | margin-left: 0.5rem; | <div className="space-x-2"></div> |
space-y-2 | margin-top: 0.5rem; | <div className="space-y-2"></div> |
space-x-2.5 | margin-left: 0.625rem; | <div className="space-x-2.5"></div> |
space-y-2.5 | margin-top: 0.625rem; | <div className="space-y-2.5"></div> |
space-x-3 | margin-left: 0.75rem; | <div className="space-x-3"></div> |
space-y-3 | margin-top: 0.75rem; | <div className="space-y-3"></div> |
space-x-3.5 | margin-left: 0.875rem; | <div className="space-x-3.5"></div> |
space-y-3.5 | margin-top: 0.875rem; | <div className="space-y-3.5"></div> |
space-x-4 | margin-left: 1rem; | <div className="space-x-4"></div> |
space-y-4 | margin-top: 1rem; | <div className="space-y-4"></div> |
space-x-5 | margin-left: 1.25rem; | <div className="space-x-5"></div> |
space-y-5 | margin-top: 1.25rem; | <div className="space-y-5"></div> |
space-x-6 | margin-left: 1.5rem; | <div className="space-x-6"></div> |
space-y-6 | margin-top: 1.5rem; | <div className="space-y-6"></div> |
space-x-7 | margin-left: 1.75rem; | <div className="space-x-7"></div> |
space-y-7 | margin-top: 1.75rem; | <div className="space-y-7"></div> |
space-x-8 | margin-left: 2rem; | <div className="space-x-8"></div> |
space-y-8 | margin-top: 2rem; | <div className="space-y-8"></div> |
space-x-9 | margin-left: 2.25rem; | <div className="space-x-9"></div> |
space-y-9 | margin-top: 2.25rem; | <div className="space-y-9"></div> |
space-x-10 | margin-left: 2.5rem; | <div className="space-x-10"></div> |
space-y-10 | margin-top: 2.5rem; | <div className="space-y-10"></div> |
space-x-11 | margin-left: 2.75rem; | <div className="space-x-11"></div> |
space-y-11 | margin-top: 2.75rem; | <div className="space-y-11"></div> |
space-x-12 | margin-left: 3rem; | <div className="space-x-12"></div> |
space-y-12 | margin-top: 3rem; | <div className="space-y-12"></div> |
space-x-14 | margin-left: 3.5rem; | <div className="space-x-14"></div> |
space-y-14 | margin-top: 3.5rem; | <div className="space-y-14"></div> |
space-x-16 | margin-left: 4rem; | <div className="space-x-16"></div> |
space-y-16 | margin-top: 4rem; | <div className="space-y-16"></div> |
space-x-20 | margin-left: 5rem; | <div className="space-x-20"></div> |
space-y-20 | margin-top: 5rem; | <div className="space-y-20"></div> |
space-x-24 | margin-left: 6rem; | <div className="space-x-24"></div> |
space-y-24 | margin-top: 6rem; | <div className="space-y-24"></div> |
space-x-28 | margin-left: 7rem; | <div className="space-x-28"></div> |
space-y-28 | margin-top: 7rem; | <div className="space-y-28"></div> |
space-x-32 | margin-left: 8rem; | <div className="space-x-32"></div> |
space-y-32 | margin-top: 8rem; | <div className="space-y-32"></div> |
space-x-36 | margin-left: 9rem; | <div className="space-x-36"></div> |
space-y-36 | margin-top: 9rem; | <div className="space-y-36"></div> |
space-x-40 | margin-left: 10rem; | <div className="space-x-40"></div> |
space-y-40 | margin-top: 10rem; | <div className="space-y-40"></div> |
space-x-44 | margin-left: 11rem; | <div className="space-x-44"></div> |
space-y-44 | margin-top: 11rem; | <div className="space-y-44"></div> |
space-x-48 | margin-left: 12rem; | <div className="space-x-48"></div> |
space-y-48 | margin-top: 12rem; | <div className="space-y-48"></div> |
space-x-52 | margin-left: 13rem; | <div className="space-x-52"></div> |
space-y-52 | margin-top: 13rem; | <div className="space-y-52"></div> |
space-x-56 | margin-left: 14rem; | <div className="space-x-56"></div> |
space-y-56 | margin-top: 14rem; | <div className="space-y-56"></div> |
space-x-60 | margin-left: 15rem; | <div className="space-x-60"></div> |
space-y-60 | margin-top: 15rem; | <div className="space-y-60"></div> |
space-x-64 | margin-left: 16rem; | <div className="space-x-64"></div> |
space-y-64 | margin-top: 16rem; | <div className="space-y-64"></div> |
space-x-72 | margin-left: 18rem; | <div className="space-x-72"></div> |
space-y-72 | margin-top: 18rem; | <div className="space-y-72"></div> |
space-x-80 | margin-left: 20rem; | <div className="space-x-80"></div> |
space-y-80 | margin-top: 20rem; | <div className="space-y-80"></div> |
space-x-96 | margin-left: 24rem; | <div className="space-x-96"></div> |
space-y-96 | margin-top: 24rem; | <div className="space-y-96"></div> |
space-x-px | margin-left: 1px; | <div className="space-x-px"></div> |
space-y-px | margin-top: 1px; | <div className="space-y-px"></div> |
space-y-reverse | --tw-space-y-reverse: 1; | <div className="space-y-reverse"></div> |
space-x-reverse | --tw-space-x-reverse: 1; | <div className="space-x-reverse"></div> |
Overview of space
Horizontal Spacing
To add horizontal spacing between sibling elements, use the space-x-* class.
export default function App() { return <h1>Hello world</h1> }
Vertical Spacing
To add vertical spacing between sibling elements, use the space-y-* class.
export default function App() { return <h1>Hello world</h1> }
Reverse Order
When using flex-row-reverse or flex-col-reverse to show elements in the reverse order, you can use the space-x-reverse or space-y-reverse utilities to add the space as per the order.
export default function App() { return <h1>Hello world</h1> }
Negative Spacing
Negative values can also be applied to invert the spacing between sibling elements. Add - before a space utility to use its negative value.
export default function App() { return <h1>Hello world</h1> }
Limitations of Space
When working with space-* utilities, it’s essential to understand that they cannot be used in conjunction with Tailwind’s divide-* utilities. Both of these serve similar purposes, but conflicts arise due to differences in implementation at the CSS level.
The limitation stems from the fact that space-* applies margins directly, while divide-* elements apply a border between components. For unique styling requirements, consider using one of these utilities based on the design requirements.
States and Responsiveness
Hover and Focus States
Tailwind allows you to conditionally apply the space on certain states like hover and focus. Use Tailwind's state modifiers like- hover, focus, etc. to apply the utility only when these states are active, e.g., hover:space-x-4.
export default function App() { return <h1>Hello world</h1> }
Breakpoint Modifiers
Tailwind CSS provides breakpoint modifiers to conditionally apply the space only when the screen hits the defined breakpoint. Use Tailwind's breakpoint modifiers like- sm, md, etc., to apply the utility only on these breakpoints and above, e.g., md:space-x-4.
export default function App() { return <h1>Hello world</h1> }
Custom Space Between
Extending the Theme
Tailwind lets you customize the theme file to create new utilities beyond the default ones. Inside your tailwind.config.js, modify the spacing property under the theme.extend configuration.
export default function App() { return <h1>Hello world</h1> }
Using Arbitrary Values
Tailwind also lets you define arbitrary values directly in your classes for one-off use cases. Just use the square bracket syntax [value] wherever you need it, e.g., space-x-[15px].
export default function App() { return <h1>Hello world</h1> }
Real World Examples
Product Gallery Grid with Dynamic Spacing
A responsive product gallery grid with dynamic spacing between items. Ideal for showcasing featured products with consistent spacing.
export default function App() { return <h1>Hello world</h1> }
Vertical Timeline with Spaced Events
A vertical timeline component showing events with consistent spacing between elements.
export default function App() { return <h1>Hello world</h1> }
Team Member Cards with Bio
A grid of team member cards with consistent spacing between elements.
export default function App() { return <h1>Hello world</h1> }
Feature List with Icons
A list of features with consistent spacing and icon alignment.
export default function App() { return <h1>Hello world</h1> }
Pricing Comparison Table
A pricing table with consistent spacing between elements and features.
export default function App() { return <h1>Hello world</h1> }
Customization Examples
Responsive Card Grid with Custom Spacing
This example demonstrates a responsive grid of cards with custom spacing values defined in the theme configuration.
export default function App() { return <h1>Hello world</h1> }
Dashboard Layout with Custom Spacing
This example shows a dashboard layout with custom spacing for sections and components.
export default function App() { return <h1>Hello world</h1> }
Blog Post Layout with Custom Spacing
This example showcases a blog post layout with custom spacing for content elements.
export default function App() { return <h1>Hello world</h1> }
Best Practices
Maintain Design Consistency
When using space-*, it is crucial to establish a spacing scale that aligns with the overall design system.
Define a global spacing strategy to prevent misalignment and visual clutter. For example, if buttons, cards, and headings follow a specific spacing pattern, they should be consistently applied across all sections to avoid an inconsistent look.
Another technique for maintaining uniformity is creating reusable components with predefined spacing values. By encapsulating spacing within reusable components, you can ensure that spacing decisions remain consistent across different parts of the project.
Balance with Other Layout Properties
When using space-* within a flex container, pair it with justify-* utilities to help maintain the intended structure while avoiding excessive spacing.
Additionally, spacing should be adjusted based on content hierarchy. Primary elements such as headings and sections often require more spacing than secondary elements like buttons or captions. Use higher space values for major sections and lower space for smaller elements to ensure a logical structure while keeping the interface visually appealing.
Accessibility Considerations
Enhance Readability and Navigability
Proper spacing significantly impacts readability and content flow. Using space-* to provide sufficient separation between lines of text, buttons, or form fields ensures users can process information without feeling overwhelmed. When spacing is too tight, content becomes difficult to scan, whereas excessive spacing disrupts the natural reading rhythm.
Navigation menus also benefit from well-structured spacing. Applying space-y-4 to vertical menus or space-x-6 to horizontal ones improves clarity, making it easier for users to distinguish between options. When working with dense UI layouts, moderate spacing helps prevent cognitive overload while maintaining a logical structure.
Focus on High Contrast
Spacing plays a role in enhancing contrast, particularly when dealing with background and foreground elements. Proper separation between text blocks and interactive elements ensures they remain distinguishable, improving accessibility for visually impaired users. For example, using space-y-5 between form inputs prevents crowding, allowing each field to stand out clearly.
High contrast is especially important for interactive elements such as buttons or links. Sufficient spacing ensures they do not visually merge, reducing the risk of misclicks. When paired with appropriate text colors and backgrounds, well-implemented spacing improves usability for users with low vision or color blindness.