为什么通用选择器的行高表现很奇怪?
据我所知,在 css 中,我们将 line-height 设置为整数,因为如果我们更改子元素中的字体大小,那么它会重新计算适当的值。除了通用选择器之外,这工作正常,因为如果我在通用选择器中设置 line-height:1.5em
,它不会创建重叠。但是,如果我在 div
中设置 line-height:1.5em
,则会产生重叠。
* { line-height: 1.5em; }
div { }
span { font-size:4em; }
<div>ghghghfghdfhdhhghdhdfghfhhgfhfhghdhghgh<br>hfghdfghdhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh<br>hfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhhghghfghdfhdhhghdhdfghfhhgfhfhghdhghgh<br>hfghdfghdhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh<br> <span>start hfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhofhfh<br>fhfhfhfhfhfhfhfhfhfhfhfhfhfhfgfgdfggdfgdfsgdfgdsfhjjghjhjfghjhfgjh end</span><br>.fhhhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhjhjjghjghjhgjhgjghhfhhh</div>
* { }
div { line-height:1.5em; }
span { font-size:4em; }
<div>ghghghfghdfhdhhghdhdfghfhhgfhfhghdhghgh<br>hfghdfghdhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh<br>hfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhhghghfghdfhdhhghdhdfghfhhgfhfhghdhghgh<br>hfghdfghdhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh<br> <span>start span overlap line-height:1.5em; inside parent hfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhofhfh<br>fhfhfhfhfhfhfhfhfhfhfhfhfhfhfgfgdfggdfgdfsgdfgdsfhjjghjhjfghjhfgjh end</span><br>.fhhhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhjhjjghjghjhgjhgjghhfhhh</div>
As I know in css we set line-height
as integer because if we change font size in child elements then it recalculate appropriate value. This works fine except with universal selector because if I set line-height:1.5em
in universal selector it does not create overlap. However, if I set line-height:1.5em
in div
it creates overlap.
* { line-height: 1.5em; }
div { }
span { font-size:4em; }
<div>ghghghfghdfhdhhghdhdfghfhhgfhfhghdhghgh<br>hfghdfghdhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh<br>hfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhhghghfghdfhdhhghdhdfghfhhgfhfhghdhghgh<br>hfghdfghdhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh<br> <span>start hfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhofhfh<br>fhfhfhfhfhfhfhfhfhfhfhfhfhfhfgfgdfggdfgdfsgdfgdsfhjjghjhjfghjhfgjh end</span><br>.fhhhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhjhjjghjghjhgjhgjghhfhhh</div>
* { }
div { line-height:1.5em; }
span { font-size:4em; }
<div>ghghghfghdfhdhhghdhdfghfhhgfhfhghdhghgh<br>hfghdfghdhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh<br>hfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhhghghfghdfhdhhghdhdfghfhhgfhfhghdhghgh<br>hfghdfghdhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh<br> <span>start span overlap line-height:1.5em; inside parent hfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhofhfh<br>fhfhfhfhfhfhfhfhfhfhfhfhfhfhfgfgdfggdfgdfsgdfgdsfhjjghjhjfghjhfgjh end</span><br>.fhhhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhfhjhjjghjghjhgjhgjghhfhhh</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通用选择器没有错。
第一个示例之所以起作用的原因是因为通用选择器指定
div
和span
行高
to1.5 EM
其Orignial线路高
。You specify the line-height for span here
For example two, you doesn't specify the
line-height
for span, so its line-height property will inherted from its parent container which isdiv
(24 px line height)Note that line-height is
There is nothing wrong with universal selector.
The reason that that first example works because universal selector specify
div
andspan
both of themline height
to1.5 em
of its orignialline-height
.You specify the line-height for span here
For example two, you doesn't specify the
line-height
for span, so its line-height property will inherted from its parent container which isdiv
(24 px line height)Note that line-height is inherted.