# line-height

line-height 属性用于设置多行文本的间距。

# uni-app x 兼容性

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

# 语法

line-height: normal | <number> | <length> | <percentage>;

# 值限制

  • length
  • number

# line-height 的属性值

名称 兼容性 描述
normal
由各平台实现,约为 1.2,取决于元素的 font-family。

# 默认值

normal

HBuilderX5.0+ 调整 line-height 默认值为 normal,该值不是固定高度,而是根据字体调节,可保障不会发生行高太小裁剪字体。但在不同平台,由于其默认字体不同,会导致行高的绝对高度有差异。HBuilderX5.0以下版本默认值曾为 1.2em,该值在某些字体下会出现上下边缘裁剪。 如需统一各平台行高绝对值且避免裁剪,请设置1.5em以上的数字。

# 适用组件

# tips

App平台仅支持以像素值(px)、相对像素值(rpx)、无单位和相对元素字体大小单位(em)。不支持百分比。

无单位表示数字值乘以该元素的字体大小,在App平台由于不支持继承,无单位与em单位相同,即 1.5 与 1.5em 效果一致。

注意
Web平台 无单位 和em单位在line-height样式继承自父元素时存在差异:

  • 无单位
    子元素会用自己的 font-size 乘以这个无单位数值,子元素的字体变大时,行高自动适应
  • em单位
    父元素先根据自己的 font-size 计算出固定像素值,子元素使用这个固定像素值,如果子元素字体变大,行高保持不变,可能出现文字重叠

# 示例

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

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

示例

<template>
  <view style="flex-grow: 1;">
    <view style="height: 500px;background-color: gray;justify-content: center;align-items: center;">
      <text class="common" style="line-height: 75px;">line-height: 75px</text>
      <text class="line-height-3 common">line-height: 3</text>
      <text class="common" style="line-height: 3em;">line-height: 3em</text>
      <text class="common" style="line-height: 3;">line-height: 3\nline-height: 3\nline-height: 3</text>
    </view>
  </view>
</template>

<script>

</script>

<style>
  .common {
    font-size: 20px;
    border: 1px red solid;
    margin: 10px 0;
    padding: 0 10px;
  }
  .line-height-3 {
    line-height: 3;
  }
</style>

# 参见