22

Possible Duplicate:
How to center DIV in DIV?

Now i try

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

?????

Community
  • 1
  • 1
John Nall
  • 839
  • 1
  • 7
  • 11
  • Have tags been placed by robot? – MiKo May 12 '10 at 21:41
  • 1
    @Bar at least it doesn't have C++ tag anymore ... – Cogwheel May 12 '10 at 21:46
  • 1
    duplicate: http://stackoverflow.com/questions/114543/how-to-center-div-in-div http://stackoverflow.com/questions/49350/practical-solution-to-center-vertically-and-horizontally-in-html-that-works-in-ff – ghoppe May 12 '10 at 21:51

5 Answers5

4

vertical-align does not work the way most beginners expect it to work.

Here is one tutorial explaining the situation. It sounds like you want Method 1.

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
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>
mike clagg
  • 830
  • 7
  • 12
0

This tutorial covers a method that has worked well for me in the past.

warrenm
  • 31,094
  • 6
  • 92
  • 116
0

People may hate me for suggesting this, but place it within a TD, where vertical-aligning is still doable without inflicting any harm on yourself.

I've provided a working examples here: http://jsbin.com/ezolu3/edit

The markup follows:

<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>
Sampson
  • 265,109
  • 74
  • 539
  • 565
0

Try this:

<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>
Eric
  • 95,302
  • 53
  • 242
  • 374