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

# margin

APISummary
## 介绍

`margin` 属性为给定元件设置所有四个（上右下左）方向的外边距属性。也就是 [`margin-top`](/next/zh/api/css/properties/margin-top.md)、[`margin-right`](/next/zh/api/css/properties/margin-right.md)、[`margin-bottom`](/next/zh/api/css/properties/margin-bottom.md)、[`margin-left`](/next/zh/api/css/properties/margin-left.md) 四个外边距属性设置的简写。该属性可以为负值。

## 使用示例

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

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

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

const Margin = () => {
  const scrollViewStyle = {
    height: "100%",
  };

  return (
    <scroll-view className="root" scroll-orientation="vertical" style={scrollViewStyle}>
      <view className="con">
        <text className="intro" style={{ margin: "20px" }}>
          margin: 20px; All sides
        </text>
      </view>

      <view className="con">
        <text className="intro" style={{ margin: "20px 5px" }}>
          margin: 20px 5px; Top/Bottom | Left/Right
        </text>
      </view>

      <view className="con">
        <text className="intro" style={{ margin: "20px 15px 10px" }}>
          margin: 20px 15px 10px; Top | Left/Right | Bottom
        </text>
      </view>

      <view className="con">
        <text className="intro" style={{ margin: "20px 15px 10px 5px" }}>
          margin: 20px 15px 10px 5px; Top | Right | Bottom | Left
        </text>
      </view>
    </scroll-view>
  );
};

root.render(<Margin />);

```

v0.11.0



## 语法

```css
/* Apply to all four sides */
margin: 1em;
margin: -3px;

/* top and bottom | left and right */
margin: 5% auto;

/* top | left and right | bottom */
margin: 1em auto 2em;

/* top | right | bottom | left */
margin: 2px 1em 0 auto;
```

`margin` 属性接受 1\~4 个值。每个值可以是 [`length`](/next/zh/api/css/data-type/length.md)，[`percentage`](/next/zh/api/css/data-type/percentage.md)，或 `auto`。取值为负时元件会比原来更接近临近元件。

当只指定一个值时，该值会统一应用到全部四个边的外边距上。
指定两个值时，第一个值会应用于上边和下边的外边距，第二个值应用于左边和右边。
指定三个值时，第一个值应用于上边，第二个值应用于右边和左边，第三个则应用于下边的外边距。
指定四个值时，依次（顺时针方向）作为上边，右边，下边，和左边的外边距。

### 取值

#### `0px`

**默认值**。

#### `<length>`

[`<length>`](/next/zh/api/css/data-type/length.md) 定义了一个确定值。可以为负值。

#### `<percentage>`

相对于[包含块](https://developer.mozilla.org/zh-CN/docs/Web/CSS/Containing_block)的宽度，以百分比值为外边距。

#### `auto`

让布局流程自己选择一个合适的外边距。

:::info
目前支持在[弹性布局](https://developer.mozilla.org/zh/docs/Web/CSS/CSS_flexible_box_layout)、[网格布局](https://developer.mozilla.org/zh/docs/Web/CSS/CSS_grid_layout)、[线性布局](/next/zh/guide/ui/layout/linear-layout.md)的非 [`position: fixed/absolute/sticky`](/next/zh/api/css/properties/position.md) 子元件中使用。

其中，对于线性布局，仅支持在交叉轴方向上的外边距 `auto` 值。
:::

## 形式定义

| 初始值 | as each of the properties of the shorthand: 
- margin-bottom: 0
- margin-left: 0
- margin-right: 0
- margin-top: 0 |
| 适用元素 | 所有元件 |
| 是否支持继承 | 否 |
| 是否支持动画 |  |
| 百分比 | refer to the width of the containing block |


## 形式语法

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

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

## 与 Web 的区别

[mdn: margin](https://developer.mozilla.org/zh-CN/docs/Web/CSS/margin)

1. 在 Lynx 下，`margin` 任何情况都不会重叠。
2. 目前支持在[弹性布局](https://developer.mozilla.org/zh/docs/Web/CSS/CSS_flexible_box_layout)、[网格布局](https://developer.mozilla.org/zh/docs/Web/CSS/CSS_grid_layout)、[线性布局](/next/zh/guide/ui/layout/linear-layout.md)的非 [`position: fixed/absolute/sticky`](/next/zh/api/css/properties/position.md) 子元件中使用 `auto`。其中，对于线性布局，仅支持在交叉轴方向上的外边距 `auto` 值。

## 兼容性

**Compatibility Table**

**Query:** `css.properties.margin`

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

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| margin | Yes (1.0) | Yes (3.4) | Yes (1.0) | Yes (1.0) | Yes (1.0) | Yes |
| supported&#95;in&#95;flex&#95;layout | Yes (1.0) | Yes (3.4) | Yes (1.0) | Yes (1.0) | Yes (1.0) | Yes |
| supported&#95;in&#95;flex&#95;layout.auto | Yes (1.0) | Yes (3.4) | Yes (1.0) | Yes (1.0) | Yes (1.0) | Yes |
| supported&#95;in&#95;linear&#95;layout | Yes (1.0) | Yes (3.4) | Yes (1.0) | Yes (1.0) | Yes (1.0) | Yes |
| supported&#95;in&#95;linear&#95;layout.nested&#95;value&#95;1 | Yes (1.0) | Yes (3.4) | Yes (1.0) | Yes (1.0) | Yes (1.0) | Yes |
| supported&#95;in&#95;linear&#95;layout.nested&#95;value&#95;2 | No | Yes (3.4) | No | No | No | Yes |
| supported&#95;in&#95;grid&#95;layout | Yes (2.1) | Yes (3.4) | Yes (2.1) | Yes (2.1) | Yes (2.1) | Yes |
| supported&#95;in&#95;grid&#95;layout.auto | Yes (2.1) | Yes (3.4) | Yes (2.1) | Yes (2.1) | Yes (2.1) | Yes |

**`margin`**

**Specifications:**

**`supported_in_flex_layout`**

Supported in Flexible Box Layout

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

**`supported_in_flex_layout.auto`**

<code>auto</code>

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

**`supported_in_linear_layout`**

Supported in Linear Layout

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

**`supported_in_linear_layout.nested_value_1`**

<code>auto</code> in cross-axis

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

**`supported_in_linear_layout.nested_value_2`**

<code>auto</code> in main-axis

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

**`supported_in_grid_layout`**

Supported in Grid Layout

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

**`supported_in_grid_layout.auto`**

<code>auto</code>

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

