# width

width 属性用于设置元素的宽度。width 默认设置内容区域的宽度,但如果 box-sizing 属性被设置为 border-box,就转而设置边框区域的宽度。

# uni-app x 兼容性

Web Android iOS HarmonyOS HarmonyOS(Vapor)
4.0 3.9 4.11 4.61 5.0

# 语法

width: <viewport-length>{1,2};

# 值限制

  • length
  • percentage

# width 的属性值

名称 兼容性 描述
auto
浏览器将会为指定的元素计算并选择一个宽度。
fit-content
取以下两种值中的较大值:

固有的最小宽度
固有首选宽度(max-content)和可用宽度(available)两者中的较小值

可表示为:min(max-content, max(min-content, <length-percentage>))
max-content
元素内容固有的(intrinsic)合适宽度。
min-content
元素内容固有的最小宽度。

注意

属性值为长度 <length> 时,App平台可以不设置单位,Web端必须设置单位,详情参考长度单位

# 默认值

auto

# 示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见

扫码体验(手机浏览器跳转到App直达页)

示例

<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
  <!-- #endif -->
  <view style="flex-grow: 1;">
    <text class="uni-tips">说明:左边是正常版本,右边是拍平版本</text>
    <view>
      <text>width: 100px</text>
      <view class="demo-box">
        <view class="common" style="width: 100px;"></view>
        <view class="common" style="width: 100px;" flatten></view>
      </view>
    </view>

    <view>
      <text>width: 200rpx</text>
      <view class="demo-box">
        <view class="common" style="width: 200rpx;"></view>
        <view class="common" style="width: 200rpx;" flatten></view>
      </view>
    </view>

    <view>
      <text>width: 30%</text>
      <view class="demo-box">
        <view class="common" style="width: 30%;"></view>
        <view class="common" style="width: 30%;" flatten></view>
      </view>
    </view>



    <view class="uni-common-mt">
      <text class="uni-title-text">setProperty 设置与 getPropertyValue 获取 width </text>
    </view>

    <!-- 普通版本 -->
    <view class="test-container">
      <view class="test-item">
        <text class="uni-subtitle-text">view 组件</text>
        <text class="uni-info">设置值: {{width}}</text>
        <text class="uni-info">获取值: {{widthActual}}</text>
        <view class="test-box">
          <view ref="viewRef" class="common test-view" :style="{ width: width }">
            <text>view</text>
          </view>
        </view>
      </view>

      <view class="test-item">
        <text class="uni-subtitle-text">text 组件</text>
        <text class="uni-info">设置值: {{width}}</text>
        <text class="uni-info">获取值: {{widthActualText}}</text>
        <view class="test-box">
          <text ref="textRef" class="common test-text" :style="{ width: width }">text</text>
        </view>
      </view>

      <view class="test-item">
        <text class="uni-subtitle-text">image 组件</text>
        <text class="uni-info">设置值: {{width}}</text>
        <text class="uni-info">获取值: {{widthActualImage}}</text>
        <view class="test-box">
          <image ref="imageRef" class="common test-image" :style="{ width: width }" src="/static/test-image/logo.png"></image>
        </view>
      </view>
    </view>

    <!-- 拍平版本 -->
    <view class="test-container">
      <view class="test-item">
        <text class="uni-subtitle-text">view 组件拍平</text>
        <text class="uni-info">设置值: {{width}}</text>
        <text class="uni-info">获取值: {{widthActualFlat}}</text>
        <view class="test-box">
          <view ref="viewRefFlat" class="common test-view-flatten" :style="{ width: width }" flatten>
            <text>view</text>
          </view>
        </view>
      </view>

      <view class="test-item">
        <text class="uni-subtitle-text">text 组件拍平</text>
        <text class="uni-info">设置值: {{width}}</text>
        <text class="uni-info">获取值: {{widthActualTextFlat}}</text>
        <view class="test-box">
          <text ref="textRefFlat" class="common test-text-flatten" :style="{ width: width }" flatten>text</text>
        </view>
      </view>

      <view class="test-item">
        <text class="uni-subtitle-text">image 组件拍平</text>
        <text class="uni-info">设置值: {{width}}</text>
        <text class="uni-info">获取值: {{widthActualImageFlat}}</text>
        <view class="test-box">
          <image ref="imageRefFlat" class="common test-image-flatten" :style="{ width: width }" flatten src="/static/test-image/logo.png"></image>
        </view>
      </view>
    </view>

    <view class="uni-common-mt uni-common-mb">
      <text class="uni-tips">第一个枚举值,'' (空字符串) - 空值情况</text>
      <enum-data :items="widthEnum" title="width 枚举值" @change="radioChangeWidth" :compact="true"></enum-data>
      <input-data :defaultValue="width" title="width 自定义值" type="text" @confirm="inputChangeWidth"></input-data>
    </view>

    <view class="uni-common-mt uni-common-mb">
      <text class="uni-title-text">默认宽高规则 (无宽高会不显示或使用默认值)</text>
    </view>

    <view>
      <text>空 view - 无宽高,有背景色 (预期不显示)</text>
      <text class="uni-info">getPropertyValue: width={{emptyElementValues.emptyViewWidth}}, height={{emptyElementValues.emptyViewHeight}}</text>
      <view class="demo-box">
        <view ref="emptyViewRef" style="background-color: cyan;"></view>
        <view ref="emptyViewFlatRef" style="background-color: cyan;" flatten></view>
      </view>
    </view>

    <view>
      <text>空 text - 无文字、无宽高,有背景色</text>
      <text class="uni-info">getPropertyValue: width={{emptyElementValues.emptyTextWidth}}, height={{emptyElementValues.emptyTextHeight}}</text>
      <view class="demo-box">
        <text ref="emptyTextRef" style="background-color: cyan;"></text>
        <text ref="emptyTextFlatRef" style="background-color: cyan;" flatten></text>
      </view>
    </view>

    <view>
      <text>空 image - 无 src、无宽高,有背景色 (image 有默认宽高)</text>
      <text class="uni-info">getPropertyValue: width={{emptyElementValues.emptyImageWidth}}, height={{emptyElementValues.emptyImageHeight}}</text>
      <view class="demo-box">
        <image ref="emptyImageRef" style="background-color: cyan;"></image>
        <image ref="emptyImageFlatRef" style="background-color: cyan;" flatten></image>
      </view>
    </view>

    <view>
      <text>空 scroll-view - 无宽高,有背景色</text>
      <text class="uni-info">getPropertyValue: width={{emptyElementValues.emptyScrollViewWidth}}, height={{emptyElementValues.emptyScrollViewHeight}}</text>
      <view class="demo-box">
        <scroll-view ref="emptyScrollViewRef" style="background-color: cyan;"></scroll-view>
      </view>
    </view>

    <!-- #ifdef APP-ANDROID || APP-IOS || APP-HARMONY -->
    <view class="uni-common-mb">
      <text>空 native-view - 无宽高,有背景色</text>
      <text class="uni-info">getPropertyValue: width={{emptyElementValues.emptyNativeViewWidth}}, height={{emptyElementValues.emptyNativeViewHeight}}</text>
      <view class="demo-box">
        <native-view ref="emptyNativeViewRef" style="background-color: cyan;"></native-view>
      </view>
    </view>
    <!-- #endif -->

    <!-- #ifdef WEB -->
    <view class="container">
      <text>嵌套子元素 v-deep width: 150rpx</text>
      <child-width-uvue></child-width-uvue>
    </view>
    <!-- #endif -->
  </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script setup lang="uts">
  import childWidthUvue from "./child-width.uvue"
  import { ItemType } from '@/components/enum-data/enum-data-types'

  const widthEnum: ItemType[] = [
    { value: 0, name: '' },
    { value: 1, name: '0' },
    { value: 2, name: '0px' },
    { value: 3, name: '50px' },
    { value: 4, name: '0%' },
    { value: 5, name: '50%' },
    { value: 6, name: 'auto' }
  ]

  const width = ref('100px')
  const widthActual = ref('')
  const widthActualText = ref('')
  const widthActualImage = ref('')
  const widthActualFlat = ref('')
  const widthActualTextFlat = ref('')
  const widthActualImageFlat = ref('')
  const viewRef = ref(null as UniElement | null)
  const textRef = ref(null as UniTextElement | null)
  const imageRef = ref(null as UniImageElement | null)
  const viewRefFlat = ref(null as UniElement | null)
  const textRefFlat = ref(null as UniTextElement | null)
  const imageRefFlat = ref(null as UniImageElement | null)

  // 空元素 ref
  const emptyViewRef = ref(null as UniElement | null)
  const emptyViewFlatRef = ref(null as UniElement | null)
  const emptyTextRef = ref(null as UniTextElement | null)
  const emptyTextFlatRef = ref(null as UniTextElement | null)
  const emptyImageRef = ref(null as UniImageElement | null)
  const emptyImageFlatRef = ref(null as UniImageElement | null)
  const emptyScrollViewRef = ref(null as UniElement | null)
  const emptyNativeViewRef = ref(null as UniElement | null)

  // 空元素宽高值
  const emptyElementValues = reactive({
    emptyViewWidth: '',
    emptyViewHeight: '',
    emptyTextWidth: '',
    emptyTextHeight: '',
    emptyImageWidth: '',
    emptyImageHeight: '',
    emptyScrollViewWidth: '',
    emptyScrollViewHeight: '',
    emptyNativeViewWidth: '',
    emptyNativeViewHeight: ''
  })

  const getEmptyElementValues = () => {
    nextTick(() => {
      if (emptyViewRef.value != null) {
        emptyElementValues.emptyViewWidth = emptyViewRef.value.style.getPropertyValue('width') ?? ''
        emptyElementValues.emptyViewHeight = emptyViewRef.value.style.getPropertyValue('height') ?? ''
      }
      if (emptyTextRef.value != null) {
        emptyElementValues.emptyTextWidth = emptyTextRef.value.style.getPropertyValue('width') ?? ''
        emptyElementValues.emptyTextHeight = emptyTextRef.value.style.getPropertyValue('height') ?? ''
      }
      if (emptyImageRef.value != null) {
        emptyElementValues.emptyImageWidth = emptyImageRef.value.style.getPropertyValue('width') ?? ''
        emptyElementValues.emptyImageHeight = emptyImageRef.value.style.getPropertyValue('height') ?? ''
      }
      if (emptyScrollViewRef.value != null) {
        emptyElementValues.emptyScrollViewWidth = emptyScrollViewRef.value.style.getPropertyValue('width') ?? ''
        emptyElementValues.emptyScrollViewHeight = emptyScrollViewRef.value.style.getPropertyValue('height') ?? ''
      }
      if (emptyNativeViewRef.value != null) {
        emptyElementValues.emptyNativeViewWidth = emptyNativeViewRef.value.style.getPropertyValue('width') ?? ''
        emptyElementValues.emptyNativeViewHeight = emptyNativeViewRef.value.style.getPropertyValue('height') ?? ''
      }
    })
  }

  const getPropertyValues = () => {
    widthActual.value = viewRef.value?.style.getPropertyValue('width') ?? ''
    widthActualFlat.value = viewRefFlat.value?.style.getPropertyValue('width') ?? ''
    widthActualText.value = textRef.value?.style.getPropertyValue('width') ?? ''
    widthActualTextFlat.value = textRefFlat.value?.style.getPropertyValue('width') ?? ''
    widthActualImage.value = imageRef.value?.style.getPropertyValue('width') ?? ''
    widthActualImageFlat.value = imageRefFlat.value?.style.getPropertyValue('width') ?? ''
  }

  const changeWidth = (value: string) => {
    width.value = value
    viewRef.value?.style.setProperty('width', value)
    viewRefFlat.value?.style.setProperty('width', value)
    textRef.value?.style.setProperty('width', value)
    textRefFlat.value?.style.setProperty('width', value)
    imageRef.value?.style.setProperty('width', value)
    imageRefFlat.value?.style.setProperty('width', value)
    // 使用 nextTick 确保样式已应用后再获取值
    nextTick(() => {
      getPropertyValues()
    })
  }

  const radioChangeWidth = (index: number) => {
    const selectedItem = widthEnum.find((item): boolean => item.value === index)
    if (selectedItem != null) {
      changeWidth(selectedItem.name)
    }
  }

  const inputChangeWidth = (value: string) => {
    changeWidth(value)
  }

  onReady(() => {
    getPropertyValues()
    getEmptyElementValues()
  })

  defineExpose({
    radioChangeWidth,
    emptyElementValues
  })
</script>

<style>
  .common {
    height: 80px;
    background-color: cyan;
  }
  .demo-box {
    flex-direction: row;
    margin-top: 10px;
    justify-content: space-around;
  }

  .test-container {
    flex-direction: row;
    justify-content: space-between;
    margin-top: 10px;
  }

  .test-item {
    flex: 1;
    margin: 0 5px;
  }

  .test-box {
    width: 100%;
    height: 80px;
    background-color: gray;
  }

  /* #ifdef WEB */
  .container ::v-deep .box {
    width: 150px;
    height: 100px;
  }
  /* #endif */
</style>

# 参见