简体   繁体   English

理解不同字体大小的 CSS 换行

[英]Understanding CSS line wrapping with different font-sizes

Consider this example:考虑这个例子:

 div.big { font-size: 2em; font-weight: bold; } div.big .small { font-size: 40%; } div.small { font-size: 0.8em; font-weight: bold; } div.small .big { font-size: 2rem; }
 <div class="big">Lorem ipsum dolor sit amet, <span class="small">consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</span></div> <p>Something else</p> <div class="small"><span class="big">Lorem ipsum dolor sit amet, </span>consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</div> <p>Something else</p>

In the first case (small font-size inline element inside a bigger font-size block element), the spacing around the wrapped text isn't the same as in the second case (big font-size inline element inside a smaller font-size block element).在第一种情况下(较大字体大小块元素内的小字体大小内联元素),环绕文本周围的间距与第二种情况(较小字体大小内联元素内的大字体大小内联元素)不同块元素)。

预览 1

It gets even more obvious when wrapping on multiple lines:在多行换行时,它变得更加明显:

预览 2

What is the property/behavior that causes this difference, and is there a way to make the first example look like the second one without changing the markup?导致这种差异的属性/行为是什么,有没有办法在不更改标记的情况下使第一个示例看起来像第二个示例?

The reason for what you see is the line-height , which by default is relative to the font-size , ie to the font-size of the parent/block element.您看到的原因是line-height ,默认情况下它与font-size ,即与父/块元素的font-size相关。 It usually is defined in percent or as a multiplication factor like 1.6 - if you don't see any such parameter in the stylesheet, there's still an according browser default setting, usually between 1.4 and 1.6.它通常以百分比或乘法因子(如1.6 - 如果您在样式表中没有看到任何此类参数,则仍有相应的浏览器默认设置,通常介于 1.4 和 1.6 之间。

If you set the line-height to a fixed pixel value (which in real life you should almost never do), both examples look the same, as in the following variation of your code:如果您将 line-height 设置为固定像素值(在现实生活中您几乎不应该这样做),则两个示例看起来相同,如下面的代码变体所示:

 div.big { font-size: 2em; font-weight: bold; line-height: 16px; } div.big .small { font-size: 40%; } div.small { font-size: 0.8em; font-weight: bold; line-height: 16px; } div.small .big { font-size: 2rem; }
 <div class="big">Lorem ipsum dolor sit amet, <span class="small">consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</span></div> <p>Something else</p> <div class="small"><span class="big">Lorem ipsum dolor sit amet, </span>consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</div> <p>Something else</p>

Welcome to SO.欢迎来到 SO。

Just add display: contents;只需添加display: contents; to

div.big {
  font-size: 2em;
  font-weight: bold;
  display: contents;
}

DEMO:演示:

 div.big { font-size: 2em; font-weight: bold; display: contents; } div.big .small { font-size: 40%; } div.small { font-size: 0.8em; font-weight: bold; } div.small .big { font-size: 2rem; }
 <div class="big">Lorem ipsum dolor sit amet, <span class="small">consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</span></div> <p>Something else</p> <div class="small"><span class="big">Lorem ipsum dolor sit amet, </span>consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</div> <p>Something else</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM