简体   繁体   English

元素内联时的额外像素

[英]Extra pixels when element is inline

I am struggling to understand how the calculation of width/height in inline elements works.我正在努力理解内联元素中宽度/高度的计算是如何工作的。 My question is very similar to this Extra pixels are being added to the span element yet slightly different.我的问题非常类似于这个额外的像素被添加到跨度元素但略有不同。

There is a div element of size 50x50.有一个大小为 50x50 的 div 元素。 Within the div, there is a span with padding 15px.在 div 中,有一个间距为 15px 的跨度。 The span contains SVG circle of size 20x20.跨度包含大小为 20x20 的 SVG 圆。

So there are three use cases:所以有三个用例:

  1. Only div is a block只有 div 是一个块

    • div - size 50x50 ✔️ div - 大小 50x50 ✔️
    • span - size: 50x47 ❌ where are those three pixels? span - size: 50x47 ❌ 那三个像素在哪里?
    • svg - size: 20x20 ✔️ svg - 尺寸:20x20 ✔️
  2. div and span is a block div 和 span 是一个块

    • div - size 50x50 ✔️ div - 大小 50x50 ✔️
    • span - size: 50x54 ❌ where do these 4 pixels come from? span - size: 50x54 ❌ 这 4 个像素是从哪里来的?
    • svg - size: 20x20 ✔️ svg - 尺寸:20x20 ✔️
  3. eveything is a block一切都是块

    • div - size 50x50 ✔️ div - 大小 50x50 ✔️
    • span - size: 50x50 ✔️跨度 - 尺寸:50x50 ✔️
    • svg - size: 20x20 ✔️ svg - 尺寸:20x20 ✔️

 span { /* display: block; */ padding: 15px; } div { height: 50px; width: 50px; } svg { /* display: block; */ height: 20px; width: 20px; }
 <div> <span> <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> </div>

CodePen is available here . CodePen 可在此处获得。

Note: I tried it in the latest Chrome but I think it will be the same everywhere.注意:我在最新的 Chrome 中尝试过,但我认为它在任何地方都是一样的。 It's probably just some fundamental thing I am missing.这可能只是我缺少的一些基本的东西。 :) :)

Your second case is covered here: Image inside div has extra space below the image .您的第二种情况在这里介绍: div 内的图像在图像下方有额外的空间 Due to the default alignment you will have extra space under your SVG.由于默认的 alignment 您将在 SVG 下有额外的空间。 This can be fixed by adding display:block like you discovered or by adding vertical-align:top which is more logical as solution:这可以通过像您发现的那样添加display:block vertical-align:top添加更合乎逻辑的解决方案来解决此问题:

 span { display: block; padding: 15px; outline:1px solid green; } div { height: 50px; width: 50px; margin:30px; outline:1px solid blue; } svg { height: 20px; width: 20px; outline:1px solid red; }
 <div> <span> <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> </div> <div> <span> <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" style="vertical-align:top;" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> </div>

Your first case is a bit tricky because it has nothing to do with the SVG or the width/height you are setting.您的第一个案例有点棘手,因为它与 SVG 或您设置的宽度/高度无关。 It's all about font metrics.这都是关于字体指标的。

To simplify let's remove the div around and consider different SVG inside the same span and without padding:为了简化,让我们删除周围的 div 并考虑在同一跨度内不同的 SVG 并且没有填充:

 span { border: 1px solid green; margin:0 10px; } svg { outline: 1px solid red; }
 <span> <svg viewBox="0 0 24 24" height="20" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> <span> <svg viewBox="0 0 24 24" height="30" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> <span> <svg viewBox="0 0 24 24" height="50" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> <span> <svg viewBox="0 0 24 24" height="200" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span>

Notice how the span has always the same height whataver the SVG inside due to the nature of inline element.请注意,由于内联元素的性质,跨度如何始终具有相同的高度,无论 SVG 内部。 Let's increase the font-size让我们增加font-size

 span { border: 1px solid green; margin:0 10px; } svg { outline: 1px solid red; } body { font-size:40px; }
 <span> <svg viewBox="0 0 24 24" height="20" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> <span> <svg viewBox="0 0 24 24" height="30" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> <span> <svg viewBox="0 0 24 24" height="50" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span> <span> <svg viewBox="0 0 24 24" height="200" xmlns="http://www.w3.org/2000/svg" > <circle strokeLinecap="butt" strokeDasharray="64" cx="12" cy="12" r="9" /> </svg> </span>

The span are now bigger in height and the SVG are kept the same.跨度现在更大,SVG 保持不变。 You will also note the small gap at the bottom of the SVG due to the alignment I explained previously.由于我之前解释过的 alignment,您还会注意到 SVG 底部的小间隙。 Try to add font-size:0 and see the result.尝试添加font-size:0并查看结果。

As you can see the height of your span has nothing to do with the SVG.如您所见,跨度的高度与 SVG 无关。 To that height, you add the vertical padding to get the final height.对于该高度,您添加垂直填充以获得最终高度。 In your case, the height was 17px and adding the padding you will have 47px which is close to 50px but there is no relation with.在您的情况下,高度为17px并添加填充,您将拥有47px接近50px但与之无关。

Note that you may get a different result than 47px if you test in different browsers/OS since the font will not for sure be the same and the initial height can vary.请注意,如果您在不同的浏览器/操作系统中进行测试,您可能会得到与47px不同的结果,因为字体肯定不会相同,并且初始高度可能会有所不同。

If you check the speficiation you can read:如果您检查规范,您可以阅读:

The 'height' property does not apply. 'height' 属性不适用。 The height of the content area should be based on the font ...内容区域的高度应根据字体...

The vertical padding, border and margin of an inline, non-replaced box start at the top and bottom of the content area,内联、非替换框的垂直内边距、边框和边距从内容区域的顶部和底部开始,

Making the span block element will change this behavior and you will have a more intuitive result as you noticed in your last example: 2*15px of padding + 20px of SVG height.制作 span 块元素将改变这种行为,您将获得更直观的结果,正如您在上一个示例中注意到的那样: 2*15px的填充 + 20px的 SVG 高度。

Related question with more detail in order to understand how the height of element are calculated: How to determine height of content-box of a block and inline element更详细的相关问题以了解如何计算元素的高度: How to determine height of content-box of a block and inline element

Another related question: Can specific text character change the line height?另一个相关问题:特定文本字符可以改变行高吗?

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

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