How to horizontal & vertical center a div?

This question already has an answer here:

  • How to horizontally center a <div> in another <div>? 82 answers
  • Center a DIV horizontally and vertically [duplicate] 7 answers

  • If you know the dimensions of the inner div you can use CSS.

    #outer {
        position: relative;
    }
    
    #inner {
        position: absolute;
        width: 200px;
        height: 200px;
        left: 50%;
        top: 50%;
        margin-top: -100px;
        margin-left: -100px;
    }
    

    There are other options using display: table-cell and vertical-align: middle etc.

    Other options include JavaScript to dynamically determine the dimensions of the inner div and set the negative margins like above.


    使用javaScript或尝试使用http://www.jakpsatweb.cz/css/css-vertical-center-solution.html


    The easiest and most reliable way to do it is with JavaScript. Try the code given in this blog post. Trying to do it in CSS is a nightmare, because CSS was not designed to handle vertical alignment.

    链接地址: http://www.djcxy.com/p/13204.html

    上一篇: 如何在父div中水平居中三个div?

    下一篇: 如何水平和垂直居中div?