1. 弹性盒子与网格
  2. 弹性

弹性盒子与网格

flex

用于控制 flex 项目如何增长和收缩的实用工具。

ClassStyles
flex-<number>
flex: <number>;
flex-<fraction>
flex: calc(<fraction> * 100%);
flex-auto
flex: 1 1 auto;
flex-initial
flex: 0 1 auto;
flex-none
flex: none;
flex-(<custom-property>)
flex: var(<custom-property>);
flex-[<value>]
flex: <value>;

示例

基本示例

使用 flex-<number> 工具,如 flex-1,以允许 flex 项目根据需要增长和收缩,忽略其初始大小:

01
02
03
<div class="flex">
<div class="w-14 flex-none ...">01</div>
<div class="w-64 flex-1 ...">02</div>
<div class="w-32 flex-1 ...">03</div>
</div>

初始

使用 flex-initial 允许一个 flex 项目收缩但不增长,考虑其初始大小:

01
02
03
<div class="flex">
<div class="w-14 flex-none ...">01</div>
<div class="w-64 flex-initial ...">02</div>
<div class="w-32 flex-initial ...">03</div>
</div>

自动

使用 flex-auto 允许一个 flex 项目增长和收缩,考虑其初始大小:

01
02
03
<div class="flex ...">
<div class="w-14 flex-none ...">01</div>
<div class="w-64 flex-auto ...">02</div>
<div class="w-32 flex-auto ...">03</div>
</div>

使用 flex-none 防止一个 flex 项目增长或收缩:

01
02
03
<div class="flex ...">
<div class="w-14 flex-none ...">01</div>
<div class="w-32 flex-none ...">02</div>
<div class="flex-1 ...">03</div>
</div>

使用自定义值

Use the flex-[<value>] syntax to set the flex 缩写属性 based on a completely custom value:

<div class="flex-[3_1_auto] ...">
<!-- ... -->
</div>

For CSS variables, you can also use the flex-(<custom-property>) syntax:

<div class="flex-(--my-flex) ...">
<!-- ... -->
</div>

This is just a shorthand for flex-[var(<custom-property>)] that adds the var() function for you automatically.

响应式设计

Prefix a flex utility with a breakpoint variant like md: to only apply the utility at medium screen sizes and above:

<div class="flex-none md:flex-1 ...">
<!-- ... -->
</div>

Learn more about using variants in the variants documentation.