css实现三栏水平布局双飞翼与圣杯布局

做为布局的入门级选手,网上也查看了不少信息和资源html

双飞翼的html结构布局

<div class="container"> 
  <div class="main">
      <div class="content">main</div> 
    </div>
  <div class="left">left</div> 
  <div class="right">right</div> 
</div>

 

双飞翼和圣杯的布局方式相似,都用到了关键的浮动和负值的margin-leftspa

第一步:先正常设置长宽,同一高度为120px,因为是标准流的缘故,因此三个div盒子一共分为三层.net

    .left, .right, .main {
        height: 120px;
    }
    .content {
        margin: 0 300px 0 200px;
    }
    .left {
        width: 200px;
        background-color: green;
    }
    .right {
        width: 300px;
        background-color: red;
    }
    .main {
        width: 100%;
        background-color: blue;
    }

效果以下3d

 

第二步:添加浮动,所有脱离标准流,由于main是的宽度是100%,占满整个盒子的宽度,因此left和right的盒子被挤到第二行code

    .left, .right, .main {
        height: 120px;
        float: left;
    }
    .content {
        margin: 0 300px 0 200px;
    }
    .left {
        width: 200px;
        background-color: green;
    }
    .right {
        width: 300px;
        background-color: red;
    }
    .main {
        width: 100%;
        background-color: blue;
    }

效果以下htm

第三步:使用margin-left的负值属性来实现这个效果blog

这个margin-left:-100%指的是子盒子的左边框相对父盒子的右边框的距离资源

    .left, .right, .main {
        height: 120px;
        float: left;
        text-align: center;
    }
    .content {
        margin: 0 300px 0 200px;
    }
    .left {
        width: 200px;
        background-color: green;
        margin-left: -100%;
    }
    .right {
        width: 300px;
        background-color: red;
        margin-left: -300px; 
    }
    .main {
        width: 100%;
        background-color: blue;
    }

相对第二步,就多了margin-left的属性博客

先看right盒子,相对父盒子container,他的左边框必须距离container的右边框300px,因此是margin-left: -300px; 

left同样,left盒子的左边框必须距离父盒子的右边框的100%个宽度,因此是margin-left: -100%;

效果以下

圣杯的布局相似双飞翼

多了定位,div相对少了一个

    <div class="container">
        <div class="middle">middle</div>
        <div class="left">left</div>
        <div class="right">right</div>    
    </div>
        .container {
            padding: 0 300px 0 200px;
        }
        .left,.right,.middle {
            height: 120px;
            float: left;
            position: relative;
            text-align: center;
        }
        .middle {
            width: 100%;
            background-color: blue;
        }
        .left {
            left: -200px;
            width: 200px;
            margin-left: -100%;
            background-color: green;
        }
        .right {
            right: -300px;
            width: 300px;
            margin-left: -300px;
            background-color: red;
        }

关于圣杯布局能够参考博客

https://blog.csdn.net/wangchengiii/article/details/77926868

相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息