Adding notes to a naive link (to the w3c tips site): display:table-cell does not work with height:100% . So, if you want to vertically center an element that you donβt know its height on the page, you must place it inside the container with display:table and height:100% , and another container with display:table-cell and vertical-align:middle .
In addition, if you want to center it horizontally, you need to specify width:100% on the body, the outer container and give text-align:center inner container. The content should have display:inline-block and reset alignment, text-align:left .
Ultimately, it becomes:
<style> #vcontainer { display: table; height: 100%; width: 100%; } #hcontainer { display: table-cell; vertical-align: middle; text-align: center; } #content { display: inline-block; border: lightGray 1px solid; background-color: lightBlue; text-align: left; } </style> <body> <div id="vcontainer"><div id="hcontainer"> <div id="content"> Hoyjo!<br> I have returned to my hometown!<br> Lolololo lololo lololo lololo lololo!<br> </div> </div></div> </body>
I can not guarantee that it will work on older browsers (IE6 / 7). It will work on IE8 provided that it will work on IE8 standards (yes, it will overshadow your mind. Thanks, MS!), And you should give <html> and <body> height:100% .
syockit
source share