Here is a better approach: (live example) . It is supported in all modern browsers. Link
Set the html / body elements to height:100% and width:100% .
Then set the mapping of the body or parent to table .
Finally, use display:table-cell and vertical-align:middle for the child.
This will take care of the vertical alignment.
To center horizontally, set margin:0px auto to the child.
In some cases, when the width of the child is not defined or dynamically generated, you can also use text-align:center , assuming that it is an inline element.
HTML
<div id="parent"> <div id="child"></div> </div>
CSS
html, body { height:100%; width:100%; margin:0px; } body { display:table; } #parent { display:table-cell; vertical-align:middle; background:#123456; } #child { height:50%; width:50%; background:white; margin:0px auto; }
source share