How is it possible to create a true center CSS div cross browser for example to use within a holding page?
I have tried this 2007 css trick - How To Center an Object Exactly In The Center but as you can see from my JSFiddle of the code its not 100% center.
HTML:
<div class="center">
<p>Text</p>
</div>
CSS:
.center{
position: fixed;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -100px;
border:5px solid black;
}
You can do it with pure CSS:
It is important to give concrete (eg:
300px
) and not derived (like:auto
or30%
) values forwidth
andheight
.That's my solution:
It works in a lot of browsers. And there is no problem with content added after layout.
Have a look at this; quite a clever article.
That technique requires the element to have a fixed width and height. You are not setting the width and height. Based on your border and margins, to center it, the width would need to be 190 pixels and the height would need to be 90 pixels. Using
line-height
andtext-align
in combination with a fixed width and height makes the text and border centered. Try it.CSS
for example
This is an extra example with a height setter I created for IE vertical alignment. The extra span has a vertical-align:middle.