V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
hblevins
V2EX  ›  CSS

给 div 和 span 元素设置 line-height 为 0,为什么 div 几乎失去了高度,但 span 的高度没有受到影响?

  •  
  •   hblevins · Jun 27, 2024 · 3877 views
    This topic created in 681 days ago, the information mentioned may be changed or developed.
    4 replies    2024-08-02 04:28:00 +08:00
    QAQqingju
        1
    QAQqingju  
       Jun 27, 2024
    块级元素和行内元素
    tool2dx
        2
    tool2dx  
       Jun 27, 2024   ❤️ 1
    这属于 css 基本知识了。一个是 block 元素,另一个是 inline 元素。

    inline 元素设置高度,只能用过设置字体的大小,也就是 font-size ,你设置 line-height 没用。
    chnwillliu
        4
    chnwillliu  
       Aug 2, 2024 via Android
    span 或者说 inline boxes 有两个高度,一个是它的 content area 的高度,由第一个可用字体的上升下降线决定;第二个高度是它在 line box 中占的逻辑高度,由 line-height 控制,将来还会有 text-box-edge 这种东西来结合字体中的各种基线来控制。

    span 的 background border 都是放 content area 上,包括 getBoundingClientRect 。

    而它的逻辑高度配合 vertical-align 和其他同行的盒子,一起构成行盒的高度,最后体现在 div 这个 block container 的高度上。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3701 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 69ms · UTC 10:27 · PVG 18:27 · LAX 03:27 · JFK 06:27
    ♥ Do have faith in what you're doing.