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

# background-position

APISummary
## 介绍

`background-position` 为每一个背景图片设置初始位置。这个位置是相对于由 [`background-origin`](/next/zh/api/css/properties/background-origin.md) 定义的位置图层。

## 使用示例

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

**Entry:** `src/background-position`
**Bundle:** `dist/background-position.lynx.bundle`

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

const BackgroundPosition = () => {
  const fixedAreaStyle = {
    width: "100%",
    height: "100%",
    flexDirection: "column" as const,
  };

  const containerStyles = {
    style1: {
      backgroundOrigin: "content-box" as const,
      backgroundPosition: "left top" as const,
    },
    style2: {
      backgroundOrigin: "padding-box" as const,
      backgroundPosition: "50% 50%",
    },
    style3: {
      backgroundOrigin: "border-box" as const,
      backgroundPosition: "30px 40px",
    },
    style4: {
      backgroundOrigin: "content-box,border-box" as const,
      backgroundPosition: "50% 40px, right bottom",
    },
    style5: {
      backgroundOrigin: "padding-box" as const,
      backgroundSize: "auto 100%",
      backgroundRepeat: "no-repeat" as const,
      backgroundPosition: "-13px -20px",
    },
  };

  return (
    <scroll-view className="root" scroll-orientation="vertical">
      <view className="fixed_area" style={fixedAreaStyle}>
        <view className="container image1" style={containerStyles.style1}></view>
        <view className="container image1" style={containerStyles.style2}></view>
        <view className="container image1" style={containerStyles.style3}></view>
        <view className="container image2" style={containerStyles.style4}></view>
        <view className="container image1" style={containerStyles.style5}></view>
      </view>
    </scroll-view>
  );
};

root.render(<BackgroundPosition />);

```

v0.11.0



## 语法

```css
/* Keyword values */
background-position: top;
background-position: bottom;
background-position: left;
background-position: right;
background-position: center;

/* <percentage> values */
background-position: 25% 75%;

/* <length> values */
background-position: 0px 0px;
background-position: 1rem 2rem;
background-position: 10em 8em;

/* Multiple images */
background-position:
  0px 0px,
  center;
```

### 取值

- 关键字 `center`，用来居中背景图片。

- 关键字 `top`，`left`，`bottom`，`right`。用来指定把这个背景图放在哪一个边缘。
  - 如果只指定一个值，另一个唯独默认被设置成 50%(与 `center` 等效)。
  - 如果一个值是 `top` 或 `bottom`，另一个值不能再次指定 `top` 或 `bottom` 否则解析时会报错。
  - 如果一个值是 `left` 或 `right`，同理另一个值不能再次指定 `left` 或 `right`。

- [`<length>`](/next/zh/api/css/data-type/length.md) 或 [`<percentage>`](/next/zh/api/css/data-type/percentage.md)。指定相对于左边缘 x 轴或上边缘 y 轴的的坐标。
  - 如果只指定一个值，这个值指定的是相对于左边缘 x 轴的坐标。相对于上边缘 y 轴的坐标会默认被设置成 50%。

## 形式定义

| 初始值 | 0% 0% |
| 适用元素 | all elements |
| 是否支持继承 | no |
| 是否支持动画 | no |


## 形式语法

```
[ left | center | right | top | bottom | <length-percentage> ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ]

<length-percentage> = <length> | <percentage>
```

## 与 Web 的区别

- 不支持 **Global Value** 类型的关键字 `inherit`、`initial` 和 `unset`。
- 不支持 **Edge offsets** 语法，例如:

```
background-position: bottom 10px right 20px
```

## 兼容性

**Compatibility Table**

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

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

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| background-position | Yes (1.0) | Yes (3.4) | Yes (1.0) | Yes (1.0) | Yes (1.0) | Yes |

**`background-position`**

**Specifications:**

