三栏布局

1、浮动布局(移动端是不推荐使用float)
思想:左边左浮动,右边右浮动,外层清浮动。中间利用margin边距控制距离。dom顺序是left right content (如果变成left content right 样式中要靠margin-top:-高度 处理)。另外dom顺序是left right content 元素高度不能用继承的100%

  <div class="box">
    <div class="left"></div>
    <div class="right"></div>
    <div class="content"></div>
  </div>
    .box{
      clear: both;
   }
    .left{
      float: left;
      width:300px;
      min-height: 300px;
      background:green;
    }
    .right{
      float:right;
      width:100px;
      min-height: 300px;
      background:blue;
      /* margin-top: -300px; */
    }  
    .content{
      background:gold;
      min-height: 300px;
      margin:0 100px 0 300px;
    }

2、定位布局
思想:外相对定位,左右绝对定位。中间margin控制。只样式不同,html中左中右的位置随意

    .box{
      position:relative;
    }
    .left{
      position:absolute;
      left:0;
      top:0;
      width:200px;
      background:orange;
      min-height: 300px;
    }
    .right{
      position:absolute;
      right:0;
      top:0;
      width:300px;
      background:purple;
      min-height: 300px;
    }  
    .content{
      margin:0 300px 0 200px;
      background:blue;
      min-height: 300px;
    }

3、flex布局
思想:盒子flex布局,左右定宽,中间flex:1

  <div class="box">
    <div class="left"></div>
    <div class="content"></div>
    <div class="right"></div>
  </div>
    .box{
      display: flex;
    }  
    .left{
      width:200px;
      background:orangered;
      min-height:400px;
    }
    .right{
      width:300px;
      background:gold;
      min-height:400px;
    }  
    .content{
      background:blue;
      min-height:400px;
      flex:1
    }

4、table table-cell布局
思想:外层width:100%,display:table;3个内层display:table-cell,左右若不设置宽度,等分,设置,中间是剩余宽度

<div class="box">
    <div class="left">
    </div>
    <div class="con">
    </div>
    <div class="right">
    </div>
</div>
.box{
    width:100%;
    display:table;  
    height:200px;  
}
.left,.con,.right{
    display:table-cell;
    height:200px;  
}
.left{
    width:200px;
    background:red;
    height:200px;  
}
.right{
    width:100px;
    background:pink;
    height:200px;  
}
.con{
    background:gold;
    height:200px;
}

5、calc + inline-block布局
思想:inline-block就会在一行上,calc 能计算剩余宽度,需要解决3px问题
left、right、con都是inline-block,con用calc计算宽度,外层用font-size:0或是内部元素代码无间隙

<div class="box">
    <div class="left">
    </div>
    <div class="con">
    </div>
    <div class="right">
    </div>
</div>
.box{
    font-size:0px;
}
.left,.con,.right{
    display:inline-block;
    height:200px;  
}
.left{
    width:200px;
    background:red;
    height:200px;  
}
.right{
    width:100px;
    background:pink;
    height:200px;  
}
.con{
    background:gold;
    width:calc( 100% - 300px);
    height:200px;
}

无间隙写法

<div class="box">
    <div class="left">
    </div><div class="con">
    </div><div class="right">
    </div>
</div>

6、grid布局

两栏伸缩布局方法同上

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容

  • https://zhuanlan.zhihu.com/p/25070186?refer=learncoding 三...
    逆风飘游的鱼阅读 1,039评论 0 0
  • 六种布局方式总结:圣杯布局、双飞翼布局、Flex布局、绝对定位布局、表格布局、网格布局。 圣杯布局 圣杯布局是指布...
    西麦smile阅读 8,536评论 3 8
  • 1. 负边距在让元素偏移时和position: relative有什么区别? 通过负边距进行偏移的元素,它会放弃原...
    Timmmmmmm阅读 433评论 0 0
  • 一、负边距在让元素产生偏移时和position: relative有什么区别? 负边距在让元素产生偏移的时候其自身...
    dengpan阅读 325评论 0 0
  • 有个朋友是个理想型的,有想法有激情爱折腾,他说想放弃现有工作去找一份天使投资的工作,虽没从事过类似工作,但凭借着内...
    一枚小懵逼阅读 310评论 0 1