> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# 弹性布局

如果你需要创建一个响应式布局，如增加子元件尺寸以填满未使用的空间，或收缩子元件尺寸以避免溢出，请选择**弹性布局（flex）**。这是一种 Web 中常见的布局方式。
只需在希望应用弹性布局的元件上设置 `display: flex`，即会对其所有直接子元件进行弹性布局。

::: info
要了解更多信息，请查阅 MDN 上的 [CSS 弹性盒子布局](https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_flexible_box_layout)。Lynx 支持了常见的弹性布局属性，并在绝大多数情况下与 Web 标准保持一致。有关支持的属性，请参见[参考](#参考)部分。
:::

**以下示例展示了弹性布局的典型特性。**

## 典型特性

### 通过 `flex-grow` 填充父元件

[`flex-grow`] 属性帮助你将父元件的剩余空间按照 `flex-grow` 声明的权重分配给子元件的大小。

**下面是一个示例: layout**

**Entry:** `src/flex_grow`
**Bundle:** `dist/flex_grow.lynx.bundle` | Web: `dist/flex_grow.web.bundle`

```tsx
import { root } from "@lynx-js/react";

import "./index.scss";

const FlexGrowExample = () => {
  return (
    <scroll-view>
      <text className="title">
        flex-direction: column
      </text>
      <view className="container">
        <view className="item1">
          <text className="text">flex-grow: 1</text>
        </view>
        <view className="item2">
          <text className="text">flex-grow: 2</text>
        </view>
        <view className="item3">
          <text className="text">100px</text>
        </view>
      </view>
    </scroll-view>
  );
};

root.render(<FlexGrowExample />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```

v0.7.1



### 通过 `flex-shrink` 压缩子元件

使用 [`flex-shrink`] 属性，可以在你的子元件将要溢出父元件时，按照 `flex-shrink` 声明的权重压缩元件，从而适应父元件大小。

**下面是一个示例: layout**

**Entry:** `src/flex_shrink`
**Bundle:** `dist/flex_shrink.lynx.bundle` | Web: `dist/flex_shrink.web.bundle`

```tsx
import { root } from "@lynx-js/react";

import "./index.scss";

const FlexShrinkExample = () => {
  return (
    <scroll-view>
      <text className="title">
        flex-direction: column
      </text>
      <view className="container">
        <view className="item1">
          <text className="text">flex-shrink: 0</text>
          <text className="text">height 300px</text>
        </view>
        <view className="item2">
          <text className="text">flex-shrink: 1</text>
          <text className="text">height shrinks from 300px to fit container</text>
        </view>
      </view>
    </scroll-view>
  );
};

root.render(<FlexShrinkExample />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```

v0.7.1




> **Lynx 在子元件压缩的最小值上和 Web 的表现有差异。**
> 
> Lynx 当前不支持 [`min-content`](https://developer.mozilla.org/en-US/docs/Web/CSS/min-content)，因此暂时视作 `0px`。这意味着，当需要压缩子元件大小以适应父元件时，Web 可以保证子元件不小于其最小内容宽度，但 Lynx 目前无法保证这点。
> 
> 代码
> 
> ```html
> <div
>   style="width:100px;
>             height:70px;
>             display:flex;
>             background-color:rgb(0, 235, 235);"
> >
>   <div
>     style="display:flex;
>               height:50px;
>               background-color:rgb(255, 53, 26);"
>   >
>     <div
>       style="width:150px;
>                 height:50px;"
>     ></div>
>   </div>
> </div>
> ```
> 
> Web 和 Lynx 表现不一致
> 
> ![](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/lynx-website/assets/doc/layout/layout_min_content.png)


### 通过 `flex-wrap` 进行换行

[`flex-wrap`] 属性可以将一行显示不下的内容展示到下一行。该属性指定弹性元件是单行显示还是多行显示。当允许换行时，该属性可以控制行的堆叠方向。

**下面是一个示例: layout**

**Entry:** `src/flex_wrap`
**Bundle:** `dist/flex_wrap.lynx.bundle` | Web: `dist/flex_wrap.web.bundle`

```tsx
import { root } from "@lynx-js/react";

import "./index.scss";

const FlexWrapExample = () => {
  return (
    <scroll-view>
      <text className="title">
        flex-direction: row;
      </text>
      <text className="title">
        flex-wrap: wrap;
      </text>
      <view className="container" style={{ flexWrap: "wrap" }}>
        <view className="item">
          <text className="text">Item 1</text>
        </view>
        <view className="item" style={{ backgroundColor: "rgb(0,235,235)" }}>
          <text className="text">Item 2</text>
        </view>
        <view className="item">
          <text className="text">Item 3</text>
        </view>
      </view>

      <text className="title">
        flex-direction: row;
      </text>
      <text className="title">
        flex-wrap: nowrap;
      </text>
      <view className="container" style={{ flexWrap: "nowrap" }}>
        <view className="item">
          <text className="text">Item 1</text>
        </view>
        <view className="item" style={{ backgroundColor: "rgb(0,235,235)" }}>
          <text className="text">Item 2</text>
        </view>
        <view className="item">
          <text className="text">Item 3</text>
        </view>
      </view>
    </scroll-view>
  );
};

root.render(<FlexWrapExample />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```

v0.7.1



## 参考

目前，Lynx 支持以下常见的弹性布局属性：

- **CSS 属性**
  - [`flex`](/next/zh/api/css/properties/flex.md)
  - [`flex-basis`](/next/zh/api/css/properties/flex-basis.md)
  - [`flex-direction`](/next/zh/api/css/properties/flex-direction.md)
  - [`flex-flow`](/next/zh/api/css/properties/flex-flow.md)
  - [`flex-grow`](/next/zh/api/css/properties/flex-grow.md)
  - [`flex-shrink`](/next/zh/api/css/properties/flex-shrink.md)
  - [`flex-wrap`](/next/zh/api/css/properties/flex-wrap.md)
  - [`order`](/next/zh/api/css/properties/order.md)

- **对齐属性**
  - [`align-content`](/next/zh/api/css/properties/align-content.md)
  - [`align-items`](/next/zh/api/css/properties/align-items.md)
  - [`align-self`](/next/zh/api/css/properties/align-self.md)
  - [`justify-content`](/next/zh/api/css/properties/justify-content.md)
  - [`row-gap`](/next/zh/api/css/properties/row-gap.md)
  - [`column-gap`](/next/zh/api/css/properties/column-gap.md)
  - [`gap`](/next/zh/api/css/properties/gap.md)

要了解更多用法，请参考 MDN 上的 [CSS 弹性盒子布局](https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_flexible_box_layout)。

[`flex-grow`]: /next/zh/api/css/properties/flex-grow.md

[`flex-shrink`]: /next/zh/api/css/properties/flex-shrink.md

[`flex-wrap`]: /next/zh/api/css/properties/flex-wrap.md
