Inline-block没有内容时,仍有高度

如下图,第二个line-block的span没有内容,但是div的高度是120px
demo: https://codepen.io/GitKou/pen/qNxNxj

<div>
  <span class="a">111</span>
  <span class="b"></span>
</div>
div {
  font-size: 20px;
  line-height: 60px;
  border: 1px solid #000;
}
.a{
  background: pink;
}
.b {
  display: inline-block;
  line-height: 10px;
  width: 100%;
  height: 0;
}

这里写图片描述
这里写图片描述
为什么呢?
(来自 http://www.w3.org/TR/CSS2/visuren.html#inline-boxes)

inline-block
This value causes an element to generate an inline-level block container. The inside of an inline-block is formatted as a block box, and the element itself is formatted as an atomic inline-level box.
inline
This value causes an element to generate one or more inline boxes.

(来自 http://stackoverflow.com/questions/20106428/why-does-inline-block-cause-this-div-to-have-height)
> The most important part for this topic would be that the element itself get's formatted not just the content. Every inline-block element will be seen as atomic inline-level box and thus take up space.

关键点是这个inline-block元素本身会被formatted,而不仅仅是content。每个inline-block元素都会没看做line-level的box,他们在他们自己的inline formatting 上下文中被视为一个透明的box,所以会占据空间。对外层来说他是inline,所以外层div设置的font-size和line-height,对其高度有影响。

Inline-level elements are those elements of the source document that do not form new blocks of content; the content is distributed in lines (e.g., emphasized pieces of text within a paragraph, inline images, etc.). The following values of the ‘display’ property make an element inline-level: ‘inline’, ‘inline-table’, and ‘inline-block’. Inline-level elements generate inline-level boxes, which are boxes that participate in an inline formatting context.

An inline box is one that is both inline-level and whose contents participate in its containing inline formatting context. A non-replaced element with a ‘display’ value of ‘inline’ generates an inline box. Inline-level boxes that are not inline boxes (such as replaced inline-level elements, inline-block elements, and inline-table elements) are called atomic inline-level boxes because they participate in their inline formatting context as a single opaque box.

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值