## width
width 属性用于设置元素的宽度。width 默认设置内容区域的宽度,但如果 box-sizing 属性被设置为 border-box,就转而设置边框区域的宽度。
### uni-app x 兼容性
Web: 4.0; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61
### App平台拍平(flatten)兼容性 @flatten_compatibility
| Android(Vapor) | iOS(Vapor) | HarmonyOS(Vapor) |
| :- | :- | :- |
| 5.21 | 5.11 | 5.0 |
### 语法
```
width: {1,2};
```
### 值限制
- length
- percentage
### width 的属性值
| 名称 | 兼容性 | 描述 |
| :- | :- | :- |
| auto | Web: 4.0; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61 | 浏览器将会为指定的元素计算并选择一个宽度。 |
| fit-content | Web: 4.0; Android: x; iOS: x; HarmonyOS: x | 取以下两种值中的较大值:
固有的最小宽度
固有首选宽度(max-content)和可用宽度(available)两者中的较小值
可表示为:min(max-content, max(min-content, \)) |
| max-content | Web: 4.0; Android: x; iOS: x; HarmonyOS: x | 元素内容固有的(intrinsic)合适宽度。 |
| min-content | Web: 4.0; Android: x; iOS: x; HarmonyOS: x | 元素内容固有的最小宽度。 |
**注意**
> 属性值为长度 `` 时,App平台可以不设置单位,Web端必须设置单位,详情参考[长度单位](./README.md#length)。
### 默认值 @default-value
`auto`
### 示例
示例为[hello uni-app x alpha分支](https://gitcode.com/dcloud/hello-uni-app-x/blob/prod_alpha/pages/CSS/layout/width.uvue),与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例[另见](https://gitcode.com/dcloud/hello-uni-app-x/blob/master//pages/CSS/layout/width.uvue)
::: preview https://hellouniappx.dcloud.net.cn/web/#/pages/CSS/layout/width
> appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/CSS/layout/width
>示例
```vue
说明:左边是正常版本,右边是拍平版本
width: 100px
width: 200rpx
width: 30%
scroll-view 组件
width: 100px
width: 200px
setProperty 设置与 getPropertyValue 获取
view 组件
设置值: {{data.width}}
获取值: {{data.widthActual}}
view
text 组件
设置值: {{data.width}}
获取值: {{data.widthActualText}}
text
image 组件
设置值: {{data.width}}
获取值: {{data.widthActualImage}}
view 组件拍平
设置值: {{data.width}}
获取值: {{data.widthActualFlat}}
view
text 组件拍平
设置值: {{data.width}}
获取值: {{data.widthActualTextFlat}}
text
image 组件拍平
设置值: {{data.width}}
获取值: {{data.widthActualImageFlat}}
第一个枚举值,'' (空字符串) - 空值情况
默认宽高规则 (无宽高会不显示或使用默认值)
空 view - 无宽高,有背景色 (预期不显示)
getPropertyValue: width={{emptyElementValues.emptyViewWidth}}, height={{emptyElementValues.emptyViewHeight}}
空 text - 无文字、无宽高,有背景色
getPropertyValue: width={{emptyElementValues.emptyTextWidth}}, height={{emptyElementValues.emptyTextHeight}}
空 image - 无 src、无宽高,有背景色 (image 有默认宽高)
getPropertyValue: width={{emptyElementValues.emptyImageWidth}}, height={{emptyElementValues.emptyImageHeight}}
空 scroll-view - 无宽高,有背景色
getPropertyValue: width={{emptyElementValues.emptyScrollViewWidth}}, height={{emptyElementValues.emptyScrollViewHeight}}
空 native-view - 无宽高,有背景色
getPropertyValue: width={{emptyElementValues.emptyNativeViewWidth}}, height={{emptyElementValues.emptyNativeViewHeight}}
native-view组件: width: 100px 和 width: 200px
嵌套子元素 v-deep width: 150rpx
```
:::
### 参见
- [MDN Reference](https://developer.mozilla.org/docs/Web/CSS/width)
- [相关 Bug](https://issues.dcloud.net.cn/?mid=css.properties.width)