简体   繁体   English

垂直对齐身体内的div

[英]Vertically aligning my div within the body

Is there a CSS way to vertically align my div within the body element? 有没有一种CSS方法可以在body元素中垂直对齐我的div?

The thing is my div will have a different height each time, so its not constant. 事情是我的div每次都会有不同的高度,所以它不是恒定的。

These are the things I've tried but they dont work: 这些是我尝试但他们不工作的东西:

body { vertical-align: middle; }

#mainContent { 
   vertical-align: middle;
}

// Also this
body { margin-top: 20%; margin-bottom: 20%; }

I did it without table: ( demo on dabblet.com ) 我没有桌子就做到了:(在dabblet.com上演示

The main trick in this demo is that in the normal flow of elements going from top to bottom, so the margin-top: auto is set to zero. 这个演示的主要技巧是在正常的元素流从上到下,所以margin-top: auto设置为零。 However, for an absolutely positioned element acts the same distribution of free space, and similarly can be centered vertically at the specified top and bottom (does not work in IE7). 但是,对于绝对定位的元素,可以使用相同的自由空间分布,同样可以在指定的topbottom垂直居中(在IE7中不起作用)。

This trick will work with any sizes of div . 这个技巧适用于任何大小的div

HTML: HTML:

<div></div>

CSS: CSS:

div {
    width: 100px;
    height: 100px;
    background-color: red;

    position: absolute;
    top:0;
    bottom: 0;
    left: 0;
    right: 0;

    margin: auto;
}

A common problem indeed. 确实是个常见问题。 I have seen many people offering straight css solutions for this but they all require knowing the height of the element needing to be centered, so no help there. 我见过很多人为此提供直接的css解决方案,但他们都需要知道元素的高度需要居中,所以没有帮助。

I usually do it this way using jquery: 我通常使用jquery这样做:

$(document).ready(function(){
    site.resize();

    $(window).resize(function(){
        site.resize();
    });
});

var site = {
    resize: function(){
        var new_margin = Math.ceil(($(window).height() - $('#mainContent').height()) / 2);
        $('#mainContent').css('margin-top', new_margin + 'px');
    }
};

Surprisingly (or not), the vertical-align tool actually works best for this job. 令人惊讶(或不), vertical-align工具实际上最适合这项工作。 Best of all, no Javascript is required. 最重要的是,不需要Javascript。

In the following example, I am positioning the outer class in the middle of the body, and the inner class in the middle of the outer class. 在下面的例子中,我定位outer体内的中产阶级,和inner类在中间outer类。

Preview: http://jsfiddle.net/tLkSV/513/ 预览: http//jsfiddle.net/tLkSV/513/

HTML: HTML:

<div id="container">
    <span></span><div class="outer">
        <span></span><div class="inner">

        </div>
    </div>
</div>

CSS: CSS:

html, body {
    height: 100%;
    margin: 0;
    padding: 0; }
#container {
    text-align: center;
    height: 100%; }
span { 
    height: 100%;
    vertical-align: middle;
    display: inline-block; }
.outer {
    width: 100px;
    height: 200px;
    padding: 0;
    border: 1px solid #000;
    vertical-align: middle;
    display: inline-block; }
.inner {
    background: red;
    width: 30px;
    height: 20px;    
    vertical-align: middle;
    display: inline-block; }

Vertical align works by aligning the centers of elements that are next to each other. 垂直对齐通过对齐彼此相邻的元素的中心来工作。 Applying vertical-align to a single element does absolutely nothing. 将vertical-align应用于单个元素绝对没有任何意义。 If you add a second element that has no width but is the height of the container, your single element will move to vertically center with this no-width element, thus vertically centering it. 如果添加第二个没有宽度但是容器高度的元素,则单个元素将使用此无宽度元素移动到垂直居中,从而垂直居中。 The only requirements are that you set both elements to inline (or inline-block), and set their vertical-align attribute to vertical-align: middle . 唯一的要求是将两个元素都设置为内联(或内联块),并将其vertical-align属性设置为vertical-align: middle

Note: You may notice in my code below that my <span> tag and <div> tag are touching. 注意:您可能会在下面的代码中注意到我的<span>标记和<div>标记正在触及。 Because they are both inline elements, a space will actually add a space between the no-width element and your div, so be sure to leave it out. 因为它们都是内联元素,所以空格实际上会在无宽度元素和div之间添加一个空格,因此请务必将其保留。

You can do it without using tables, and without adding extra elements: 您可以在不使用表的情况下执行此操作,也无需添加额外元素:

<ul>
    <li>One short item</li>
    <li>Any real long text...</li>
    <li>Another short item</li>
</ul>

And then the CSS: 然后是CSS:

ul {
    list-style-type: none;
    display: table-row;
}
li {
    display: table-cell;
    vertical-align: middle;
}

You can see it here 你可以在这里看到它

It would work with any other kind of hierarchy, including div, p, etc. 它适用于任何其他类型的层次结构,包括div,p等。

Honestly, my opinion is often that if you're doing vertical alignment you should still be using a table. 老实说,我的意见通常是,如果你正在进行垂直对齐,你仍然应该使用一张桌子。 I know it's often frowned upon, but it is still the simplest and cleanest way to vertically center something. 我知道它经常不受欢迎,但它仍然是垂直居中的最简单,最干净的方式。

HTML HTML

<table>
    <tbody>
        <tr>
            <td>
                <div>Your DIV here.</div>
            </td>
        </tr>
    </tbody>
</table>

CSS CSS

td {vertical-align: middle;}

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

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