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

# z-index

APISummary
## 介绍

z-index 属性设定了一个元件及其后代元件或 flex 项目的 z-order。当元件之间重叠的时候，z-index 较大的元件会覆盖较小的元件在上层进行显示。

:::info 常见问题

如果想生成 [stacking context](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context) 可以给元件增加 z-index: 0，例如给 scroll-view 添加 z-index: 0 可以避免子元件不跟随滑动

不建议在 list 直接子元件使用 z-index，可能影响 list 复用，如果在 list item 里面使用可以将 item 作为 stacking context

:::

## 使用示例

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

**Entry:** `src/z-index`
**Bundle:** `dist/z-index.lynx.bundle`

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

const ZIndex = () => {
  const [indices, setIndices] = useState([2, 1, 0, -1, -2]);
  const [show, setShow] = useState(false);

  const containerStyle = {
    zIndex: 0,
    height: "280px",
  };

  const innerContainerStyle = {
    height: "100%",
  };

  const getBlockStyle = (index: number, color: string, top: string, left: string) => ({
    zIndex: indices[index],
    background: color,
    position: "absolute" as const,
    top,
    left,
  });

  const getTextStyle = (show: boolean) => ({
    top: show ? "0" : "80px",
  });

  const handleChange = () => {
    setIndices(show ? [2, 1, 0, -1, -2] : [-2, -1, 0, 1, 2]);
    setShow(!show);
  };

  return (
    <view className="main" bindtap={handleChange}>
      <view className="container" style={containerStyle}>
        <view className="container" style={innerContainerStyle}>
          <view className="block" style={getBlockStyle(0, "#2196F3", "10px", "10px")}>
            <text className="text" style={getTextStyle(show)}>z-index:{indices[0]}</text>
          </view>
          <view className="block" style={getBlockStyle(1, "#4CAF50", "50px", "50px")}>
            <text className="text" style={getTextStyle(show)}>z-index:{indices[1]}</text>
          </view>
          <view className="block" style={getBlockStyle(2, "#9C27B0", "90px", "90px")}>
            <text className="text" style={getTextStyle(show)}>z-index:{indices[2]}</text>
          </view>
          <view className="block" style={getBlockStyle(3, "#FF9800", "130px", "130px")}>
            <text className="text" style={getTextStyle(show)}>z-index:{indices[3]}</text>
          </view>
          <view className="block" style={getBlockStyle(4, "#E91E63", "170px", "170px")}>
            <text className="text" style={getTextStyle(show)}>z-index:{indices[4]}</text>
          </view>
        </view>
      </view>
    </view>
  );
};

root.render(<ZIndex />);

```

v0.11.0



## 语法

```css
z-index: 0;
z-index: 3;
z-index: 289;
z-index: -1;
```

### 取值

- [`<number>`](/next/zh/api/css/data-type/number.md)
  表示此元件的堆叠顺序。

- 默认值
  **auto**

## 形式定义

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


## 形式语法

```
z-index = <integer>
```

## 与 Web 的区别

Web 默认的是 auto，Lynx 是通过是否设置 z-index 来判断是否 auto

## 兼容性

**Compatibility Table**

**Query:** `css.properties.z-index`

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

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| z-index | Yes (1.0) | Yes (3.4) | Yes (1.0) | Yes (1.0) | Yes (1.0) | Yes |

**`z-index`**

**Specifications:**

