I need to center vertically and horizontally a div called ".box"
inside <body></body>
. It contains a <h1>
, and that's all. In the future, it may contents an <img>
.
I tried a lot of tricks to do this, but unfortunately it doesn't work well...
So I found on this topic : Centering a div vertically & horizontally using jQuery a solution with jQuery. But it's not working perfectly, because the div stays on the left of the screen as you can see here : http://www.arcadmedia.com/
So I'm looking for a solution (in jQuery, or in CSS but nothing was working for me).
I think solutions like this one : http://jsfiddle.net/pqDQB/ are not the best, and jQ can do better.
I'm just using this code :
$(function() {
$('.box').css({
'position' : 'absolute',
'left' : '50%',
'top' : '50%',
'margin-left' : -$('.box').outerWidth()/2,
'margin-top' : -$('.box').outerHeight()/2
});
});
I really don't know from where is this problem.
EDIT : The box has to fit its content dynamically!