Kombai Logo

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.

ClassPropertiesExample
space-x-0margin-left: 0px;<div className="space-x-0"></div>
space-y-0margin-top: 0px;<div className="space-y-0"></div>
space-x-0.5margin-left: 0.125rem; <div className="space-x-0.5"></div>
space-y-0.5margin-top: 0.125rem; <div className="space-y-0.5"></div>
space-x-1margin-left: 0.25rem; <div className="space-x-1"></div>
space-y-1margin-top: 0.25rem; <div className="space-y-1"></div>
space-x-1.5margin-left: 0.375rem; <div className="space-x-1.5"></div>
space-y-1.5margin-top: 0.375rem; <div className="space-y-1.5"></div>
space-x-2margin-left: 0.5rem; <div className="space-x-2"></div>
space-y-2margin-top: 0.5rem; <div className="space-y-2"></div>
space-x-2.5margin-left: 0.625rem; <div className="space-x-2.5"></div>
space-y-2.5margin-top: 0.625rem; <div className="space-y-2.5"></div>
space-x-3margin-left: 0.75rem; <div className="space-x-3"></div>
space-y-3margin-top: 0.75rem; <div className="space-y-3"></div>
space-x-3.5margin-left: 0.875rem; <div className="space-x-3.5"></div>
space-y-3.5margin-top: 0.875rem; <div className="space-y-3.5"></div>
space-x-4margin-left: 1rem; <div className="space-x-4"></div>
space-y-4margin-top: 1rem; <div className="space-y-4"></div>
space-x-5margin-left: 1.25rem; <div className="space-x-5"></div>
space-y-5margin-top: 1.25rem; <div className="space-y-5"></div>
space-x-6margin-left: 1.5rem; <div className="space-x-6"></div>
space-y-6margin-top: 1.5rem; <div className="space-y-6"></div>
space-x-7margin-left: 1.75rem; <div className="space-x-7"></div>
space-y-7margin-top: 1.75rem; <div className="space-y-7"></div>
space-x-8margin-left: 2rem; <div className="space-x-8"></div>
space-y-8margin-top: 2rem; <div className="space-y-8"></div>
space-x-9margin-left: 2.25rem; <div className="space-x-9"></div>
space-y-9margin-top: 2.25rem; <div className="space-y-9"></div>
space-x-10margin-left: 2.5rem; <div className="space-x-10"></div>
space-y-10margin-top: 2.5rem; <div className="space-y-10"></div>
space-x-11margin-left: 2.75rem; <div className="space-x-11"></div>
space-y-11margin-top: 2.75rem; <div className="space-y-11"></div>
space-x-12margin-left: 3rem; <div className="space-x-12"></div>
space-y-12margin-top: 3rem; <div className="space-y-12"></div>
space-x-14margin-left: 3.5rem; <div className="space-x-14"></div>
space-y-14margin-top: 3.5rem; <div className="space-y-14"></div>
space-x-16margin-left: 4rem; <div className="space-x-16"></div>
space-y-16margin-top: 4rem; <div className="space-y-16"></div>
space-x-20margin-left: 5rem; <div className="space-x-20"></div>
space-y-20margin-top: 5rem; <div className="space-y-20"></div>
space-x-24margin-left: 6rem; <div className="space-x-24"></div>
space-y-24margin-top: 6rem; <div className="space-y-24"></div>
space-x-28margin-left: 7rem; <div className="space-x-28"></div>
space-y-28margin-top: 7rem; <div className="space-y-28"></div>
space-x-32margin-left: 8rem; <div className="space-x-32"></div>
space-y-32margin-top: 8rem; <div className="space-y-32"></div>
space-x-36margin-left: 9rem; <div className="space-x-36"></div>
space-y-36margin-top: 9rem; <div className="space-y-36"></div>
space-x-40margin-left: 10rem; <div className="space-x-40"></div>
space-y-40margin-top: 10rem; <div className="space-y-40"></div>
space-x-44margin-left: 11rem; <div className="space-x-44"></div>
space-y-44margin-top: 11rem; <div className="space-y-44"></div>
space-x-48margin-left: 12rem; <div className="space-x-48"></div>
space-y-48margin-top: 12rem; <div className="space-y-48"></div>
space-x-52margin-left: 13rem; <div className="space-x-52"></div>
space-y-52margin-top: 13rem; <div className="space-y-52"></div>
space-x-56margin-left: 14rem; <div className="space-x-56"></div>
space-y-56margin-top: 14rem; <div className="space-y-56"></div>
space-x-60margin-left: 15rem; <div className="space-x-60"></div>
space-y-60margin-top: 15rem; <div className="space-y-60"></div>
space-x-64margin-left: 16rem; <div className="space-x-64"></div>
space-y-64margin-top: 16rem; <div className="space-y-64"></div>
space-x-72margin-left: 18rem; <div className="space-x-72"></div>
space-y-72margin-top: 18rem; <div className="space-y-72"></div>
space-x-80margin-left: 20rem; <div className="space-x-80"></div>
space-y-80margin-top: 20rem; <div className="space-y-80"></div>
space-x-96margin-left: 24rem; <div className="space-x-96"></div>
space-y-96margin-top: 24rem; <div className="space-y-96"></div>
space-x-pxmargin-left: 1px;<div className="space-x-px"></div>
space-y-pxmargin-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.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Vertical Spacing

To add vertical spacing between sibling elements, use the space-y-* class.

This is a live editor. Play around with it!
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.

This is a live editor. Play around with it!
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.

This is a live editor. Play around with it!
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.

This is a live editor. Play around with it!
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.

This is a live editor. Play around with it!
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.

This is a live editor. Play around with it!
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].

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Real World Examples

A responsive product gallery grid with dynamic spacing between items. Ideal for showcasing featured products with consistent spacing.

This is a live editor. Play around with it!
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.

This is a live editor. Play around with it!
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.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Feature List with Icons

A list of features with consistent spacing and icon alignment.

This is a live editor. Play around with it!
export default function App() {
  return <h1>Hello world</h1>
}

Pricing Comparison Table

A pricing table with consistent spacing between elements and features.

This is a live editor. Play around with it!
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.

This is a live editor. Play around with it!
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.

This is a live editor. Play around with it!
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.

This is a live editor. Play around with it!
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.