> For AI agents: the complete documentation index is available at /next/llms.txt, the full documentation bundle is available at /next/llms-full.txt.

# margin

APISummary
## Introduction

The margin CSS shorthand property sets the margin area on all four sides of an element.

This property is a shorthand for the following CSS properties: [margin-top](/next/api/css/properties/margin-top.md), [margin-right](/next/api/css/properties/margin-right.md), [margin-bottom](/next/api/css/properties/margin-bottom.md), [margin-left](/next/api/css/properties/margin-left.md).

## Examples

**This is an example below: 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



## Syntax

```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;
```

The margin property may be specified using one, two, three, or four values. Each value is a [`<length>`](/next/api/css/data-type/length.md), a [`<percentage>`](/next/api/css/data-type/percentage.md), or the keyword `auto`. Negative values draw the element closer to its neighbors than it would be by default.

- When one value is specified, it applies the same margin to all four sides.

- When two values are specified, the first margin applies to the top and bottom, the second to the left and right.

- When three values are specified, the first margin applies to the top, the second to the right and left, the third to the bottom.

- When four values are specified, the margins apply to the top, right, bottom, and left in that order (clockwise).

### Values

#### `0px`

**Default value**.

#### `<length>`

[`<length>`](/next/api/css/data-type/length.md) means that the size of the margin as a fixed value.

#### `<percentage>`

The size of the margin as a percentage, relative to the inline size (width) of the [containing block](https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block).

#### `auto`

Selects a suitable margin to use. For example, in certain cases this value can be used to center an element.

:::info
Currently, Lynx supports using `auto` in flex, grid and [linear](/next/guide/ui/layout/linear-layout.md) children that are not with [`position:fixed/absolute/sticky`](/next/api/css/properties/position.md).

Additionally, in linear layout, it only supports using `auto` on the cross axis. We plan to support using `auto` on the main axis in the future.
:::

## Formal definition

| Initial value | as each of the properties of the shorthand: 
-  margin-bottom: 0
- margin-left: 0
- margin-right: 0
- margin-top: 0 |
| Applies to | all elements |
| Inherited | no |
| Animatable |  |
| Percentages | refer to the width of the containing block |


## Formal syntax

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

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

## Differences from the Web

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

1. In Lynx, margins will not collapse at any time.
2. Currently, Lynx supports using `auto` in flex, grid and [linear](/next/guide/ui/layout/linear-layout.md) children that are not with [`position:fixed/absolute/sticky`](/next/api/css/properties/position.md). Additionally, in linear layout, it only supports using `auto` on the cross axis. We plan to support using `auto` on the main axis in the future.

## Compatibility

**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/en-US/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)

