简体   繁体   English

高度为100%的垂直对齐嵌入式块

[英]Vertical Align Inline-Block With Height 100%

First I'll list my code and the link to JSFiddle. 首先,我将列出我的代码和JSFiddle的链接。

HTML 的HTML

<div id="wrapper">
    <div id="container">
        <div id="content">
            Here is the content
        </div>
    </div>
</div>

JS JS

body,html{height:100%;}
#wrapper{
    height:100%;
    background-color:green;
}
#container {
    display: inline-block ;
    height:100%;
    width:100%;
    background-color:red;
    text-align:center;
    vertical-align:middle;
}
#content
{
    display:inline-block;
    background-color:blue;
}

http://jsfiddle.net/x11joex11/b4ZBg/ http://jsfiddle.net/x11joex11/b4ZBg/

(Newer one with more content for vertical center testing) (较新的具有更多用于垂直中心测试的内容)

http://jsfiddle.net/x11joex11/sDWxN/11/ http://jsfiddle.net/x11joex11/sDWxN/11/

What I'm trying to do is vertically center the blue highlighted DIV in the center of the red div. 我想做的是将蓝色突出显示的DIV 垂直居中于红色div的中心。 Is there a way to do this using inline-block and not table-cells ? 有没有一种方法可以使用内联块而不是表单元格来做到这一点?

The height of the containing div also HAS to be 100% not a set pixel amount. 包含div的高度也必须是100%,而不是设置的像素量。

The content will also be variable height 内容也将是可变高度

I am trying to avoid table-cell display because of browser bugs, but if it's the only option I would like to know that also. 由于浏览器的错误,我试图避免显示表格单元格,但是如果这是唯一的选择,我也想知道。 Any solution to this issue would be appreciative. 任何解决此问题的方法将是有益的。

The art of vertical centring with inline-block is to understand that the inline-level elements are centred in their line-box. 使用inline-block垂直居中的技巧是要了解inline-level元素在其线盒中居中。 So you need to make the line-height match the height of the containing box. 因此,您需要使line-height与包含框的高度匹配。

The line-height is determined by a combination of the line-height setting of the containing block and the content of the line. 行高由包含块的行高设置和行内容共同决定。

However the line-height of the containing box cannot be set in terms of a percentage of the height of the containing box, so that provides no solution. 但是,容纳盒的行高不能以容纳盒的高度的百分比来设置,因此无法提供解决方案。

Instead, we can create some content on the same line as the content we want to align that's the height of the containing block using 相反,我们可以在要对齐的内容的同一行上创建一些内容,即使用

#container:before {
    display:inline-block;
    content: '';
    height:100%;
    vertical-align:middle;
}

which will force the line height be tall enough to contain that content. 这将迫使行高足够高以包含该内容。

The other thing necessary is to note that vertical-align is applied to the boxes being aligned, rather than the containing box. 另一件事是要注意, vertical-align是应用于vertical-align的框,而不是包含框。

The result is http://jsfiddle.net/9j95x/ 结果是http://jsfiddle.net/9j95x/

You can use: 您可以使用:

top: 50%;
position: relative;

on #content , like so: #content ,如下所示:

#content
{
    display:inline-block;
    background-color:blue;
    position: relative;
    top: 50%;
}

Fork: http://jsfiddle.net/brandonscript/sDWxN/9/ 叉子: http//jsfiddle.net/brandonscript/sDWxN/9/

Here's my quick response: http://jsfiddle.net/H9nHh/ 这是我的快速回复: http : //jsfiddle.net/H9nHh/

Basically use: display:table; 基本用途: display:table; for #container 用于#容器

and display:table-cell; display:table-cell; for #content. 用于#content。 I then created another div with a class for x to style it to your needs. 然后,我用x的类创建了另一个div以根据您的需要设置样式。

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

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