前面的话

  前面已经介绍过单列定宽单列自适应两列自适应的两列布局。本文介绍三列布局,分为两侧定宽中间自适应、两列定宽一侧自适应、中间定宽两侧自适应、一侧定宽两列自适应和三列自适应这五种情况

 

两侧定宽中间自适应

思路一:  float

【1】float + margin + calc

复制代码
<style>
p{margin: 0;}
.parent{overflow: hidden;}
.left,.right{float: left;width: 100px;}
.center{float: left; width:calc(100% - 240px);margin: 0 20px;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>    
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
    </div>        
</div>
复制代码

【2】float + margin + (fix)

复制代码
<style>
p{margin: 0;}
.parent{overflow: hidden;}
.left,.right{position: relative;float: left;width: 100px;}
.centerWrap{float: left; width:100%; margin: 0 -100px;}
.center{margin: 0 120px;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>            
    <div class="centerWrap" style="background-color: red;">
        <div class="center" style="background-color: pink;">
            <p>center</p>
            <p>center</p>
        </div>        
    </div>
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
    </div>            
</div>
复制代码

思路二: inline-block

【1】inline-block + margin + calc

复制代码
<style>
p{margin: 0;}
.parent{font-size: 0;}
.left,.right,.center{display:inline-block; vertical-align: top;font-size: 16px;}
.left,.right{width: 100px;}
.center{width: calc(100% - 240px); margin: 0 20px;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>            
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>        
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
    </div>            
</div>
复制代码

【2】inline-block + margin + (fix)

复制代码
<style>
p{margin: 0;}
.parent{font-size: 0;}
.left,.right,.centerWrap{display:inline-block; vertical-align: top;font-size: 16px;}
.left,.right{width: 100px;position:relative;}
.centerWrap{width: 100%; margin: 0 -100px;}
.center{margin: 0 120px;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="centerWrap" style="background-color: orange;">
        <div class="center" style="background-color: pink;">
            <p>center</p>
            <p>center</p>
        </div>        
    </div>        
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
    </div>            
</div>
复制代码

思路三: table

复制代码
<style>
p{margin: 0;}
.parent{display: table; width: 100%;table-layout: fixed;}
.left,.right,.centerWrap{display:table-cell;}
.left,.right{width: 100px;}
.center{margin: 0 20px;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="centerWrap" style="background-color: orange;">
        <div class="center" style="background-color: pink;">
            <p>center</p>
            <p>center</p>
        </div>        
    </div>        
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
    </div>            
</div>
复制代码

思路四: absolute

复制代码
<style>
p{margin: 0;}
.parent{position: relative;height:40px;}
.left,.right,.center{position: absolute;}
.left{left: 0;width:100px;}
.right{right: 0;width: 100px;}
.center{left: 120px; right: 120px;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>                
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
    </div>            
</div>
复制代码

思路五: flex

复制代码
<style>
p{margin: 0;}
.parent{display: flex;}
.left,.right{width: 100px;}
.center{flex: 1; margin: 0 20px;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>                
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
    </div>            
</div>
复制代码

复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>                
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
    </div>            
</div>
复制代码

思路六: grid

<style>
p{margin: 0;}
.parent{display: grid;grid-template-columns:100px 1fr 100px; grid-gap:20px;}
</style>

 

两列定宽一侧自适应

  这种布局与单列定宽单列自适应布局非常相似

思路一: float

【1】float + margin

  缺点是IE6-浏览器的3px的bug,以及当自适应列中有元素清除浮动时,会使该元素不与左侧浮动元素同行,从而出现文字下沉现象

复制代码
<style>
p{margin: 0;}
.parent{overflow: hidden;}
.left,.center{float: left;width: 100px;margin-right: 20px;}
.right{margin-left: 240px;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
    </div>                
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>            
</div>
复制代码

【2】float + margin + calc

复制代码
<style>
p{margin: 0;}
.parent{overflow: hidden;}
.left,.center{float: left;width: 100px;margin-right: 20px;}
.right{float: left; width: calc(100% - 240px);}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
    </div>                
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>            
</div>
复制代码

【3】float + margin + (fix)

复制代码
<style>
p{margin: 0;}
.parent{overflow: hidden;}
.left,.center{position: relative; float: left;width: 100px;margin-right: 20px;}
.rightWrap{float: left; width: 100%; margin-left: -240px;}
.right{margin-left:240px;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
    </div>        
    <div class="rightWrap">
        <div class="right"  style="background-color: lightgreen;">
            <p>right</p>
            <p>right</p>
        </div>            
    </div>            
</div>
复制代码

 【4】float + overflow

复制代码
<style>
p{margin: 0;}
.parent{overflow: hidden;}
.left,.center{position: relative; float: left;width: 100px;margin-right: 20px;}
.right{overflow: hidden;zoom:1;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
    </div>        
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>                        
</div>
复制代码

思路二: inline-block

【1】inline-block + margin + calc

复制代码
<style>
p{margin: 0;}
.parent{font-size: 0;}
.left,.right,.center{display:inline-block; vertical-align: top;font-size: 16px;}
.left,.center{width: 100px;margin-right:20px;}
.right{width: calc(100% - 240px);}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
    </div>        
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>                        
</div>
复制代码

【2】inline-block + margin + (fix)

复制代码
<style>
p{margin: 0;}
.parent{font-size: 0;}
.left,.rightWrap,.center{display:inline-block; vertical-align: top;font-size: 16px;}
.left,.center{position: relative;width: 100px;margin-right:20px;}
.rightWrap{width:100%; margin-left: -240px;}
.right{margin-left: 240px;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
    </div>        
    <div class="rightWrap" style="background-color: green;">
        <div class="right"  style="background-color: lightgreen;">
            <p>right</p>
            <p>right</p>
        </div>        
    </div>            
</div>
复制代码

思路三: table

复制代码
<style>
p{margin: 0;}
.parent{display: table; width: 100%;table-layout: fixed;}
.leftWrap,.centerWrap,.right{display:table-cell;}
.leftWrap,.centerWrap{width: 120px;}
.left,.center{margin-right: 20px;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="leftWrap">
        <div class="left" style="background-color: lightblue;">
            <p>left</p>
        </div>        
    </div>
    <div class="centerWrap">
        <div class="center" style="background-color: pink;">
            <p>center</p>
        </div>        
    </div>
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>            
</div>
复制代码

思路四: absolute

复制代码
<style>
p{margin: 0;}
.parent{position: relative;width:100%;height:40px;}
.left{position: absolute;left:0;width:100px;}
.center{position: absolute;left:120px;width: 100px;}
.right{position: absolute;left:240px; right:0;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>        
    <div class="center" style="background-color: pink;">
        <p>center</p>
    </div>        
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>            
</div>
复制代码

思路五: flex

复制代码
<style>
p{margin: 0;}
.parent{display: flex;}
.left,.center{width:100px;margin-right:20px;}
.right{flex:1;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>        
    <div class="center" style="background-color: pink;">
        <p>center</p>
    </div>        
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>            
</div>
复制代码

思路六: grid

<style>
p{margin: 0;}
.parent{display: grid;grid-template-columns:100px 100px 1fr; grid-gap:20px;}
</style>
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>                
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
    </div>            
</div>
复制代码

 

中间定宽两侧自适应布局

思路一: float

复制代码
<style>
p{margin: 0;}
.parent{overflow: hidden;}
.left{float: left;margin-right: 20px;}
.center{float: left;width: 100px;margin-right: 20px;}
.right{overflow: hidden;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
        <p>left</p>
    </div>        
    <div class="center" style="background-color: pink;">
        <p>center</p>
    </div>    
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>                    
</div>
复制代码

思路二: table

复制代码
<style>
p{margin: 0;}
.parent{display: table; width: 100%;}
.leftWrap{display: table-cell; width: 0.1%;}
.centerWrap{display: table-cell;width: 120px;}
.left,.center{margin-right: 20px;}
.right{display:table-cell;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="leftWrap">
        <div class="left" style="background-color: lightblue;">
            <p>left</p>
            <p>left</p>
        </div>            
    </div>
    <div class="centerWrap">
        <div class="center" style="background-color: pink;">
            <p>center</p>
        </div>            
    </div>
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>                    
</div>
复制代码

思路三: flex

复制代码
<style>
p{margin: 0;}
.parent{display: flex;}
.left{margin-right: 20px;}
.center{width: 100px;margin-right: 20px;}
.right{flex: 1;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
        <p>left</p>
    </div>            
    <div class="center" style="background-color: pink;">
        <p>center</p>
    </div>            
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>                    
</div>
复制代码

思路四: grid

<style>
p{margin: 0;}
.parent{display: grid;grid-template-columns:auto 100px 1fr; grid-gap:20px;}
</style>
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>                
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
    </div>            
</div>
复制代码

 

一侧定宽两列自适应

思路一: float

复制代码
<style>
p{margin: 0;}
.parent{overflow: hidden;}
.left{float: left;width: 100px;margin-right: 20px;}
.center{float: left;margin-right: 20px;}
.right{overflow: hidden;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>            
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>            
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>                    
</div>
复制代码

思路二: table

复制代码
<style>
p{margin: 0;}
.parent{display: table; width: 100%;}
.leftWrap{display: table-cell; width: 120px;}
.centerWrap{display: table-cell;width: 0.1%;}
.left,.center{margin-right: 20px;}
.right{display:table-cell;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="leftWrap">
        <div class="left" style="background-color: lightblue;">
            <p>left</p>
        </div>            
    </div>
    <div class="centerWrap">
        <div class="center" style="background-color: pink;">
            <p>center</p>
            <p>center</p>
        </div>            
    </div>
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>                    
</div>
复制代码

思路三: flex

复制代码
<style>
p{margin: 0;}
.parent{display: flex;}
.left{width: 100px;margin-right: 20px;}
.center{margin-right: 20px;}
.right{flex: 1;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>            
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>            
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>                    
</div>
复制代码

思路四: grid

<style>
p{margin: 0;}
.parent{display: grid;grid-template-columns:100px auto 1fr; grid-gap:20px;}
</style>
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>                
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
    </div>            
</div>
复制代码

 

三列自适应

思路一: float

复制代码
<style>
p{margin: 0;}
.parent{overflow: hidden;}
.left,.center{float: left;margin-right: 20px;}
.right{overflow: hidden;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
        <p>left</p>
    </div>            
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>            
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>                    
</div>
复制代码

思路二: table

复制代码
<style>
p{margin: 0;}
.parent{display: table; width: 100%;}
.leftWrap,.centerWrap{display: table-cell;width: 0.1%;}
.left,.center{margin-right: 20px;}
.right{display:table-cell;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="leftWrap">
        <div class="left" style="background-color: lightblue;">
            <p>left</p>
            <p>left</p>
        </div>            
    </div>
    <div class="centerWrap">
        <div class="center" style="background-color: pink;">
            <p>center</p>
            <p>center</p>
        </div>            
    </div>
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>                    
</div>
复制代码

思路三: flex

复制代码
<style>
p{margin: 0;}
.parent{display: flex;}
.left,.center{margin-right: 20px;}
.right{flex: 1;}
</style>
复制代码
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
        <p>left</p>
    </div>            
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>            
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>                    
</div>
复制代码

思路四: grid

<style>
p{margin: 0;}
.parent{display: grid;grid-template-columns:auto auto 1fr; grid-gap:20px;}
</style>
复制代码
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>    
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>                
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
    </div>            
</div>
复制代码

 

总结

  三列布局类似于大号的两列布局。无论是什么布局方式,无外乎需要应用floatinline-blocktableabsoluteflexgrid这6种布局属性,然后再配合负margin、calc()函数、bfc、增加结构等来实现布局

  自适应包括两种情况:一种是宽度由内容撑开,一种是宽度自动撑满父元素剩余宽度

  可实现宽度由内容撑开的属性有: float、inline、inline-block、table、table-cell、absolute、fixed、flex、grid

  可实现宽度自动撑满父元素剩余宽度的属性有: overflow(配合float)、table、flex、grid

 

转载: http://www.cnblogs.com/xiaohuochai/p/5455905.html

 

posted on 2018-02-11 20:06  云雀sunshine  阅读(246)  评论(0编辑  收藏  举报