最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vertically aligning my div within the body - Stack Overflow

programmeradmin2浏览0评论

Is there a CSS way to vertically align my div within the body element?

The thing is my div will have a different height each time, so its not constant.

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%; }

Is there a CSS way to vertically align my div within the body element?

The thing is my div will have a different height each time, so its not constant.

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%; }
Share Improve this question asked Dec 8, 2011 at 1:54 sazrsazr 25.9k70 gold badges211 silver badges385 bronze badges 7
  • 1 There are many links online about this; it's a tricky thing to do unless you know the height of the div. phrogz.net/css/vertical-align/index.html – Jeffrey Sweeney Commented Dec 8, 2011 at 1:57
  • vertical-align works only for table-cell elements.. – ptriek Commented Dec 8, 2011 at 1:58
  • Would it just be easier with javascript? I am better a javascript than css. – sazr Commented Dec 8, 2011 at 1:58
  • If you want to use javascript, you can just get the div size after the page is loaded and position it with javascript. – Ronnie Commented Dec 8, 2011 at 2:00
  • Check these examples (although they're divs within divs): stackoverflow.com/questions/8328314/… and stackoverflow.com/questions/7967217/… – Kamil Sindi Commented Dec 8, 2011 at 2:04
 |  Show 2 more comments

5 Answers 5

Reset to default 17

I did it without table: (demo on 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. 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).

This trick will work with any sizes of div.

HTML:

<div></div>

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.

I usually do it this way using 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. Best of all, no Javascript is required.

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.

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

HTML:

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

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

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. 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.

Note: You may notice in my code below that my <span> tag and <div> tag are touching. 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.

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:

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.

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

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

CSS

td {vertical-align: middle;}
发布评论

评论列表(0)

  1. 暂无评论