设为首页收藏本站
开启辅助访问

创星网络[分享知识 传递快乐]

 找回密码
 立即注册

QQ登录

只需一步,快速开始

用新浪微博登录

只需一步,快速搞定

搜索

不用float如何实现DIV模块居中布局

2012-8-8 10:47| 发布者: cryinglily| 查看: 655| 评论: 0|原作者: luinstein

摘要: 最常见的DIV+CSS网页布局形式:上、中左、中右、底四个模块,宽度760px,整体页面居中。 结构代码,topleftrightfoot四个模块全部独立、互不嵌套。 ExampleSourceCode div id=tophead/div div id=left div ...

  最常见的DIV+CSS网页布局形式:上、中左、中右、底四个模块,宽度760px,整体页面居中。
  结构代码,topleftrightfoot四个模块全部独立、互不嵌套。
  ExampleSourceCode
     
  1. <div id="top">head</div>         
  2. <div id="left">      
  3. <div id="left_module">left</div>   
  4. </div>         
  5. <div id="right">      
  6. <div id="right_module">right</div>      
  7. </div>         
  8. <div id="foot">foot</div>   
复制代码

  顶部属于常规定义。     

       
  1. #top{height:100px;background:#ccc;      
  2.      
  3. width:760px;margin:auto;text-align:center;}      
复制代码
   
  方法A:
  外层left定义为760px宽并居中;内层left_module定义为实际的左侧宽度280px,且绝对定位,top值等于顶部定义的高度。
这种方法的好处是:leftright两个模块代码片断可以互换调整显示优先级。        
  1. #left{width:760px;margin:auto;}      
  2.      
  3. #left_module{width:280px;position:absolute;      
  4.      
  5. top:100px;padding:10px;}      
  6.   
复制代码

  方法B:
  外层left定义为760px宽并居中,相对浮动于top;内层left_module定义为实际的左侧宽度280px,且绝对定位。
这种方法的好处是:顶部的高度可以自由延伸。
     
  1. #left{width:760px;margin:auto;position:relative;}      
  2.      
  3. #left_module{width:280px;position:absolute;padding:10px;}      
  4.    
复制代码

  外层right定义为760px宽并居中,内层right_module定义为实际的右侧宽度440px,使用margin语法居左。right_module定义的背景色是实际右侧的背景色,定义的高度就是实际中间模块的高度;right的背景色就是实际左侧的背景色。
     
  1. #right{width:760px;margin:auto;background:#E8E8E8;}      
  2.      
  3. #right_module{width:440px;background:#F0F0F0;      
  4.      
  5. height:360px;margin:000auto;padding:10px;}      
  6.    
复制代码

  底部也属于常规定义。        
  1. #foot{height:100px;background:#ccc;width:760px;      
  2.      
  3. margin:auto;text-align:center;}      
  4.   
复制代码

  
  测试环境IE6.0和FF1.5,都是最俗的语法,非常简单,实用有限,可做技术参考。
原文:http://developer.51cto.com/art/201009/224044.htm

鲜花

握手

雷人

路过

鸡蛋

相关分类

QQ|Archiver|手机版|小黑屋|创星网络 ( 苏ICP备11027519号|网站地图  

GMT+8, 2024-5-17 12:28 , Processed in 0.057472 second(s), 18 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

返回顶部