总结一下,DIV+CSS的布局,居中布局的几个方案。
方案1:
- #div
- {
- width:450px;
- height:600px;
- background-color:#FF0000;
- position:absolute;
- left:50%;
- margin-left:-225px;
- }
这样,该容器的左边距将从页面的50%的宽度开始算起;
然后,将容器DIV的左外边距值设置为负的容器宽度的一半。这样即可将该容器固定在页面水平方向的中点。
--------------------------------------------------------------------
方案2:
- #div
- {
-
- width:450px;
- height:600px;
- background-color:#FF0000;
- text-align:center;
- }
这个的缺点是,DIV容器中的所有子元素都会居中。
--------------------------------------------------------------------
方案3:
- #div
- {
- width:450px;
- height:600px;
- background-color:#FF0000;
- margin:0 auto;
- }
这个方案用的比较多,但是不支持IE6以下的浏览器。
--------------------------------------------------------------------
方案4:
- body
- {
- text-align:center;
- }
- #div
- {
- width:450px;
- height:600px;
- background-color:#FF0000;
- margin:0 auto;
- text-align:left;
- }
这个方案是方案2和方案3的完美结合,最大的兼容所有的浏览器。