
间距
用于控制子元素之间空间的工具。
使用 space-x-* 工具来控制元素之间的水平间距。
🌐 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>
使用 space-y-* 工具来控制元素之间的垂直间距。
🌐 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>
如果你的元素顺序是反的(例如使用 flex-row-reverse 或 flex-col-reverse),请使用 space-x-reverse 或 space-y-reverse 工具确保空格添加到每个元素的正确一侧。
🌐 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>
要使用负间距值,在类名前加上短横线即可将其转换为负值。
🌐 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>
这些工具实际上只是为一组中除了第一个元素之外的所有元素添加外边距的快捷方式,并不用于处理复杂情况,比如网格、换行布局,或者子元素是以复杂自定义顺序而非自然 DOM 顺序渲染的情况。
🌐 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.
在这些情况下,最好在可能的情况下使用 gap 工具,或者在每个元素上添加外边距,并在父元素上使用对应的负外边距:
🌐 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>
space-* 工具不适用于与 divide 工具 一起使用。在这种情况下,可以考虑给子元素添加 margin/padding 工具。
🌐 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.
Tailwind 允许你使用变体修饰符在不同状态下有条件地应用工具类。例如,使用hover:space-x-8 仅在 hover 时应用 space-x-8 工具。
<div class="flex space-x-2 hover:space-x-8">
<!-- ... -->
</div>
有关所有可用状态修饰符的完整列表,请查看悬停、聚焦、以及其他状态 文档。
你还可以使用变体修饰符来定位媒体查询,例如响应式断点、暗黑模式、首选减少运动等。例如,使用 md:space-x-8 仅在中等屏幕尺寸及以上时应用 space-x-8 工具。
<div class="flex space-x-2 md:space-x-8">
<!-- ... -->
</div>
要了解更多信息,请查看有关 响应式设计、暗黑模式、以及 其他媒体查询修饰符 的文档。
默认情况下,Tailwind 的间距比例使用默认间距比例。你可以通过在 tailwind.config.js 文件中编辑 theme.spacing 或 theme.extend.spacing 来自定义你的间距比例。
🌐 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',
}
}
}
}
或者,你可以通过编辑 tailwind.config.js 文件中的 theme.space 或 theme.extend.space 来单独自定义间距比例。
🌐 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.
如果你需要使用一次性的 space 值,而该值没有必要包含在你的主题中,请使用方括号动态生成属性,使用任意值。
<div class="space-y-[5px]">
<!-- ... -->
</div>
在 任意值 文档中了解有关任意值支持的更多信息。