1

What is the quickest way for me to get a div vertically centered - Cross browser?

HTML:

<body>
    <div id="mainContainer">
        <header>
            blah
            </header><!-- End header -->
        </div><!-- End mainContainer -->

</body>

CSS:

#mainContainer{
    width:960px;
    margin:60px auto;
    background:#FFF;
    border:1px solid black;
}
header{
    padding:30px 0 30px 0;
    background:red;
    margin:0 auto;
}
Karl Keefer
  • 645
  • 1
  • 5
  • 13
Jess McKenzie
  • 8,345
  • 27
  • 100
  • 170

2 Answers2

2

If you can specify a height, then you can use

position:absolute
top:50%;
left:50%;
width:200px;
height:200px;
margin-left:-100px;
margin-top:-100px;

where the negative margins are half of the width and height respectively

Karl Keefer
  • 645
  • 1
  • 5
  • 13
  • Looks like left:50% didn't take. The parent element that you want to center it inside of needs to have a position:relative declared, but if you're just centering relative to 'body' you don't need to do that. If you post it somewhere where I can see the code I can probably tell you what's up. http://jsfiddle.net/ – Karl Keefer Apr 28 '12 at 02:36
1

Hey Please use easy way

https://stackoverflow.com/a/10010055/1312610

http://jsfiddle.net/ERuX4/1/

Community
  • 1
  • 1
ShibinRagh
  • 6,530
  • 4
  • 35
  • 57