左侧固定宽度,右侧自适应宽度的CSS布局

一边固定宽度,另一边根据浏览器窗口大小自动缩放宽度是这种很常用的布局。特别是在BS项目的界面设计中,几乎都会用到。BI上有高手专门讨论了这种布局方法,但他用了较多的hack,还回避了IE6的dtd。我在实际使用中,发现回避掉IE6的dtd定义后,会导致ajax模态框无法居中(VS的一个控件,自动生成的代码,很难修改)。 于是自己写了个简单的左右两列的布局,没用到什么hack,很简单,只是练手用用。

css代码:left和right都贴住左侧。设置left在right上面(z-index);在right内加个放内容的层(content);设置content距离right的左侧为200px,即刚巧等于left的宽度。

  1. * {margin:0; padding:0; list-style:none; }
  2. .wrapper {width: 100%}
  3. .left {width:200px;background:#fcc; position:absolute; left:0 ;z-index:1  }
  4. .right {width:100%;background:#ccc; position:absolute;left:0}
  5.     .content {margin-left:200px;background:#ffc;   }



完整代码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title></title>
  6. <style type="text/css">
  7. * {margin:0; padding:0; list-style:none; }
  8. .wrapper {width: 100%;  }
  9. .left {width:200px; background:#fcc; position:absolute; left:0 ;z-index:1  }
  10. .right {width:100%; background:#ccc; position:absolute; left:0}
  11.     .content {margin-left:200px; background:#ffc;   }
  12.  
  13. </style>
  14. </head>
  15. <body>
  16. <div class="wrapper">
  17.     <div class="left">左侧固定宽度200px</div>
  18.     <div class="right">
  19.         <div class="content">
  20.             右侧宽度自动适应
  21.         </div>
  22.     </div>
  23. </div>
  24. </body>
  25. </html>
Tags: 08月 20th, 2008 Posted in CSS

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress | Blue Weed by Blog Oh! Blog | Entries (RSS) and Comments (RSS).