# height

height CSS 属性指定了一个元素的高度。默认情况下,这个属性决定的是内容区( content area)的高度,但是,如果将 box-sizing 设置为 border-box , 这个属性决定的将是边框区域(border area)的高度。

# uni-app x 兼容性

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

# 语法

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

# 值限制

  • length
  • percentage

# height 的属性值

名称 兼容性 描述
auto
由浏览器为元素计算并选择一个高度。
fit-content
将 fill-content 公式中的可用位置替换为特定的参数以进行使用,如:min(max-content, max(min-content, ))
max-content
设置为允许的最大高度。
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>height: 100px</text>
        <view class="demo-box">
          <view class="common" style="height: 100px;"></view>
          <view class="common" style="height: 100px;" flatten></view>
        </view>
      </view>

      <view >
        <text>height: 50%</text>
        <view class="demo-box" style="height: 100px;">
          <view class="common" style="height: 50%;"></view>
          <view class="common" style="height: 50%;" flatten></view>
        </view>
      </view>

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

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

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

        <view class="test-item">
          <text class="uni-subtitle-text">image 组件</text>
          <text class="uni-info">设置值: {{height}}</text>
          <text class="uni-info">获取值: {{heightActualImage}}</text>
          <view class="test-box">
            <image ref="imageRef" class="common-dynamic test-image" :style="{ height: height }" 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">设置值: {{height}}</text>
          <text class="uni-info">获取值: {{heightActualFlat}}</text>
          <view class="test-box">
            <view ref="viewRefFlat" class="common-dynamic test-view-flatten" :style="{ height: height }" flatten>
              <text>view</text>
            </view>
          </view>
        </view>

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

        <view class="test-item">
          <text class="uni-subtitle-text">image 组件拍平</text>
          <text class="uni-info">设置值: {{height}}</text>
          <text class="uni-info">获取值: {{heightActualImageFlat}}</text>
          <view class="test-box">
            <image ref="imageRefFlat" class="common-dynamic test-image-flatten" :style="{ height: height }" 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="heightEnum" title="height 枚举值" @change="radioChangeHeight" :compact="true"></enum-data>
        <input-data :defaultValue="height" title="height 自定义值" type="text" @confirm="inputChangeHeight"></input-data>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

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

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

  const height = ref('100px')
  const heightActual = ref('')
  const heightActualText = ref('')
  const heightActualImage = ref('')
  const heightActualFlat = ref('')
  const heightActualTextFlat = ref('')
  const heightActualImageFlat = 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)

  const getPropertyValues = () => {
    heightActual.value = viewRef.value?.style.getPropertyValue('height') ?? ''
    heightActualFlat.value = viewRefFlat.value?.style.getPropertyValue('height') ?? ''
    heightActualText.value = textRef.value?.style.getPropertyValue('height') ?? ''
    heightActualTextFlat.value = textRefFlat.value?.style.getPropertyValue('height') ?? ''
    heightActualImage.value = imageRef.value?.style.getPropertyValue('height') ?? ''
    heightActualImageFlat.value = imageRefFlat.value?.style.getPropertyValue('height') ?? ''
  }

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

  const radioChangeHeight = (index: number) => {
    const selectedItem = heightEnum.find((item): boolean => item.value === index)
    if (selectedItem != null) {
      changeHeight(selectedItem.name)
    }
  }

  const inputChangeHeight = (value: string) => {
    changeHeight(value)
  }

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

  defineExpose({
    radioChangeHeight
  })
</script>

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

  .common-dynamic {
    width: 100px;
    background-color: cyan;
  }

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

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

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

</style>

# 参见