垂直和水平居中主包装div

时间:2010-05-12 21:33:38

标签: html css

  

可能重复:
  How to center DIV in DIV?

现在我尝试

<html>
<head>

        <title>?????????????????</title>
        <style type="text/css">   
    body
    {

            margin-left: auto;
            margin-right:auto;
    }

    #wrap
    {
            background: black;
            margin-left: auto;
            margin-right:auto;
            height:450px;
            width:450px;
            position:absolute;
            top:50%;
            right:50%;
            left:50%;
            margin-top:-225px;
         }
    </style>
</head>
<body>
        <div id="wrap">
                Hello
        </div>
</body>
</html>

···

5 个答案:

答案 0 :(得分:4)

vertical-align不能像大多数初学者期望的那样工作。

Here是一个解释这种情况的教程。听起来你想要方法1。

答案 1 :(得分:0)


<html>
<head>
    <title>?????????????????</title>
    <style type="text/css">
.ui-container { background: red; } .ui-wrapper { margin: auto; background: black; height:450px; width:450px; color: red; } </style> </head> <body> <div class="ui-container"> <div class="ui-wrapper"> <p>Hello</p> </div> </div> </body> </html>

答案 2 :(得分:0)

This tutorial涵盖了一种过去对我有用的方法。

答案 3 :(得分:0)

人们可能讨厌我这样做,但是把它放在TD内,垂直对齐仍然可行,而不会对自己造成任何伤害。

我在这里提供了一个工作示例:http://jsbin.com/ezolu3/edit

标记如下:

<table id="vCent">
  <tbody>
    <tr>
      <td valign="center">

        <div id="foo">
          <p>Using tables incorrectly is wrong, without question. But sometimes there are other things that are more wrong - "wrongerer," if you will. Causing yourself unnecessary frustration trying to get an element to center itself vertically, for example, is one of those <em>wrongerer</em> things. Don't bother, just go with what works.</p>
        </div>

      </td>
    </tr>
  </tbody>
</table>

答案 4 :(得分:-1)

试试这个:

<html>
    <head>
        <title>?????????????????</title>
        <style type="text/css">
        #content
        {
            background: black;
            margin: -225px;
            height: 450px;
            width: 450px;
        }
        #wrap
        {
            height: 0px;
            width: 0px;

            position:absolute;
            top:50%;
            left:50%;
        }
        </style>
    </head>
    <body>
        <div id="wrap">
            <div id="content">
                Hello
            </div>
        </div>
    </body>
</html>