2011-06-25 82 views

回答

5
width:900px; 
margin:auto; 

確保它沒有浮動。

+0

樂施會..我缺少'margin:auto'。謝謝我的問題已經解決了。但我不明白爲什麼沒有設置這個網頁居中。 –

+0

因爲默認值是'margin:0'。 :-) – Artfunkel

1

CSS:

div#body { 
    width:902px; //Or whatever your content width is 
    margin:0 auto; 
    padding:0; 
} 

如何網站看起來像:

<html> 
    <body> 
     <div id"body"> 
      ...The rest of your content... 
     </div> 
    </body> 
</html> 

您可以使用jQuery也:

$(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(); 

也爲您的教程:http://tutorialzine.com/2010/03/centering-div-vertically-and-horizontally/