垂直和水平居中主要包裹div

22

可能是重复问题:
如何在DIV中居中显示另一个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>

?????


机器人是否放置了标签? - MiKo
1
@Bar 至少它不再有 C++ 标签了... - Cogwheel
1
重复:https://dev59.com/C3VD5IYBdhLWcg3wBW3d https://dev59.com/UkXRa4cB1Zd3GeqPoxek - ghoppe
5个回答

4

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

这里有一个解释这种情况的教程。看起来你需要使用方法1。


2
我建议你点击链接并按照它所告诉你的去做。 - Eric

0


<html>
<head>
    <title>?????????????????</title>
    <style type="text/css"><br>
    .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>


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>

0

试试这个:

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

0

这个教程介绍了一个在过去对我非常有效的方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接