
参考文档:
寻找圣杯:In Search of the Holy Grail
W3CN:http://www.w3cn.org/article/layout/2004/55.html
我把重要的类容拿出来说说。
这种三栏布局的目标是:
- 允许中间的内容先于其他出现在代码中
- 一个自适应适应的中心和固定宽度的侧边栏
- 允许任何一栏都是最高的高度
- 只需要额外的一个DIV标签
- 非常的简单的CSS代码和很少很少的HACK 补丁
传统方式:利用float 方式实现:
样式:
- .wrap{overflow:hidden;}
- .left{width:250px;float:left;background:#999;}
- .right{width:250px;float:right;background:#999;}
- .center{background:#eee; +float:left;/* ie6 hack*/ overflow:hidden;/* 删除此,ff下不能高度自适应 */}
- .row{margin-bottom:-10000px;padding-bottom:10000px;}
- .header, .footer{width:100%;background:#ccc;}
xhtml:
- <div class="header">Top</div>
- <div class="wrap">
- <div class="left row">left</div>
- <div class="right row">
- right<br />
- right<br />
- </div>
- <div class="center row">Center<br />
- Center<br />
- Center<br />
- Center<br />
- Center0<br />
- <span style="float:right; font-size:0; "> </span> <!-- for ie -->
- </div>
- <div style="clear:both;"> </div>
- </div>
- <div class="footer">bottom</div>
使用float方式的麻烦显而易见,比如会带来很多HACK 补丁,XHTML里面也需要一些无意义标签辅助等。
以下是所谓“圣杯”,基本原理就是:利用外容器设padding,left 和right 设为相对定位,保证中间列自适应。
这里有个例子
- <!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" xml:lang="en" lang="en">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>The Holy Grail of Layouts: Example #3: A List Apart</title>
- <style type="text/css">
- /*** The Essential Code ***/
- body {
- min-width: 630px; /* 2 x (LC fullwidth + CC padding) + RC fullwidth */
- }
- #container {
- padding-left: 200px; /* LC fullwidth */
- padding-right: 190px; /* RC fullwidth + CC padding */
- }
- #container .column {
- position: relative;
- float: left;
- }
- #center {
- padding: 10px 20px; /* CC padding */
- width: 100%;
- }
- #left {
- width: 180px; /* LC width */
- padding: 0 10px; /* LC padding */
- right: 240px; /* LC fullwidth + CC padding */
- margin-left: -100%;
- }
- #right {
- width: 130px; /* RC width */
- padding: 0 10px; /* RC padding */
- margin-right: -100%;
- }
- #footer {
- clear: both;
- }
- /*** IE Fix ***/
- * html #left {
- left: 150px; /* RC fullwidth */
- }
- /*** Equal-height Columns ***/
- #container {
- overflow: hidden;
- }
- #container .column {
- padding-bottom: 1001em; /* X + padding-bottom */
- margin-bottom: -1000em; /* X */
- }
- /*** Footer Fix ***/
- * html body {
- overflow: hidden;
- }
- * html #footer-wrapper {
- float: left;
- position: relative;
- width: 100%;
- padding-bottom: 10010px;
- margin-bottom: -10000px;
- background: #FFF; /*** Same as body background ***/
- }
- /*** Just for Looks ***/
- body {
- margin: 0;
- padding: 0;
- background: #FFF;
- }
- #header, #footer {
- font-size: large;
- text-align: center;
- padding: 0.3em 0;
- background: #999;
- }
- #left {
- background: #66F;
- }
- #center {
- background: #DDD;
- }
- #right {
- background: #F66;
- }
- #container .column {
- padding-top: 1em;
- text-align: justify;
- }
- </style>
- </head>
- <body>
- <div id="header">This is the header.</div>
- <div id="container">
- <div id="center" class="column">
- <h1>This is the main content.</h1>
- </div>
- <div id="left" class="column">
- <h2>This is the left sidebar.</h2>
- </div>
- <div id="right" class="column">
- <h2>This is the right sidebar.</h2>
- </div>
- </div>
- <div id="footer-wrapper">
- <div id="footer">This is the footer.</div>
- </div>
- </body>
- </html>
点击查看完整示例:layouttest.html
