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

# max-width

APISummary
## 介绍

设置元件的最大宽度。它可以阻止 [`width`](/next/zh/api/css/properties/width.md) 属性的应用值大于 `max-width` 的值。
`max-width` 的值会覆盖 [`width`](/next/zh/api/css/properties/width.md) 和 [`min-width`](/next/zh/api/css/properties/min-width.md)。

## 使用示例

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

**Entry:** `src/max-width`
**Bundle:** `dist/max-width.lynx.bundle`

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

const MaxWidth = () => {
  const containerStyle = {
    display: "linear" as const,
  };

  const firstBoxStyle = {
    width: "300px",
    backgroundColor: "green",
  };

  const secondBoxStyle = {
    width: "300px",
    backgroundColor: "yellow",
    maxWidth: "300px",
  };

  const thirdBoxStyle = {
    width: "300px",
    backgroundColor: "blue",
    minWidth: "400px",
    maxWidth: "200px",
  };

  return (
    <view className="intro" style={containerStyle}>
      <view style={firstBoxStyle}>
        <text>width: 300px</text>
      </view>
      <view style={secondBoxStyle}>
        <text>width: 400px; max-width: 300px</text>
      </view>
      <view style={thirdBoxStyle}>
        <text>width: 300px; min-width: 400px; max-width: 200px</text>
      </view>
    </view>
  );
};

root.render(<MaxWidth />);

```

v0.11.0



## 语法

```css
/* <length> */
max-width: 120px;
max-width: 10em;

/* <percentage> */
max-width: 75%;
```

### 取值

默认盒子宽度没有限制。

#### `<length>`

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

#### `<percentage>`

以包含区块的宽度百分比定义 `max-width`。

## 形式定义

| 初始值 | 盒子宽度没有限制 |
| 适用元素 | 所有元件 |
| 是否支持继承 | 否 |
| 是否支持动画 | yes |
| 百分比 | refer to the width of the containing block |


## 形式语法

```
max-width = <length-percentage [0,∞]>

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

## 与 Web 的区别

[mdn: max-width](https://developer.mozilla.org/en-US/docs/Web/CSS/max-width)

1. 不支持 `none`，`max-content`，`min-content`，`fit-content`。
2. 在 Lynx 中 `max-width` 会覆盖 [`min-width`](/next/zh/api/css/properties/min-width.md)。
3. 在 Lynx 中，如果指定的 `max-width` 小于 `min-width`，则 `min-width` 会被忽略。

## 兼容性

**Compatibility Table**

**Query:** `css.properties.max-width`

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

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

**`max-width`**

sets the maximum width of an element.

**Specifications:**

