събота, 9 март 2013 г.

More about Centering Elements

Horizontal centering with CSS:
.className{
    margin:0 auto;
    width:200px;
    height:200px;
}
Horizontal and vertical centering with CSS:
className{
    width:300px;
    height:200px;
    position:absolute;
    left:50%;
    top:50%;
    margin:-100px 0 0 -150px;
}
The jQuery way:
$(window).resize(function(){
   $('.className').css({
      position:'absolute',
      left: ($(window).width() - $('.className').outerWidth())/2,
       top: ($(window).height() - $('.className').outerHeight())/2
      });
});  
// To initially run the function
$(window).resize();

Няма коментари:

Публикуване на коментар