下面图片来自网络:
line-height
CSS 属性用于设置多行元素的空间量,如多行文本的间距。对于块级元素,它指定元素行盒(line boxes)的最小高度。对于非替代的 inline 元素,它用于计算行盒(line box)的高度。
line-height
是参考font-size
的值计算的,如下图为无单位赋值;另外百分比、px等类似。
font-size:12px;
line-height:1;
/* 此时line-height=1*font-size=12px */
其次当
line-height
的值等于font-size
的值时,即此时行距为0,但实际显示效果如下。
HTML:
<div class="f1">
<span class="p1">测试一下line-height属性</span>
</div>
<div class="f2">
<span class="p2">测试一下line-height属性2.0</span>
</div>
CSS:
.p1 {
line-height: 40px;
outline: 1px solid #3d08fc;
font-size: 40px;
}
.p2 {
line-height: 40px;
outline: 1px solid #07f02a;
font-size: 40px;
}
.f1 {
outline: 1px solid red;
}
.f2 {
outline: 1px solid rgb(204, 255, 0);
}
效果:
个人认为在实际开发中,line-height
主要是对文字内容进行css样式调整,这里我们探讨一下其对块盒和行盒的区别。
该属性是块盒content
区域中行盒的最小高度,不涉及padding、border区域,在实际开发,通常用来使文字内容居中显示;且在块盒没有设置高度的前提下,则块盒高度默认等于line-height
的值。
第一张图片:父元素中设置的
line-height
值等于其自身盒子(border+padding+content)高度,导致其子元素中的文字内容下移。
第二张图片:父元素中的
line-height
值设置等于其自身盒子content区的高度,则其子元素继承后,其文字内容能在父元素中居中显示。
该属性的值就是该行盒的高度,但是用 Computed
在控制台得不到其高度(元素为inline,则不显示宽高);
CSS:
span{
line-height: 20px;
outline: 1px solid #3d08fc;
font-size: 20px;
}
div{
outline: 1px solid red;
}
HTML:
<div>
<span>测试一下line-height属性</span>
</div>
效果:
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有