垂直对齐不工作在内联块 [英] Vertical align not working on inline-block

查看:105
本文介绍了垂直对齐不工作在内联块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知:


垂直对齐仅适用于内联,内联块,图像和表
元素。
它必须应用于子元素,与
父元素相反,与text-align不同。


然而,当我试图设置垂直对齐中间在一个inline-block元素,它没有工作。为什么?



< pretype =snippet-code-css lang-css prettyprint-override> #wrapper {border:1px solid black; width:500px; height:500px;}#content {border:1px solid black; display:inline-block; vertical-align:middle;}

 < div id ='wrapper'>< div id ='content'>内容< / div>< / div>  

方案

它无效,因为



如果只有一行,就像你的情况一样,它也是一个行框



使用 vertical-align :middle 中心 .content 。但是问题是,行框不是在包含块内垂直居中。



如果要在包含块的中间垂直居中,请参阅如何将文本垂直居中在div中与CSS对齐?


I was told that:

Vertical align only works for inline,inline-blocks,images,and table elements.
It has to be applied on the child element, as oppose to the parent element, unlike text-align.

However, when I tried to set vertical align middle on an inline-block element, it didn't work. Why?

#wrapper {
border: 1px solid black;
width: 500px;
height: 500px;
}
#content {
border: 1px solid black;
display: inline-block;
vertical-align: middle;
}

<div id = 'wrapper'>
<div id = 'content'> content </div>
</div>

解决方案

It doesn't work because vertical-align sets the alignment of inline-level contents with respect to their line box, not their containing block:

This property affects the vertical positioning inside a line box of the boxes generated by an inline-level element.

A line box is

The rectangular area that contains the boxes that form a line

When you see some text which has multiple lines, each one is a line box. For example, if you have

p { border: 3px solid; width: 350px; height: 200px; line-height: 28px; padding: 15px; }

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur.</p>

If there is only a single line, like in your case, it's also a line box

Using vertical-align: middle centers .content vertically inside that line box. But the problem is that the line box is not vertically centered inside the containing block.

If you want to center something vertically at the middle of the containing block see How to align text vertically center in div with CSS?

这篇关于垂直对齐不工作在内联块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆