> 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.

# position

APISummary
## 介绍

`position` 属性用于指定一个元件在文档中的定位方式。
与 [`top`](/next/zh/api/css/properties/top.md)、[`right`](/next/zh/api/css/properties/right.md)、[`bottom`](/next/zh/api/css/properties/bottom.md)、[`left`](/next/zh/api/css/properties/left.md) 属性一起决定元件的最终位置。

## 定位类型

- 定位元件（positioned element）是其计算后位置属性为 `relative`，`absolute`，`fixed` 或 `sticky` 的一个元件。

- 相对定位元件（relatively positioned element）是计算后位置属性为 `relative` 的元件。

- 绝对定位元件（absolutely positioned element）是计算后位置属性为 `absolute` 或 `fixed` 的元件。

- 粘性定位元件（stickily positioned element）是计算后位置属性为 `sticky` 的元件。

大多数情况下，[`height`](/next/zh/api/css/properties/height.md) 和 [`width`](/next/zh/api/css/properties/width.md) 被设定为 `auto` 的绝对定位元件，按其内容大小调整尺寸。但是，被绝对定位的元件可以通过指定 [`top`](/next/zh/api/css/properties/top.md) 和 [`bottom`](/next/zh/api/css/properties/bottom.md)，保留 [`height`](/next/zh/api/css/properties/height.md) 未指定（即 `auto`），来填充可用的垂直空间。它们同样可以通过指定 [`left`](/next/zh/api/css/properties/left.md) 和 [`right`](/next/zh/api/css/properties/right.md) 并将 [`width`](/next/zh/api/css/properties/width.md) 指定为 `auto` 来填充可用的水平空间。

除了刚刚描述的情况（绝对定位元件填充可用空间）：

如果 [`top`](/next/zh/api/css/properties/top.md) 和 [`bottom`](/next/zh/api/css/properties/bottom.md) 都被指定（严格来说，这里指定的值不能为 `auto`），`top` 优先。
如果指定了 [`left`](/next/zh/api/css/properties/left.md) 和 [`right`](/next/zh/api/css/properties/right.md)，当 [`direction`](/next/zh/api/css/properties/direction.md) 设置为 `ltr`（水平书写的中文、英语）时 `left` 优先，当 `direction` 设置为 `rtl`（阿拉伯语、希伯来语、波斯语由右向左书写）时 `right` 优先。

## 使用示例

**下面是一个示例: css-api**

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

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

const Position = () => {
  const boxStyles = {
    fixed1: {
      position: "fixed" as const,
      top: "5px",
      left: "5px",
      width: "50px",
      height: "50px",
      backgroundColor: "red",
    },
    absolute1: {
      position: "absolute" as const,
      top: "0px",
      left: "50px",
      width: "50px",
      height: "50px",
      backgroundColor: "brown",
    },
    fixed2: {
      position: "fixed" as const,
      top: "5px",
      left: "105px",
      width: "50px",
      height: "50px",
      backgroundColor: "pink",
    },
    absolute2: {
      position: "absolute" as const,
      right: "0px",
      width: "50px",
      height: "50px",
      backgroundColor: "coral",
    },
    fixed3: {
      position: "fixed" as const,
      left: "5px",
      top: "55px",
      width: "50px",
      height: "50px",
      backgroundColor: "Chocolate",
    },
    absolute3: {
      position: "absolute" as const,
      left: "100px",
      top: "50px",
      width: "50px",
      height: "50px",
      backgroundColor: "DarkOrange",
    },
    absolute4: {
      position: "absolute" as const,
      top: "50px",
      left: "50px",
      width: "50px",
      height: "50px",
      backgroundColor: "yellow",
    },
    absolute5: {
      position: "absolute" as const,
      left: "150px",
      top: "50px",
      width: "50px",
      height: "50px",
      backgroundColor: "GoldenRod",
    },
    absolute6: {
      position: "absolute" as const,
      left: "0px",
      top: "100px",
      width: "50px",
      height: "50px",
      backgroundColor: "GreenYellow",
    },
    absolute7: {
      position: "absolute" as const,
      left: "50px",
      top: "100px",
      width: "50px",
      height: "50px",
      backgroundColor: "LightBlue",
    },
    absolute8: {
      position: "absolute" as const,
      left: "150px",
      top: "100px",
      width: "50px",
      height: "50px",
      backgroundColor: "DodgerBlue",
    },
    absolute9: {
      position: "absolute" as const,
      left: "0px",
      top: "150px",
      width: "50px",
      height: "50px",
      backgroundColor: "DarkTurquoise",
    },
    fixed4: {
      position: "fixed" as const,
      left: "55px",
      top: "155px",
      width: "50px",
      height: "50px",
      backgroundColor: "DarkSlateBlue",
    },
    fixed5: {
      position: "fixed" as const,
      left: "105px",
      top: "155px",
      width: "50px",
      height: "50px",
      backgroundColor: "DarkSlateGrey",
    },
    absolute10: {
      position: "absolute" as const,
      bottom: "50px",
      right: "50px",
      width: "50px",
      height: "50px",
      backgroundColor: "green",
    },
    fixed6: {
      position: "fixed" as const,
      top: "155px",
      left: "155px",
      width: "50px",
      height: "50px",
      backgroundColor: "blue",
    },
    leadIn: {
      height: "72px",
      justifyContent: "center" as const,
      alignItems: "center" as const,
      backgroundColor: "#f4f4f5",
    },
    stickyHeader: {
      position: "sticky" as const,
      top: "0px",
      zIndex: 1,
      width: "750rpx",
      height: "44px",
      justifyContent: "center" as const,
      alignItems: "center" as const,
      backgroundColor: "#111827",
    },
    stickyText: {
      color: "#ffffff",
      fontSize: "14px",
    },
    tail: {
      height: "260px",
      justifyContent: "center" as const,
      alignItems: "center" as const,
      backgroundColor: "#f4f4f5",
    },
  };

  return (
    <scroll-view className="root" scroll-orientation="vertical">
      <view style={boxStyles.leadIn}>
        <text>Scroll to see position: sticky</text>
      </view>
      <view style={boxStyles.stickyHeader}>
        <text style={boxStyles.stickyText}>position: sticky; top: 0</text>
      </view>
      <view className="fixed_area">
        <view className="container">
          <view style={boxStyles.fixed1}></view>
          <view style={boxStyles.absolute1}></view>
          <view style={boxStyles.fixed2}></view>
          <view style={boxStyles.absolute2}></view>
          <view style={boxStyles.fixed3}></view>
          <view style={boxStyles.absolute3}></view>
          <view style={boxStyles.absolute4}></view>
          <view style={boxStyles.absolute5}></view>
          <view style={boxStyles.absolute6}></view>
          <view style={boxStyles.absolute7}></view>
          <view style={boxStyles.absolute8}></view>
          <view style={boxStyles.absolute9}></view>
          <view style={boxStyles.fixed4}></view>
          <view style={boxStyles.fixed5}></view>
          <view style={boxStyles.absolute10}></view>
          <view style={boxStyles.fixed6}></view>
        </view>
      </view>
      <view style={boxStyles.tail}>
        <text>The sticky bar stays at the top while scrolling.</text>
      </view>
    </scroll-view>
  );
};

root.render(<Position />);

```

v0.11.0



## 语法

```css
position: relative;
position: fixed;
position: absolute;
position: sticky;
```

### 取值

#### `relative`

**默认值**。当前元件将参与父节点布局，[`top`](/next/zh/api/css/properties/top.md) 等属性将在布局所得基础上添加相对偏移。

#### `absolute`

当前元件将**不**参与父节点布局，当前元件会在父元件的 padding bound 中进行绝对布局，即由 top 等属性决定其位置。绝对定位的元件可以设置外边距（[`margin`](/next/zh/api/css/properties/margin.md)），且不会与其他边距合并。

在[弹性布局](/next/zh/guide/ui/layout/flexible-box-layout.md)中，可以使用 [`align-self: center`](/next/zh/api/css/properties/align-self.md) 实现在交叉轴居中的效果。

#### `fixed`

当前元件将**不**参与父节点布局，当前元件将被视为根节点的子元件进行绝对布局，即由 [`top`](/next/zh/api/css/properties/top.md) 等属性决定其位置。

#### `sticky`

当前元件先按正常流布局，再相对最近的滚动容器和包含块产生偏移，偏移量由 [`top`](/next/zh/api/css/properties/top.md)、[`right`](/next/zh/api/css/properties/right.md)、[`bottom`](/next/zh/api/css/properties/bottom.md)、[`left`](/next/zh/api/css/properties/left.md) 决定。该偏移不会影响其他元件的位置。

3.9 之前，`sticky` 仅对 `scroll-view` 的直接子节点生效。

## 形式定义

| 初始值 | `relative` |
| 适用元素 | 所有元件 |
| 是否支持继承 | 否 |
| 是否支持动画 | 否 |


## 形式语法

```
position: relative | absolute | fixed | sticky
```

## 与 Web 的区别

[mdn: position](https://developer.mozilla.org/en-US/docs/Web/CSS/position)

1. Lynx 的默认值为 `relative`，Web 的默认值为 `static`。

## 兼容性

**Compatibility Table**

**Query:** `css.properties.position`

[Source](https://github.com/lynx-family/lynx/blob/develop/tools/css_generator/css_defines/5-position.json) · [MDN](https://developer.mozilla.org/zh-CN/docs/Web/CSS/position)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| position | Yes (1.0) | Yes (3.4) | Yes (1.0) | Yes (1.0) | Yes (1.0) | Yes |
| relative | Yes (1.0) | Yes (3.4) | Yes (1.0) | Yes (1.0) | Yes (1.0) | Yes |
| absolute | Yes (1.0) | Yes (3.4) | Yes (1.0) | Yes (1.0) | Yes (1.0) | Yes |
| fixed | Yes (1.0) | Yes (3.4) | Yes (1.0) | Yes (1.0) | Yes (1.0) | Yes |
| sticky | Yes (1.4) | Yes (3.4) | Yes (1.4) | Yes (1.4) | Yes (1.4) | Yes |
| static | No | No | No | No | No | Yes |

**`position`**

**Specifications:**

**`relative`**

[Source](https://github.com/lynx-family/lynx/blob/develop/tools/css_generator/css_defines/5-position.json)

**`absolute`**

[Source](https://github.com/lynx-family/lynx/blob/develop/tools/css_generator/css_defines/5-position.json)

**`fixed`**

[Source](https://github.com/lynx-family/lynx/blob/develop/tools/css_generator/css_defines/5-position.json)

**`sticky`**

[Source](https://github.com/lynx-family/lynx/blob/develop/tools/css_generator/css_defines/5-position.json)

**`static`**

[Source](https://github.com/lynx-family/lynx/blob/develop/tools/css_generator/css_defines/5-position.json)


## 常见问题

1. 假如在 `position: absolute` 的元件使用了 [`z-index`](/next/zh/api/css/properties/z-index.md)，请同时在父元件上设置 `z-index: 0`；
