1. Achieve horizontal centering through css:
.className{
margin:0 auto;
width:200px;
height:200px;
}
2. Horizontal centering through css and vertical centering
Creating a horizontally centered and vertically centered div through css is a troublesome thing. You must know the size of the other div in advance:
.className{
width:300px;
height:200px;
position:absolute;
left:50%;
top:50%;
margin:-100px 0 0 -150px;
}
3. Leveling through jQuery Centering and vertical centering have been mentioned before. The css method only applies to divs with fixed sizes, so jQuery comes into play:
$(window).resize(function(){
$('.className').css({
position :'absolute',
left: ($(window).width() - $('.className').outerWidth())/2,
top: ($(window).height() - $('.className').outerHeight())/2 $(document).scrollTop()
});
});
//Initialization function
$(window).resize ();
The advantage of this method is that you don't need to know how big the div is, the disadvantage is that it can only be achieved through JavaScript.