I want to position a <div>
(or a <table>
) element at the center of the screen irrespective of screen size. In other words, the space left on 'top' and 'bottom' should be equal and space left on 'right' and 'left' sides should be equal. I would like to accomplish this with only CSS.
I have tried the following but it is not working:
<body>
<div style="top:0px; border:1px solid red;">
<table border="1" align="center">
<tr height="100%">
<td height="100%" width="100%" valign="middle" align="center">
We are launching soon!
</td>
</tr>
</table>
</div>
</body>
Note:
It is either way fine if the <div>
element (or <table>
) scrolls with the website or not. Just want it to be centered when the page loads.
The easy way, if you have a fixed width and height:
#divElement{
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
width: 100px;
height: 100px;
}​
Please don't use inline styles! Here is a working example http://jsfiddle.net/S5bKq/.