> 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

APISummary
## 介绍

`background` 是一种 CSS 简写属性，用于一次性集中定义各种背景属性，包括 color,image,origin 与 size,repeat 方式等等。
这是一个简写属性，可以在一次声明中定义一个或多个属性：[`background-clip`](/next/zh/api/css/properties/background-clip.md), [`background-color`](/next/zh/api/css/properties/background-color.md), [`background-image`](/next/zh/api/css/properties/background-image.md), [`background-origin`](/next/zh/api/css/properties/background-origin.md), [`background-position`](/next/zh/api/css/properties/background-position.md), [`background-repeat`](/next/zh/api/css/properties/background-repeat.md), [`background-size`](/next/zh/api/css/properties/background-size.md)

## 使用示例

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

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

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

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

  const container1Style = {
    backgroundOrigin: "content-box" as const,
    backgroundPosition: "left top" as const,
    backgroundRepeat: "no-repeat" as const,
  };

  const container2Style = {
    backgroundOrigin: "padding-box" as const,
    backgroundPosition: "50% 50%",
    backgroundRepeat: "repeat-x" as const,
  };

  const container3Style = {
    backgroundOrigin: "border-box" as const,
    backgroundPosition: "30px 40px",
    backgroundRepeat: "repeat" as const,
  };

  const container4Style = {
    backgroundOrigin: "content-box,border-box" as const,
    backgroundPosition: "50% 40px, right bottom",
    backgroundRepeat: "repeat-y,repeat-x" as const,
  };

  return (
    <scroll-view className="root" scroll-orientation="vertical">
      <view className="fixed_area" style={fixedAreaStyle}>
        <view className="container image1" style={container1Style}></view>
        <view className="container image1" style={container2Style}></view>
        <view className="container image1" style={container3Style}></view>
        <view className="container image2" style={container4Style}></view>
      </view>
    </scroll-view>
  );
};

root.render(<Background />);

```

v0.11.0



## 语法

```css
/* 使用 <background-color> */
background: green;

/* 使用 <bg-image> 和 <repeat-style> */
background: url('test.jpg') repeat-y;

/* 使用 <box> 和 <background-color> */
background: border-box red;

/* 将背景设置为一张居中放大的图片 */
background: no-repeat center / 80% url('../img/image.png');
```

### 取值

当 `background` 属性被指定多个背景层时，使用逗号分隔每个背景层。

每一次的语法如下：

- 在每一层中，下列的值可以出现 0 次或 1 次：
  - [`<bg-image>`](#bg-image)
  - [`<position>`](#position)
  - [`<bg-size>`](#bg-size)
  - [`<repeat-style>`](#repeat-style)
- `<bg-size>` 只能紧接着 `<position>` 出现，以“/”分割，如：`"center/80%"`.
- [`<box>`](#box) 可能出现 0 次、1 次或 2 次。如果出现 1 次，它同时设置 [`background-origin`](/next/zh/api/css/properties/background-origin.md) 和 [`background-clip`](/next/zh/api/css/properties/background-clip.md)。如果出现 2 次，第一次的出现设置 [`background-origin`](/next/zh/api/css/properties/background-origin.md)，第二次的出现设置 [`background-clip`](/next/zh/api/css/properties/background-clip.md)。

> **注意：[`background-color`](/next/zh/api/css/properties/background-color.md) 只能在 background 的最后一个属性上定义，因为整个元件只有一种背景色**

#### `<box>` \{#box}

参见 [`background-clip`](/next/zh/api/css/properties/background-clip.md) 和 [`background-origin`](/next/zh/api/css/properties/background-origin.md)

#### `<background-color>` \{#background-color"}

参见 [`background-color`](/next/zh/api/css/properties/background-color.md)

#### `<bg-image>` \{#bg-image}

参见 [`background-image`](/next/zh/api/css/properties/background-image.md)

#### `<position>` \{#position}

参见 [`background-position`](/next/zh/api/css/properties/background-position.md)

#### `<repeat-style>` \{#repeat-style}

参见 [`background-repeat`](/next/zh/api/css/properties/background-repeat.md)

#### `<bg-size>` \{#bg-size}

参见 [`background-size`](/next/zh/api/css/properties/background-size.md)

## 注意事项

- color 必须写在最后一个 image 中，否则 parser 会认为这是一个无效 background
  例如: `background: red url('./a.png'), url('./b.png);'` 会被当作错误的写法直接忽略。
  正确的写法应该是：`background: url('./a.png'),red url('./b.png);`

## 形式定义

| 初始值 | background-image: none
background-position: 0% 0%
background-size: auto auto
background-repeat: repeat
background-origin: padding-box
background-clip: border-box
background-color: transparent |
| 适用元素 | all elements |
| 是否支持继承 | no |
| 是否支持动画 | no |


## 形式语法

```
[ <bg-layer> , ]* <final-bg-layer>

where

<bg-layer> = <bg-image> || <bg-position> [ / <bg-size> ]? || <repeat-style> || <box> || <box>
<final-bg-layer> = <'background-color'> || <bg-image> || <bg-position> [ / <bg-size> ]? || <repeat-style> || <attachment> || <box> || <box>

where

<bg-image> = none | <image>
<bg-position> = [ [ left | center | right | top | bottom | <length-percentage> ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ] | [ center | [ left | right ] <length-percentage>? ] && [ center | [ top | bottom ] <length-percentage>? ] ]
<bg-size> = [ <length-percentage> | auto ]{1,2} | cover | contain
<repeat-style> = repeat-x | repeat-y | [ repeat | space | round | no-repeat ]{1, 2}
<box> = border-box | padding-box | content-box

where

<image> = <url> | <gradient>
<length-percentage> = <length> | <percentage>

where

<gradient> = <linear-gradient> | <radial-gradient>

```

## 与 Web 的区别

- `<bg-layer>`
  中不支持 [`<attachment>`](https://developer.mozilla.org/zh-CN/docs/Web/CSS/background#attachment) 属性。

- `<image>`
  中只支持 `<url>` 和 `<gradient>`
  > _标准 web 中还支持 `<image-set>`、`<element()>`、`<paint()>`、`<cross-fade()>` 等自定义绘图属性。_

- `<gradient>`
  中目前只支持 `<linear-gradient>` 和 `<radial-gradient>`
  > _标准 web 中还支持 `<repeating-linear-gradient()>`, `<radial-gradient()>`, `<repeating-radial-gradient()>`, `<conic-gradient()>`_

## 兼容性

**Compatibility Table**

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

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

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

**`background`**

**Specifications:**

