你对div模块居中布局的实现方法是否了解,这里和大家分享一下,相信本文介绍一定会让你有所收获。
不用float实现div模块居中布局
最常见的DIV+CSS网页布局形式:上、中左、中右、底四个模块,宽度760px,整体页面居中。
结构代码,topleftrightfoot四个模块全部独立、互不嵌套。
ExampleSourceCode
复制
<divid="top">head</div> <divid="left"> <divid="left_module">left</div> </div> <divid="right"> <divid="right_module">right</div> </div> <divid="foot">foot</div>
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
顶部属于常规定义。
复制
#top{height:100px;background:#ccc;width:760px;margin:auto; text-align:center;}
1.
2.
3.
◆方法A:
外层left定义为760px宽并居中;内层left_module定义为实际的左侧宽度280px,且绝对定位,top值等于顶部定义的高度。
这种方法的好处是:leftright两个模块代码片断可以互换调整显示优先级。
复制
#left{width:760px;margin:auto;} #left_module{width:280px;position:absolute;top:100px;padding:10px;}
1.
2.
3.
◆方法B:
外层left定义为760px宽并居中,相对浮动于top;内层left_module定义为实际的左侧宽度280px,且绝对定位。
这种方法的好处是:顶部的高度可以自由延伸。
复制
#left{width:760px;margin:auto;position:relative;} #left_module{width:280px;position:absolute;padding:10px;}
1.
2.
3.
外层right定义为760px宽并居中,内层right_module定义为实际的右侧宽度440px,使用margin语法居左。right_module定义的背景色是实际右侧的背景色,定义的高度就是实际中间模块的高度;right的背景色就是实际左侧的背景色。
复制
#right{width:760px;margin:auto;background:#E8E8E8;} #right_module{width:440px;background:#F0F0F0; height:360px;margin:000auto;padding:10px;}
1.
2.
3.
4.
◆底部也属于常规定义。
复制
#foot{height:100px;background:#ccc; width:760px;margin:auto;text-align:center;}
1.
2.
3.
测试环境IE6.0和FF1.5,都是最俗的语法,非常简单,实用有限,可做技术参考。
文章来源:Div-Css.net设计网参考:http://www.div-css.net/div_css/topic/index.asp?id=7123
【编辑推荐】