中心对齐网页

时间:2011-06-25 13:23:43

标签: html css

如何将网页的整个body内容对齐?

我尝试通过text-align:center进行对齐,但失败了!

2 个答案:

答案 0 :(得分:5)

width:900px;
margin:auto;

确保它没有漂浮。

答案 1 :(得分: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/

相关问题