CSS左侧固定宽 右侧自适应(兼容所有浏览器)

本文介绍了两种实现网页中固定宽度侧边栏与自适应主要内容区域布局的方法:通过设置左侧栏浮动及主要内容区域的左外边距;利用负的右外边距使左侧栏覆盖主要内容区域。
方法一、浮动布局

[html]  view plain  copy
  1. HTML   
  2. <div id="left">Left sidebar</div>  
  3.   
  4. <div id="content">Main Content</div>  
  5.   
  6. CSS Code  
  7. <style type="text/css">  
  8.   
  9. *{margin: 0;padding: 0;}  
  10. #left {  
  11. float: left;  
  12. width: 220px;  
  13. background-color: green;  
  14. }  
  15. #content {  
  16. background-color: orange;  
  17. margin-left: 220px;/*==等于左边栏宽度==*/  
  18. }  
  19. </style>  
[html]  view plain  copy
  1. <span style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; line-height: 18px;">上面这种实现方法最关键之处就是自适应宽度一栏“div#content”的“margin-left”值要等于固定宽度一栏的宽度值</span>  
方法二、

[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>Document</title>  
  6.     <style type="text/css">  
  7.         *{margin:0 auto;padding:0 auto;}  
  8.         #left {  
  9.             background-color: green;  
  10.             float: left;  
  11.             width: 220px;  
  12.             margin-right: -100%;  
  13.             /*min-height: 300px;*/  
  14.         }  
  15.         #content {  
  16.             float: left;  
  17.             width: 100%;  
  18.         }  
  19.         #contentInner {  
  20.             margin-left: 220px;/*==等于左边栏宽度值==*/  
  21.             background-color: orange;  
  22.             height: 400px;content高度不能自己变化,随着contentInner的高度变化而变化  
  23.         }  
  24.     </style>  
  25. </head>  
  26. <body>  
  27.     <div id="left">  
  28.         Left Sidebar  
  29.     </div>  
  30.     <div id="content">  
  31.         <div id="contentInner">  
  32.             Main Content  
  33.         </div>  
  34.     </div>  
  35. </body>  
  36. </html>  
方法三……

详情见:http://www.cnblogs.com/dearxinli/p/3799094.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值