码迷,mamicode.com
首页 > 其他好文 > 详细

圣杯布局

时间:2016-03-19 00:44:44      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:

圣杯布局和双飞翼布局,他们的都要求三列布局,中间宽度自适应,两边定宽,这样做的优势是重要的东西放在文档流前面可以优先渲染,而双飞翼布局是对圣杯布局的一种改良,下一篇文章会讲到。

圣杯布局:用到浮动、负边距、相对定位,不添加额外标签

DOM结构:

技术分享
<div class="header">Header</div>
<div class="bd">
    <div class="main">Main</div>
    <div class="left">Left</div>
    <div class="right">Right
    </div>
</div>
<div class="footer">Footer</div>
技术分享

样式:

技术分享
    <style>
        body{padding:0;margin:0}
        .header,.footer{width:100%;  background: #666;height:30px;clear:both;}
        .bd{
            padding-left:150px;
            padding-right:190px;
        }
        .left{
            background: #E79F6D;
            width:150px;
            float:left;
            margin-left:-100%;
            position: relative;
            left:-150px;
        }
        .main{
            background: #D6D6D6;
            width:100%;
            float:left;

        }
        .right{
            background: #77BBDD;
            width:190px;
            float:left;
            margin-left:-190px;
            position:relative;
            right:-190px;
        }
    </style>
技术分享

 

左中右部分样式变化过程

1、中间部分需要根据浏览器宽度的变化而变化,所以要用100%,这里设左中右向左浮动,因为中间100%,左层和右层根本没有位置上去

技术分享
       .left{
            background: #E79F6D;
            width:150px;
            float:left;
        }
        .main{
            background: #D6D6D6;
            width:100%;
            float:left;

        }
        .right{
            background: #77BBDD;
            width:190px;
            float:left;
        }
技术分享

技术分享

2、把左层负margin150后,发现left上去了,因为负到出窗口没位置了,只能往上挪

 .left{
            background: #E79F6D;
            width:150px;
            float:left;
            margin-left:-150px;
        }

技术分享

3、那么按第二步这个方法,可以得出它只要挪动窗口宽度那么宽就能到最左边了,利用负边距,把左右栏定位

技术分享
        .left{
            background: #E79F6D;
            width:150px;
            float:left;
            margin-left:-100%;
        }

        .right{
            background: #77BBDD;
            width:190px;
            float:left;
            margin-left:-190px;
        }
技术分享

技术分享

 4、然而问题来了,中间被左右挡住了啊,只好给外层加padding了

       .bd{
            padding-left:150px;
            padding-right:190px;
        }

技术分享

5、但是加了之后左右栏也缩进来了,于是采用相对定位方法,各自相对于自己把自己挪出去,得到最终结果

技术分享
        .left{
            background: #E79F6D;
            width:150px;
            float:left;
            margin-left:-100%;
            position: relative;
            left:-150px;
        }
        .right{
            background: #77BBDD;
            width:190px;
            float:left;
            margin-left:-190px;
            position:relative;
            right:-190px;
        }
技术分享

技术分享

 

圣杯布局

标签:

原文地址:http://www.cnblogs.com/henanbuct/p/5294062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!