- 入门
- 核心概念
- 定制
- Base Styles
- 布局
- Flexbox & Grid
- Flex Basis
- Flex Direction
- Flex Wrap
- Flex
- Flex Grow
- Flex Shrink
- Order
- Grid Template Columns
- Grid Column Start / End
- Grid Template Rows
- Grid Row Start / End
- Grid Auto Flow
- Grid Auto Columns
- Grid Auto Rows
- Gap
- Justify Content
- Justify Items
- Justify Self
- Align Content
- Align Items
- Align Self
- Place Content
- Place Items
- Place Self
- 间隔
- 尺寸
- 排版
- Font Family
- Font Size
- Font Smoothing
- Font Style
- Font Weight
- Font Variant Numeric
- Letter Spacing
- Line Clamp
- Line Height
- List Style Image
- List Style Position
- List Style Type
- Text Align
- Text Color
- Text Decoration
- Text Decoration Color
- Text Decoration Style
- Text Decoration Thickness
- Text Underline Offset
- Text Transform
- Text Overflow
- Text Wrap
- Text Indent
- Vertical Align
- Whitespace
- Word Break
- Hyphens
- Content
- 背景
- 边框
- Effects
- Filters
- 表格 Tables
- Transitions & Animation
- Transforms
- Interactivity
- SVG
- Accessibility
- 其他
- Install Tailwind CSS using PostCSS
- Framework Guides
- Try Tailwind CSS using the Play CDN
- Install Tailwind CSS with Next.js
- Install Tailwind CSS with Laravel
- Install Tailwind CSS with Vite
- Install Tailwind CSS with Nuxt
- Install Tailwind CSS with Gatsby
- Install Tailwind CSS with SolidJS
- Install Tailwind CSS with SvelteKit
- Install Tailwind CSS with Angular
- Install Tailwind CSS with Ruby on Rails
- Install Tailwind CSS with Remix
- Install Tailwind CSS with Phoenix
- Install Tailwind CSS with Parcel
- Install Tailwind CSS with Symfony
- Install Tailwind CSS with Meteor
- Install Tailwind CSS with Create React App
- Install Tailwind CSS with Adonis
- Install Tailwind CSS With Ember.js
- Install Tailwind CSS with Astro
- Install Tailwind CSS with Qwik
- Install Tailwind CSS with Rspack
Space Between
Utilities for controlling the space between child elements.
Quick reference
Class | Properties |
---|---|
space-x-0 > * + * | margin-left: 0px; |
space-y-0 > * + * | margin-top: 0px; |
space-x-0.5 > * + * | margin-left: 0.125rem; /* 2px */ |
space-y-0.5 > * + * | margin-top: 0.125rem; /* 2px */ |
space-x-1 > * + * | margin-left: 0.25rem; /* 4px */ |
space-y-1 > * + * | margin-top: 0.25rem; /* 4px */ |
space-x-1.5 > * + * | margin-left: 0.375rem; /* 6px */ |
space-y-1.5 > * + * | margin-top: 0.375rem; /* 6px */ |
space-x-2 > * + * | margin-left: 0.5rem; /* 8px */ |
space-y-2 > * + * | margin-top: 0.5rem; /* 8px */ |
space-x-2.5 > * + * | margin-left: 0.625rem; /* 10px */ |
space-y-2.5 > * + * | margin-top: 0.625rem; /* 10px */ |
space-x-3 > * + * | margin-left: 0.75rem; /* 12px */ |
space-y-3 > * + * | margin-top: 0.75rem; /* 12px */ |
space-x-3.5 > * + * | margin-left: 0.875rem; /* 14px */ |
space-y-3.5 > * + * | margin-top: 0.875rem; /* 14px */ |
space-x-4 > * + * | margin-left: 1rem; /* 16px */ |
space-y-4 > * + * | margin-top: 1rem; /* 16px */ |
space-x-5 > * + * | margin-left: 1.25rem; /* 20px */ |
space-y-5 > * + * | margin-top: 1.25rem; /* 20px */ |
space-x-6 > * + * | margin-left: 1.5rem; /* 24px */ |
space-y-6 > * + * | margin-top: 1.5rem; /* 24px */ |
space-x-7 > * + * | margin-left: 1.75rem; /* 28px */ |
space-y-7 > * + * | margin-top: 1.75rem; /* 28px */ |
space-x-8 > * + * | margin-left: 2rem; /* 32px */ |
space-y-8 > * + * | margin-top: 2rem; /* 32px */ |
space-x-9 > * + * | margin-left: 2.25rem; /* 36px */ |
space-y-9 > * + * | margin-top: 2.25rem; /* 36px */ |
space-x-10 > * + * | margin-left: 2.5rem; /* 40px */ |
space-y-10 > * + * | margin-top: 2.5rem; /* 40px */ |
space-x-11 > * + * | margin-left: 2.75rem; /* 44px */ |
space-y-11 > * + * | margin-top: 2.75rem; /* 44px */ |
space-x-12 > * + * | margin-left: 3rem; /* 48px */ |
space-y-12 > * + * | margin-top: 3rem; /* 48px */ |
space-x-14 > * + * | margin-left: 3.5rem; /* 56px */ |
space-y-14 > * + * | margin-top: 3.5rem; /* 56px */ |
space-x-16 > * + * | margin-left: 4rem; /* 64px */ |
space-y-16 > * + * | margin-top: 4rem; /* 64px */ |
space-x-20 > * + * | margin-left: 5rem; /* 80px */ |
space-y-20 > * + * | margin-top: 5rem; /* 80px */ |
space-x-24 > * + * | margin-left: 6rem; /* 96px */ |
space-y-24 > * + * | margin-top: 6rem; /* 96px */ |
space-x-28 > * + * | margin-left: 7rem; /* 112px */ |
space-y-28 > * + * | margin-top: 7rem; /* 112px */ |
space-x-32 > * + * | margin-left: 8rem; /* 128px */ |
space-y-32 > * + * | margin-top: 8rem; /* 128px */ |
space-x-36 > * + * | margin-left: 9rem; /* 144px */ |
space-y-36 > * + * | margin-top: 9rem; /* 144px */ |
space-x-40 > * + * | margin-left: 10rem; /* 160px */ |
space-y-40 > * + * | margin-top: 10rem; /* 160px */ |
space-x-44 > * + * | margin-left: 11rem; /* 176px */ |
space-y-44 > * + * | margin-top: 11rem; /* 176px */ |
space-x-48 > * + * | margin-left: 12rem; /* 192px */ |
space-y-48 > * + * | margin-top: 12rem; /* 192px */ |
space-x-52 > * + * | margin-left: 13rem; /* 208px */ |
space-y-52 > * + * | margin-top: 13rem; /* 208px */ |
space-x-56 > * + * | margin-left: 14rem; /* 224px */ |
space-y-56 > * + * | margin-top: 14rem; /* 224px */ |
space-x-60 > * + * | margin-left: 15rem; /* 240px */ |
space-y-60 > * + * | margin-top: 15rem; /* 240px */ |
space-x-64 > * + * | margin-left: 16rem; /* 256px */ |
space-y-64 > * + * | margin-top: 16rem; /* 256px */ |
space-x-72 > * + * | margin-left: 18rem; /* 288px */ |
space-y-72 > * + * | margin-top: 18rem; /* 288px */ |
space-x-80 > * + * | margin-left: 20rem; /* 320px */ |
space-y-80 > * + * | margin-top: 20rem; /* 320px */ |
space-x-96 > * + * | margin-left: 24rem; /* 384px */ |
space-y-96 > * + * | margin-top: 24rem; /* 384px */ |
space-x-px > * + * | margin-left: 1px; |
space-y-px > * + * | margin-top: 1px; |
space-y-reverse > * + * | --tw-space-y-reverse: 1; |
space-x-reverse > * + * | --tw-space-x-reverse: 1; |
Basic usage
Add horizontal space between children
Use the space-x-*
utilities to control the horizontal space between elements.
<div class="flex space-x-4 ...">
<div>01</div>
<div>02</div>
<div>03</div>
</div>
Add vertical space between children
Use the space-y-*
utilities to control the vertical space between elements.
<div class="flex flex-col space-y-4 ...">
<div>01</div>
<div>02</div>
<div>03</div>
</div>
Reversing children order
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.
<div class="flex flex-row-reverse space-x-4 space-x-reverse ...">
<div>01</div>
<div>02</div>
<div>03</div>
</div>
Using negative values
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>
Limitations
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>
Cannot be paired with divide utilities
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.
Applying conditionally
Hover, focus, and other states
Tailwind lets you conditionally apply utility classes in different states using variant modifiers. For example, use hover:space-x-8
to only apply the space-x-8
utility on hover.
<div class="flex space-x-2 hover:space-x-8">
<!-- ... -->
</div>
For a complete list of all available state modifiers, check out the Hover, Focus, & Other States documentation.
Breakpoints and media queries
You can also use variant modifiers to target media queries like responsive breakpoints, dark mode, prefers-reduced-motion, and more. For example, use md:space-x-8
to apply the space-x-8
utility at only medium screen sizes and above.
<div class="flex space-x-2 md:space-x-8">
<!-- ... -->
</div>
To learn more, check out the documentation on Responsive Design, Dark Mode and other media query modifiers.
Using custom values
Customizing your theme
By default, Tailwind’s space scale uses the default spacing scale. You can customize your spacing scale by editing theme.spacing
or theme.extend.spacing
in your tailwind.config.js
file.
module.exports = {
theme: {
extend: {
spacing: {
'5px': '5px',
}
}
}
}
Alternatively, you can customize just the space scale by editing theme.space
or theme.extend.space
in your tailwind.config.js
file.
module.exports = {
theme: {
extend: {
space: {
'5px': '5px',
}
}
}
}
Learn more about customizing the default theme in the theme customization documentation.
Arbitrary values
If you need to use a one-off space value that doesn’t make sense to include in your theme, use square brackets to generate a property on the fly using any arbitrary value.
<div class="space-y-[5px]">
<!-- ... -->
</div>
Learn more about arbitrary value support in the arbitrary values documentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论