左侧固定宽度,右侧自适应宽度的CSS布局
一边固定宽度,另一边根据浏览器窗口大小自动缩放宽度是这种很常用的布局。特别是在BS项目的界面设计中,几乎都会用到。BI上有高手专门讨论了这种布局方法,但他用了较多的hack,还回避了IE6的dtd。我在实际使用中,发现回避掉IE6的dtd定义后,会导致ajax模态框无法居中(VS的一个控件,自动生成的代码,很难修改)。 于是自己写了个简单的左右两列的布局,没用到什么hack,很简单,只是练手用用。
css代码:left和right都贴住左侧。设置left在right上面(z-index);在right内加个放内容的层(content);设置content距离right的左侧为200px,即刚巧等于left的宽度。
- * {margin:0; padding:0; list-style:none; }
- .wrapper {width: 100%; }
- .left {width:200px;background:#fcc; position:absolute; left:0 ;z-index:1 }
- .right {width:100%;background:#ccc; position:absolute;left:0}
- .content {margin-left:200px;background:#ffc; }
完整代码
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title></title>
- <style type="text/css">
- * {margin:0; padding:0; list-style:none; }
- .wrapper {width: 100%; }
- .left {width:200px; background:#fcc; position:absolute; left:0 ;z-index:1 }
- .right {width:100%; background:#ccc; position:absolute; left:0}
- .content {margin-left:200px; background:#ffc; }
- </style>
- </head>
- <body>
- <div class="wrapper">
- <div class="left">左侧固定宽度200px</div>
- <div class="right">
- <div class="content">
- 右侧宽度自动适应
- </div>
- </div>
- </div>
- </body>
- </html>