简体   繁体   English

垂直对齐的行内块元素上方的空白

[英]White space above a vertically aligned inline-block element

Code代码

 .outer { display: block; background: lightblue; font-size: 50px; }.inner { display: inline-block; width: 50px; height: 50px; background: red; vertical-align: middle; }
 <div class="outer"><div class="inner"></div></div>

You can play and see the result here: https://jsfiddle.net/y1jkw3nu/5/你可以在这里玩看结果: https://jsfiddle.net/y1jkw3nu/5/

Problem问题

I have this weird empty space above the red square.我在红色方块上方有这个奇怪的空白区域。

Question问题

Can someone explain to me where does this empty space come from?有人可以向我解释这个空白空间是从哪里来的吗? I understand that it's because of font-size: 50px , but the .inner div have vertical-align: middle .我知道这是因为font-size: 50px ,但是.inner div 有vertical-align: middle Doesn't it mean it should be positioned in the middle of the line?这不是意味着它应该位于行的中间吗?

Thats because the 'content' inside reserves height for the text that could be there... put line-height:0 and its gone..那是因为里面的“内容”为可能存在的文本保留了高度......放 line-height:0 并且它消失了..

 .outer { display: block; background: lightblue; font-size: 50px; line-height:0; } .inner { display: inline-block; width: 50px; height: 50px; background: red; vertical-align: middle; }
 <div class="outer"><div class="inner"></div></div>

First you need to understand what vertical-align is doing here:首先,您需要了解 vertical-align 在这里做了什么:

middle中间

Align the vertical midpoint of the box with the baseline of the parent box plus half the x-height of the parent.框的垂直中点与父框的基线加上父框 x 高度的一半对齐。

So you need to find the "baseline", the "x-height" and the midpoint of your box (this one is trivial).所以你需要找到你的盒子的“基线”、“x-height”和中点(这个很简单)。

To better find those values, add some text:为了更好地找到这些值,添加一些文本:

 .outer { display: block; background: linear-gradient(blue 0 0) 50% calc(50% + .33em - .5ex)/100% 2px no-repeat, linear-gradient(#fff 0 0) 50% calc(50% + .33em)/100% 2px no-repeat lightblue; font-size: 50px; } .inner { display: inline-block; width: 50px; height: 50px; background: linear-gradient(green 0 0) 50%/100% 2px no-repeat red; vertical-align: middle; }
 <div class="outer">ÂBp <div class="inner"></div> </div>

In the above, note how the "blue" (baseline + half the x-height) and "green" (midpoint) line are aligned.在上面,请注意“蓝色”(基线 + x 高度的一半)和“绿色”(中点)线是如何对齐的。 Also note how the blue is shifted by 0.5ex from the baseline (in white).另请注意蓝色如何从基线(白色)偏移0.5ex

Even without text, the above still apply and only the font properties will affect this alignment.即使没有文本,上述内容仍然适用,只有字体属性会影响这种对齐方式。

... if each line box starts with a zero-width inline box with the element's font and line height properties. ...如果每个行框都以具有元素字体和行高属性的零宽度行内框开头。 We call that imaginary box a "strut."我们称那个假想的盒子为“支柱”。 (The name is inspired by TeX.). (这个名字的灵感来自 TeX。)。

The height and depth of the font above and below the baseline are assumed to be metrics that are contained in the font.基线上方和下方字体的高度和深度被假定为包含在字体中的度量。 (For more details, see CSS level 3.) (有关更多详细信息,请参阅 CSS 级别 3。)

That "strut" element is the one used instead of the text.该“支柱”元素是用来代替文本的元素。

You can also reduce the size of the box and you will have space at the bottom as well:您还可以减小框的大小,并且底部也将有空间:

 .outer { display: block; background: linear-gradient(blue 0 0) 50% calc(50% + .33em - .5ex)/100% 2px no-repeat, linear-gradient(#fff 0 0) 50% calc(50% + .33em)/100% 2px no-repeat lightblue; font-size: 50px; } .inner { display: inline-block; width: 50px; height: 30px; background: linear-gradient(green 0 0) 50%/100% 2px no-repeat red; vertical-align: middle; }
 <div class="outer">ÂBp <div class="inner"></div> </div>

You could give the .outer a height: 50px and the .inner position:absolute .你可以给.outer一个height: 50px.inner position:absolute

    .outer {
    display: block;
    background: lightblue;
    font-size: 50px;
    height:50px;
}

.inner {
    display: inline-block;
    width: 50px;
    height: 50px;
    background: red;
    vertical-align: middle;
    position:absolute;
}

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

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