欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

span设为inline-block之后,为什么未包含文字时下面会多出一条空白?

程序员文章站 2022-06-07 08:41:46
...

回复内容:

泻药
貌似说的是这个
w3help.org/zh-cn/causes 为什么我提问从来都无人问津? 这个问题的主要原因是:元素的默认vertical-align是baseline。在CSS规范的Visual formatting model details中关于vertical-align: baseline有以下的描述:
baselineAlign the baseline of the box with the baseline of the parent box. If the box does not have a baseline, align the bottom margin edge with the parent's baseline.
因为推断一个元素的baseline的位置,需要根据它使用的字体信息来推断(一个span中的文字可能因为字符集不同,而使用不同的字体)。因此,一个没有内容的inline-block也就没有了baseline。因此,对齐的时候,就会将它的底边作为baseline在父容器中对齐。你的例子中多出来的3px就是baseline到底边的距离。
写到这里,发现父容器的baseline应该也算不出来啊。不知道谁可以补充一下。
解决这个问题的方法就很多了:设置span的vertical-align为bottom;或为span添加内容为空格;