如何在页面的中心设置div [英] How to set the div in center of the page

查看:176
本文介绍了如何在页面的中心设置div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://jsfiddle.net/VjMy8/

请帮助我将#container设置为页面中心,以便#container下的所有其他内容都位于中心。

Please help me to set the #container to keep in the center of the page so that all other contents under the #container will be on center.

推荐答案

在css中:

#container {
    width: xxx em/px/%/...;
    margin-left: auto;
    margin-right: auto;
    /* or margin: 0 auto; */
}

使用此方法,您甚至不需要容器,做这样的代码下面的代码。

这段代码不需要一个float,所以它不会中断页面​​的流动

With this method you won't even need a container, you can do something like this the code below.
This code doesn't need a float, so it doesn't interrupt the flow of a page

#div_left {
    width: 25%;
    margin-left: 12%;
}
#div_right {
    width: 50%;
    margin-left: 38%;
}



此方法在每个浏览器中都很方便。更长)方法:

This method works in every browser in is much easyer then this (older & longer) method:

body {
    text-algin: center;
}
#container {
    width: xxx;
    text-align: center;
}
#div_left {
    width: 30%; /* 30% of the container-width */
}
#div_right {
    width: 70%;
}

这篇关于如何在页面的中心设置div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆