> For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt, and this page is available as Markdown at /zh/guide/ui/layout/index.md.

# 理解布局

Lynx 提供了：

- [`width`]、[`height`]、[`margin`]、[`padding`] 等属性，用于描述元件的大小。
- [`display`] 属性及[线性布局]、[弹性布局]、[网格布局]、[相对布局]，用于对元件进行布局。
- 包含 [`align-items`]、[`justify-content`] 等的对齐属性，用于对齐元件。
- [`position`] 及 [`left`]、[`right`]、[`top`]、[`bottom`] 属性，用于定位元件。
- [`direction`] 及[逻辑属性]，用于支持布局的国际化。

Lynx 支持的布局属性与 Web 中同名的属性在行为上表现一致。但两者设计思路存在差异：Web 的布局是以文本为主体，而 Lynx 的布局则是以**元件**（`<view>`、`<text>`、`<image>` 等）作为基本布局单位。这意味着 Lynx 的布局元件都是[块级元件](https://developer.mozilla.org/zh-CN/docs/Glossary/Block-level_content)。

下面教程将展示如何在 Lynx 中完成元件的布局。

## 调整元件大小

你可以使用 [`width`]、[`height`]、[`max-width`]、[`min-width`]、[`max-height`]、[`min-height`]、[`margin`]、[`padding`] 和 [`border-width`] 描述元件大小与盒模型，这些属性支持 `px`、`%`、`vh` 等多种[长度单位](/zh/api/css/data-type/length.md#单位)。同时 `width` 和 `height` 支持 [`fit-content`] 和 [`max-content`] 的取值，用于元件的大小根据其内容自行撑开。

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

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

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

import "./index.scss";

const SizingExample = () => {
  return (
    <scroll-view>
      <text className="box">Auto Sized Element</text>
      <text className="box" style={{ width: "fit-content" }}>
        Size Decided by Content
      </text>
      <text className="box" style={{ width: "150px", height: "150px" }}>
        Size as Specified
      </text>
      <text className="box" style={{ width: "50%" }}>
        Specify Size with Percentage
      </text>
      <text className="box" style={{ maxWidth: "100px" }}>
        Limited By Max Size
      </text>
      <text className="box" style={{ minHeight: "100px" }}>
        Expanded By Min Size
      </text>
    </scroll-view>
  );
};

root.render(<SizingExample />);

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

```



在 Lynx 中，`box-sizing` 的默认值为 `border-box`，且不具备外边距折叠的表现。Lynx 的大小属性 `width`、`height`、`max-width`
等默认情况下描述的是含边框大小，即包含 `border` 与 `padding` 的内容大小，和
Web 默认表现不一致。 同时，Lynx 不具备 Web 的
[外边距折叠](https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_box_model/Mastering_margin_collapsing)
的表现。

## 布局元件

[`<view>`] 元件可以用于布局子元件，且可以通过在 `<view>` 元件上设置 [`display`] 属性，来控制其对子元件的布局方式。`display` 支持 `linear`、`flex`、`grid`、`relative`、`none` 五种取值。

### 线性布局（linear）

如果你想要简单的按顺序排列元件。则可以将 [`display`] 属性设置为 `linear`，使用 Lynx 的默认布局--[线性布局]。线性布局（受 Android [Linear Layout](https://developer.android.com/develop/ui/views/layout/linear) 的启发）可以按照你声明的方向将元件按顺序排列。

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

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

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

import "./index.scss";

const LinearExample = () => {
  return (
    <view className="column">
      <text className="item">column item 1</text>
      <text className="item">column item 2</text>
      <text className="item">column item 3</text>
      <view className="row">
        <text className="item">row item 1</text>
        <text className="item">row item 2</text>
        <text className="item">row item 3</text>
      </view>
    </view>
  );
};

root.render(<LinearExample />);

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

```



### 弹性布局（flex）

如果需要在顺序排列元件的基础上，让子元件的大小自适应父元件的大小（如增加子元件尺寸以填满未使用的空间，收缩子元件尺寸以避免溢出），则可以将 [`display`] 属性设置为 `flex`，使用[弹性布局]。Lynx 中的弹性布局和 Web 中的功能和行为上一致。

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

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

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

import "./index.scss";

const FlexExample = () => {
  return (
    <view>
      <view className="flex">
        <text className="item">item</text>
        <text className="item" style={{ flexGrow: 1 }}>
          grow to fill
        </text>
      </view>
      <view className="flex">
        <text className="item" style={{ flexShrink: 0, width: "75%" }}>
          large item
        </text>
        <text className="item" style={{ flexShrink: 1 }}>
          shrink to fit
        </text>
      </view>
    </view>
  );
};

root.render(<FlexExample />);

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

```



### 网格布局（grid）

如果想要将多个元件同时在垂直和水平方向上交错排列，形成一个二维的布局方式，则可以将 [`display`] 属性设置为 `grid`，使用[网格布局]。网格布局是 Web 中的一种布局，可以将空间划分为二维网格，将元件放到指定的行列上。Lynx 支持其能力的子集。

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

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

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

import "./index.scss";

const GridExample = () => {
  return (
    <scroll-view>
      <text className="title">
        grid-template-columns: 1fr 100px 2fr;
      </text>
      <text className="title">
        grid-template-rows: 1fr 1fr;
      </text>
      <view
        className="container"
        style={{ height: "80px", gridTemplateColumns: "1fr 100px 2fr", gridTemplateRows: "1fr 1fr" }}
      >
        <text className="item" style={{ gridRowStart: "span 2" }}>span 2</text>
        <text className="item">TWO</text>
        <text className="item">THREE</text>
        <text className="item" style={{ gridColumnStart: "span 2" }}>span 2</text>
      </view>

      <text className="title">
        grid-template-columns:
      </text>
      <text className="title">
        20% max-content minmax(50px, max-content);
      </text>
      <view
        className="container"
        style={{
          gridTemplateColumns: "20% max-content minmax(50px, max-content)",
          height: "max-content",
        }}
      >
        <text className="item">20%</text>
        <text className="item" style={{ fontSize: "17px" }}>NO WRAP!</text>
        <text className="item">min-width:50px, will fit the container</text>
      </view>

      <text className="title">
        grid-template-columns: 1fr 1fr;
      </text>
      <text className="title">grid-template-rows: 1fr max-content 1fr;</text>
      <view
        className="container"
        style={{
          gridTemplateColumns: "1fr 1fr",
          gridTemplateRows: "1fr max-content 1fr",
          height: "140px",
        }}
      >
        <text className="item" style={{ gridColumnStart: "span 2" }}>span 2</text>
        <text className="item">max-content: will take the maximum size of the items as the row size</text>
        <text className="item">THREE</text>
        <text className="item" style={{ gridColumnStart: "-2" }}>columnStart: -2</text>
      </view>
    </scroll-view>
  );
};

root.render(<GridExample />);

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

```



### 相对布局（relative）

如果你希望通过元件间的相对位置关系来描述布局，则可以通过将 [`display`] 属性设置为 `relative`，使用[相对布局]。相对布局（受 Android [Relative Layout](https://developer.android.com/develop/ui/views/layout/relative) 的启发）可以通过描述元件之间的位置关系（例如，一个元件位于另一个元件的左侧）来声明布局。

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

**Entry:** `src/relative_layout`
**Bundle:** `dist/relative.lynx.bundle`

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

import "./index.scss";

const RelativeExample = () => {
  return (
    <view className="container">
      <view className="close"></view>
      <view className="user_name"></view>
      <view className="user_description"></view>
      <view className="avatar"></view>
      <view className="user_lv"></view>
      <view className="follow"></view>
    </view>
  );
};

root.render(<RelativeExample />);

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

```



### 文字与行内布局

[`<view>`] 组件内不能直接插入文字，且 Lynx 的 [`display`] 属性不支持 `inline` 与 `block` 的取值。而 [`<text>`] 元件可以完成文字展示，以及元件的行内布局。详细参考[文字排版](/zh/guide/styling/text-and-typography.md)。

## 对齐元件

你可以使用 [`align-items`]、[`align-self`]、[`justify-content`]、[`align-content`]、[`justify-items`] 和 [`justify-self`] 属性，对齐 [`<view>`] 中布局的元件.

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

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

```tsx {10,13,18,29}
import { root } from "@lynx-js/react";

import "./index.scss";

const AlignExample = () => {
  return (
    <view className="container">
      <view
        className="linear box"
        style={{ alignItems: "center", justifyContent: "space-between" }}
      >
        <text>linear item 1</text>
        <text style={{ alignSelf: "end" }}>linear item 2</text>
        <text>linear item 3</text>
      </view>
      <view
        className="flex  box"
        style={{ justifyContent: "center", alignContent: "center" }}
      >
        <text>flex item 1</text>
        <text>flex item 2</text>
        <text>flex item 3</text>
        <text>flex item 4</text>
      </view>
      <view className="grid box">
        <text>grid item 1</text>
        <text
          className="grid-row-span"
          style={{ justifySelf: "center", alignSelf: "center" }}
        >
          grid item 2
        </text>
        <text>grid item 3</text>
      </view>
    </view>
  );
};

root.render(<AlignExample />);

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

```



## 位移和绝对定位

通过设置 [`top`]、[`left`]、[`bottom`]、[`right`] 属性，你可以将元件在原有位置的基础上进行位移。同时可以配合对 [`position`] 属性配置，完成对元件的绝对定位.

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

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

```tsx {12,26-28,43}
import { root } from "@lynx-js/react";

import "./index.scss";

const PositionExample = () => {
  return (
    <view className="column">
      <view className="row">
        <view className="box">
          <text>1</text>
        </view>
        <view className="box" style={{ bottom: "20px", right: "10px" }}>
          <text>2</text>
        </view>
        <view className="box">
          <text>3</text>
        </view>
      </view>
      <view className="row">
        <view className="box">
          <text>1</text>
        </view>
        <view
          className="box"
          style={{
            position: "absolute",
            top: "20px",
            left: "10px",
          }}
        >
          <text>ABS</text>
        </view>
        <view className="box">
          <text>3</text>
        </view>
      </view>
      <view className="row">
        <view className="box">
          <text>1</text>
        </view>
        <view
          className="box"
          style={{ position: "fixed", top: "50px", left: "50px" }}
        >
          <text>FIXED</text>
        </view>
        <view className="box">
          <text>3</text>
        </view>
      </view>
    </view>
  );
};

root.render(<PositionExample />);

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

```



## 国际化

你可以通过 [`direction`](/zh/api/css/properties/direction.md) 属性和[逻辑属性]，来使你的页面同时支持从左至右书写的语言（如中文）和从右至左书写的语言（如阿拉伯文）。逻辑属性指 `XX-inline-start`、`XX-inline-end` 等一系列属性（如 [`inset-inline-start`](/zh/api/css/properties/inset-inline-start.md)）。

启用 CSS 属性继承和逻辑属性请参考 [CSS 继承](/zh/guide/styling/custom-theming.md#按需使用-css-继承), 使用
[`enableCSSInheritance`] 配置，允许 `direction` 属性被继承，以及使用依赖于
`direction` 的逻辑属性。

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

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

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

import "./index.scss";

const DirectionExample = () => {
  return (
    <view className="column">
      <view className="row logic-padding" style={{ direction: "ltr" }}>
        <view className="box">
          <text>1</text>
        </view>
        <view className="box">
          <text>2</text>
        </view>
        <view className="box">
          <text>3</text>
        </view>
      </view>
      <view className="row logic-padding" style={{ direction: "rtl" }}>
        <view className="box">
          <text>1</text>
        </view>
        <view className="box">
          <text>2</text>
        </view>
        <view className="box">
          <text>3</text>
        </view>
      </view>
    </view>
  );
};

root.render(<DirectionExample />);

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

```



[`width`]: /zh/api/css/properties/width.md

[`height`]: /zh/api/css/properties/height.md

[`min-width`]: /zh/api/css/properties/min-width.md

[`min-height`]: /zh/api/css/properties/min-height.md

[`max-width`]: /zh/api/css/properties/max-width.md

[`max-height`]: /zh/api/css/properties/max-height.md

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

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

[`direction`]: /zh/api/css/properties/direction.md

[`display`]: /zh/api/css/properties/display.md

[`margin`]: /zh/api/css/properties/margin.md

[`padding`]: /zh/api/css/properties/padding.md

[`position`]: /zh/api/css/properties/position.md

[`border-width`]: /zh/api/css/properties/border-width.md

[`inset-inline-start`]: /zh/api/css/properties/inset-inline-start.md

[`top`]: /zh/api/css/properties/top.md

[`bottom`]: /zh/api/css/properties/bottom.md

[`max-content`]: /zh/api/css/data-type/max-content.md

[`fit-content`]: /zh/api/css/data-type/fit-content.md

[`justify-self`]: /zh/api/css/properties/justify-self.md

[`justify-items`]: /zh/api/css/properties/justify-items.md

[`align-self`]: /zh/api/css/properties/align-self.md

[`align-content`]: /zh/api/css/properties/align-content.md

[`justify-content`]: /zh/api/css/properties/justify-content.md

[`align-items`]: /zh/api/css/properties/align-items.md

[`left`]: /zh/api/css/properties/left.md

[`right`]: /zh/api/css/properties/right.md

[`<view>`]: /zh/api/elements/built-in/view.md

[`<text>`]: /zh/api/elements/built-in/text.md

[length units]: /zh/api/css/data-type/length-percentage.md

[relative layout]: /zh/guide/ui/layout/relative-layout.md

[线性布局]: /zh/guide/ui/layout/linear-layout.md

[相对布局]: /zh/guide/ui/layout/relative-layout.md

[弹性布局]: /zh/guide/ui/layout/flexible-box-layout.md

[网格布局]: /zh/guide/ui/layout/grid-layout.md

[逻辑属性]: https://developer.mozilla.org/zh/docs/Web/CSS/CSS_logical_properties_and_values

[`enableCSSInheritance`]: /zh/api/rspeedy/react-rsbuild-plugin.pluginreactlynxoptions.enablecssinheritance.md#pluginreactlynxoptionsenablecssinheritance-property
