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

# Understanding Layout

Lynx provides:

- Properties such as [`width`], [`height`], [`margin`], and [`padding`] are used to describe the size of elements.
- The [`display`] property, along with [linear layout], [flexible box layout], [grid layout], and [relative layout], are used for laying out elements.
- The aligning properties, including [`align-items`], [`justify-content`] and etc., are used for aligning elements.
- The [`position`] property, along with [`left`], [`right`], [`top`], [`bottom`] properties, are used to position elements.
- [`direction`] and [logical properties] are used to support the internationalization of layouts.

For the layout properties supported by Lynx with the same name in the Web, the behavior of these properties will be consistent with those in the Web.
However, there is a difference in the design concept between Lynx layout and Web layout: Web layout is primarily text-based. While Lynx layout is based on elements (`<view>`, `<text>`, `<image>`, etc.). In other words, elements in Lynx are all [block-level elements](https://developer.mozilla.org/en/docs/Glossary/Block-level_content).

The following tutorial will show you how to complete the layout of elements in Lynx.

## Sizing the Elements

You can use [`width`], [`height`], [`max-width`], [`min-width`], [`max-height`], [`min-height`], [`margin`], [`padding`], and [`border-width`] to describe the size and box model of an element. These properties support various [length units] such as `px`, `%`, `vh` and etc. Additionally, `width` and `height` support [`max-content`] and [`fit-content`] for sizing elements according to their content.

**This is an example below: 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();
}

```

v0.7.1




> **`box-sizing` is `border-box` by default and Lynx does not exhibit the behavior of margin collapsing.**
> 
> By default, the size properties of Lynx, such as `width`, `height`, and `max-width`, describe the size of the border box. This is inconsistent with the default behavior of the Web.
> 
> Lynx does not exhibit the behavior of [margin collapsing](https://developer.mozilla.org/en/docs/Web/CSS/CSS_box_model/Mastering_margin_collapsing) as in the Web.
> 


## Layout the Elements

The [`<view>`] element can be used for laying out child elements, and by setting the [`display`] property on the `<view>` element, you can control the way it lays out its child elements. The display property supports five values: `linear`, `flex`, `grid`, `relative` and `none`.

### Linear Layout

If you want to simply arrange the elements in order, you can set the [`display`] property to linear and use Lynx's default layout, which is the [linear layout]. The Linear layout (inspired by Android's [Linear Layout](https://developer.android.com/develop/ui/views/layout/linear)) can arrange the elements in order according to the direction you declare.

**This is an example below: 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();
}

```

v0.7.1



### Flexible Box Layout

If you need to make the size of child elements adapt to the space of the parent element (such as expanding child elements to fill the unused space or shrinking child elements to avoid overflow), you can set the [`display`] property to `flex` and use the [flexible box layout]. The flexible box layout in Lynx is consistent with the one in Web.

**This is an example below: 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();
}

```

v0.7.1



### Grid Layout

If you want to arrange multiple elements alternately in both vertical and horizontal directions to form a two-dimensional layout, you can set the [`display`] property to `grid` and use the [grid layout], a layout in the Web that can divide the space into a two-dimensional grid and place elements in the specified rows and columns. Lynx supports a subset of its features.

**This is an example below: 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();
}

```

v0.7.1



### Relative Layout

If you want to describe the layout through the relative position relationship between elements, you can set the [`display`] property to `relative` and use the [relative layout]. The relative layout(inspired by Android's [Relative Layout](https://developer.android.com/develop/ui/views/layout/relative)) can declare the layout by describing the position relationship between elements (for example, one element is located to the left of another element).

**This is an example below: 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();
}

```

v0.7.1



### Text and Inline Layout

In Lynx, text cannot be directly inserted into the [`<view>`] element, and the [`display`] property of Lynx does not support the values of `inline` and `block`. The [`<text>`] element can complete text display and the inline layout of elements. For details, please refer to [Text Typography](/next/guide/styling/text-and-typography.md).

## Aligning the Elements

You can align the elements laid out by a [`<view>`] with [`align-items`], [`align-self`], [`justify-content`], [`align-content`], [`justify-items`] and [`justify-self`] properties.

**This is an example below: 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();
}

```

v0.7.1



## Offsetting and Absolute Positioning

You can offset the elements with [`top`], [`left`], [`bottom`], and [`right`] properties. And you can make elements absolutely positioned with [`position`] property.

**This is an example below: 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();
}

```

v0.7.1



## Internationalization

You can use the [`direction`] property and [logical properties] to make your page support both languages written from left to right (such as Chinese) and languages written from right to left (such as Arabic).
[logical properties] refer to a series of properties like `XX-inline-start` and `XX-inline-end` (such as [`inset-inline-start`]).

> **Enable CSS Inheritance and Logical Properties**
> 
> Please refer to [CSS
> Inheritance](/next/guide/styling/custom-theming.md#leveraging-css-inheritance-as-needed),
> using [`enableCSSInheritance`] to enable `direction` to be inheritable, and
> using logical properties depends on the `direction` value.
> 



**This is an example below: 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();
}

```

v0.7.1



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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

[linear layout]: /next/guide/ui/layout/linear-layout.md

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

[flexible box layout]: /next/guide/ui/layout/flexible-box-layout.md

[grid layout]: /next/guide/ui/layout/grid-layout.md

[logical properties]: https://developer.mozilla.org/en/docs/Web/CSS/CSS_logical_properties_and_values

[`enableCSSInheritance`]: /next/api/packages/react-rsbuild-plugin.md#enablecssinheritance
