Spacing
Utilities for controlling the space between child elements.
Class | Styles |
---|---|
space-x-<number> | margin-inline-start: calc(calc(var(--spacing) * <number>) * var(--tw-space-x-reverse));
margin-inline-end: calc(calc(var(--spacing) * <number>) * calc(<number> - var(--tw-space-x-reverse))); |
space-y-<number> | margin-block-start: calc(calc(var(--spacing) * <number>) * var(--tw-space-x-reverse));
margin-block-end: calc(calc(var(--spacing) * <number>) * calc(<number> - var(--tw-space-x-reverse))); |
space-x-reverse | --tw-space-y-reverse: 1; |
space-y-reverse | --tw-space-y-reverse: 1; |
space-x-(<custom-property>) | margin-left: var(<custom-property>); |
space-y-(<custom-property>) | margin-top: var(<custom-property>); |
space-x-[<value>] | margin-left: <value>; |
space-y-[<value>] | margin-top: <value>; |
Use the space-x-*
utilities to control the horizontal space between elements:
Use the space-y-*
utilities to control the vertical space between elements:
If your elements are in reverse order (using say flex-row-reverse
or flex-col-reverse
), use the space-x-reverse
or space-y-reverse
utilities to ensure the space is added to the correct side of each element:
To use a negative space value, prefix the class name with a dash to convert it to a negative value:
<div class="flex -space-x-4 ..."> <!-- ... --></div>
These utilities are really just a shortcut for adding margin to all-but-the-first-item in a group, and aren't designed to handle complex cases like grids, layouts that wrap, or situations where the children are rendered in a complex custom order rather than their natural DOM order.
For those situations, it's better to use the gap utilities when possible, or add margin to every element with a matching negative margin on the parent:
<div class="flow-root"> <div class="-m-2 flex flex-wrap"> <div class="m-2 ..."></div> <div class="m-2 ..."></div> <div class="m-2 ..."></div> </div></div>
The space-*
utilities are not designed to work together with the divide utilities. For those situations, consider adding margin/padding utilities to the children instead.
Use the space-(<custom-property>)
syntax to set the space based on a custom property:
<div class="space-(--my-space) ..."> <!-- ... --></div>
Use the space-[<value>]
syntax to set the space based on a completely custom value:
<div class="space-[5px] ..."> <!-- ... --></div>
Prefix a space
utility with a breakpoint variant like md:
to only apply the utility at medium screen sizes and above:
<div class="space-x-2 md:space-x-8"> <!-- ... --></div>
Learn more about using variants in the variants documentation.
Use the --space-*
theme variables to customize the space utilities in your project:
@theme { --space-5px: 5px; }
Now the space-5px
utility can be used in your markup:
<div class="space-5px"> <!-- ... --></div>
Learn more about customizing your theme in the theme documentation.