95992828九五至尊2

两列布局中单列定宽单列自适应布局的6种思路,CSS全屏布局的6种办法

三月 25th, 2019  |  882828九五至尊手机版

前面包车型地铁话

  全屏布局在实质上工作中是很常用的,比如管理类别、监察和控制平台等。本文将介绍有关全屏布局的6种思路

 

面前的话

  说起自适应布局格局,单列定宽单列自适应布局是最中央的布局情势。本文将从float、inline-block、table、absolute、flex和grid那七种思路来详细表明什么巧妙地促成布局

 

float

【1】float + calc

  通过calc()函数计算出.middle成分的中度,并设置子成分中度为百分百

<style>
body,p{margin: 0;}
body,html,.parent{height: 100%;}
.middle{
    overflow: hidden;
    height: calc(100% - 100px);
}
.left{
    float: left;
    width: 100px;
    margin-right: 20px;
    height: 100%;
}
.right{
    overflow: auto;
    height: 100%;
}
.right-in{
    height: 1000px;
}
.top,.bottom{height:50px;}
</style>

<div class="parent" id="parent" style="background-color: lightgrey;">
    <div class="top" style="background-color: lightblue;">
        <p>top</p>
    </div>  
    <div class="middle" style="background-color: pink;">
        <div class="left" style="background-color: orange;">
            <p>left</p>
        </div>
        <div class="right" style="background-color: lightsalmon;">
            <div class="right-in">
                <p>right</p>
            </div>
        </div>
    </div>          
    <div class="bottom" style="background-color: lightgreen;">
        <p>bottom</p>
    </div>        
</div>

【2】float + absolute + (fix)

  通过增添结构来拉长兼容性,.middle成分设置百分之百的万丈,.top和.bottom设置absolute覆盖在.middle上

<style>
body,p{margin: 0;}
body,html,.parent{height: 100%;}
.top,.bottom{
    position: absolute;
    height:50px;
    left: 0;
    right: 0;
}
.top{top: 0;}
.bottom{bottom: 0;}
.middleWrap{
    height: 100%;
    overflow: hidden;
}
.middle{
    overflow: hidden;
    height: 100%;
    margin: 50px 0;
}
.left{
    float: left;
    width: 100px;
    margin-right: 20px;
    height: 100%;
}
.right{
    overflow: auto;
    height: 100%;
}
.right-in{
    height: 1000px;
}
</style>

<div class="parent" id="parent" style="background-color: lightgrey;">
    <div class="top" style="background-color: lightblue;">
        <p>top</p>
    </div>  
    <div class="middleWrap">
        <div class="middle" style="background-color: pink;">
            <div class="left" style="background-color: orange;">
                <p>left</p>
            </div>
            <div class="right" style="background-color: lightsalmon;">
                <div class="right-in">
                    <p>right</p>
                </div>
            </div>
        </div>        
    </div> 
    <div class="bottom" style="background-color: lightgreen;">
        <p>bottom</p>
    </div>        
</div>

 

float

【思路一】float

  说起两列布局,最普遍的就是利用float来兑现。float浮动布局的老毛病是浮动后会造成文本环绕等功效,以及供给立即铲除浮动。即使各浮动成分的万丈不等时,恐怕会出犬牙相错的效益

【1】float + margin

  将定宽的一列使用float,而自适应的一列使用计算后的margin

<style>
p{margin: 0;}
.parent{overflow: hidden;zoom: 1;}
.left{float: left;width: 100px;}    
.right{margin-left: 120px;}
</style>

<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>
</div>

  [缺点1]IE6-浏览器下3像素bug,具身体表面以后右手首行文字会向右偏移3px。消除办法是在left成分上安装margin-right:
-100px

882828九五至尊手机版 1

  [缺点2]当动手容器中有成分消除浮动时,会使该因素不与左侧浮动成分同行,从而现身文字下沉现象

882828九五至尊手机版 2

【2】float + margin + (fix)

  (fix)代表增添结构,为了缓解上述办法中的三个毛病,能够经过增添结构来完成。自适应的一列外侧扩张一层结构.rightWrap并设置浮动。要兑现自适应效果,.rightWrap宽度必须安装为百分百。若不安装,float后的要素宽度将由内容撑开。同时再同盟盒模型质量的一个钱打二拾九个结,设置总结后的margin值,使两列成分在同一行呈现。同时两列之间的间隔由.right的margin值显明。由于左边成分会层叠在左手成分之上,.left必要选择relative来升高层级

<style>
p{margin: 0;}
.parent{overflow: hidden;zoom: 1;}
.left{position: relative;float: left;width: 100px;}    
.rightWrap{float: left;width: 100%;margin-left: -100px;}
.right{margin-left: 120px;}
</style>

<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>
    <div class="rightWrap" style="background-color: pink;">
        <div class="right"  style="background-color: lightgreen;">
            <p>right</p>
            <p>right</p>
        </div>        
    </div>
</div>

【3】float + margin + calc

  除了扩展结构的主意外,还能运用calc()

  [注意]IE8-、android4.3-、IOS5.1-不接济,android4.4+只辅助加减运算

<style>
p{margin: 0;}
.parent{overflow: hidden;zoom: 1;}
.left{float: left;width: 100px;margin-right: 20px;}    
.right{float: left;width: calc(100% - 120px);}
</style>

<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>
</div>

【4】float + overflow

  还足以应用overflow属性来触发bfc,来阻止浮动造成的文字环绕效果。由于接纳overflow不会改变成分的大幅度属性,所以不需求再一次设置宽度。由于设置overflow:hidden并不会触发IE6-浏览器的haslayout属性,所以供给设置zoom:1来包容IE6-浏览器

<style>
p{margin: 0;}
.parent{overflow: hidden;zoom: 1;}
.left{ 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="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>
</div>

 

inline-block

【1】inline-block + calc

<style>
body,p{margin: 0;}
body,html,.parent{height: 100%;}
.middle{
    height: calc(100% - 100px);
    font-size: 0;
}
.left,.right{
    display: inline-block;
    vertical-align: top;
    font-size: 16px;
}
.left{
    width: 100px;
    margin-right: 20px;
    height: 100%;
}
.right{
    width: calc(100% - 120px);
    height: 100%;
    overflow: auto;
}
.right-in{
    height: 1000px;
}
.top,.bottom{height: 50px;}
</style>

<div class="parent" id="parent" style="background-color: lightgrey;">
    <div class="top" style="background-color: lightblue;">
        <p>top</p>
    </div>  
    <div class="middle" style="background-color: pink;">
        <div class="left" style="background-color: orange;">
            <p>left</p>
        </div>
        <div class="right" style="background-color: lightsalmon;">
            <div class="right-in">
                <p>right</p>
            </div>
        </div>
    </div>        
    <div class="bottom" style="background-color: lightgreen;">
        <p>bottom</p>
    </div>        
</div>

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

<style>
body,p{margin: 0;}
body,html,.parent{height: 100%;}
.top,.bottom{
    position: absolute;
    left: 0;
    right: 0;
    height: 50px;
}
.top{top: 0;}
.bottom{bottom: 0;}
.middleWrap{
    height: 100%;
    font-size: 0;
    overflow: hidden;
}
.middle{
    position: relative;
    height: 100%;
    margin: 50px 0;
    overflow: hidden;
}
.left,.rightWrap{
    display: inline-block;
    vertical-align: top;
    font-size: 16px;
}
.left{
    position: absolute;
    width: 100px;
    margin-right: 20px;
    height: 100%;
}
.rightWrap{
    width: 100%;
    height: 100%;
}
.right{
    height: 100%;
    margin-left: 120px;
    overflow: auto;
}
.right-in{
    height: 1000px;
}
</style>

<div class="parent" id="parent" style="background-color: lightgrey;">
    <div class="top" style="background-color: lightblue;">
        <p>top</p>
    </div>  
    <div class="middleWrap">
        <div class="middle" style="background-color: pink;">
            <div class="left" style="background-color: orange;">
                <p>left</p>
            </div>
            <div class="rightWrap">
                <div class="right" style="background-color: lightsalmon;">
                    <div class="right-in">
                        <p>right</p>
                    </div>
                </div>                
            </div>
        </div>         
    </div>
    <div class="bottom" style="background-color: lightgreen;">
        <p>bottom</p>
    </div>        
</div>

 

inline-block

【思路二】inline-block

  inline-block内联块布局的首要缺点是供给设置垂直对齐格局vertical-align,则供给处理换行符解析成空格的茶余饭后难点。IE7-浏览器不帮忙给块级元素设置inline-block属性,包容代码是display:inline;zoom:1;

【1】inline-block + margin + calc

  一般的话,要化解inline-block成分之间的闲暇难题,要在父级设置font-size为0,然后在子成分上将font-size设置为暗中认可大小

  [注意]IE8-、android4.3-、IOS5.1-不扶助,android4.4+只扶助加减运算 

<style>
p{margin: 0;}
.parent{font-size: 0;}
.left{display:inline-block;vertical-align:top;width:100px;margin-right:20px;font-size:16px;}
.right{display:inline-block;vertical-align:top;width:calc(100% - 120px);font-size:16px;}
</style>

<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</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{position:relative;display:inline-block;vertical-align:top;width:100px;font-size:16px;}
.rightWrap{display:inline-block;vertical-align:top;width:100%;margin-left: -100px;font-size:16px;}
.right{margin-left: 120px;}
</style>

<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>
    <div class="rightWrap" style="background-color: pink;">
        <div class="right"  style="background-color: lightgreen;">
            <p>right</p>
            <p>right</p>
        </div>        
    </div>
</div>

 

table

  水平方向子成分的距离能够用border实现。全体浏览器都不扶助给table-cell成分设置overflow属性。firefox和IE11浏览器不援助给table-cell元素的安装百分之百莫斯科大学的子元素设置overflow属性

<style>
body,p{margin: 0;}
body,html,.parent{height: 100%;}
.top,.bottom{
    position: absolute;
    width: 100%;
    height: 50px;
}
.bottom{bottom: 0;}
.middleWrap{
    height: 100%;
    overflow: hidden;
}
.middle{
    width: 100%;
    height: 100%;
    display: table;    
    margin: 50px 0;
    table-layout: fixed;
}
.left{
    display: table-cell;
    width: 120px;
    border-right: 20px solid lightgray;
}
.rightWrap{
    display: table-cell;
    height: 100%;
}
.right{
    height: 100%;
    overflow: auto;
}
.right-in{
    height: 1000px;
}
</style>

<div class="parent" id="parent" style="background-color: lightgrey;">
    <div class="top" style="background-color: lightblue;">
        <p>top</p>
    </div> 
    <div class="middleWrap">
        <div class="middle" style="background-color: pink;">
            <div class="left" style="background-color: orange;">
                <p>left</p>
            </div>     
            <div class="rightWrap">
                <div class="right" style="background-color: lightsalmon;">
                    <div class="right-in">
                        <p>right</p>
                    </div>            
                </div>                    
            </div>   
        </div>         
    </div>    
    <div class="bottom" style="background-color: lightgreen;">
        <p>bottom</p>
    </div>        
</div>

882828九五至尊手机版, 

table

【思路三】table

  使用table布局的先天不足是因素被安装为table后,内容撑开宽度,所以必要设置width:百分之百。若要包容IE7-浏览器,须求改为<table>结构。由于table-cell成分不恐怕设置margin,若须求在要素间设置间距,须要增添结构

<style>
p{margin: 0;}
.parent{display:table;width: 100%;table-layout: fixed;}
.left,.rightWrap{display:table-cell;}
.left{width: 100px;}
.right{margin-left: 20px;}
</style>

<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>
    <div class="rightWrap" style="background-color: pink;">
        <div class="right"  style="background-color: lightgreen;">
            <p>right</p>
            <p>right</p>
        </div>        
    </div>    
</div>

 

absolute

<style>
body,p{margin: 0;}
body,html,.parent{height: 100%;}
.top,.middle,.bottom{
    position: absolute;
    left: 0;
    right: 0;
}
.top{
    top: 0;
    height: 50px;
}
.bottom{
    bottom: 0;
    height: 50px;
}
.middle{
    top: 50px;
    bottom: 50px;
}
.left,.right{
    position: absolute;
    top: 0;
    bottom: 0;
}
.left{
    width:100px;
}
.right{
    left: 120px;
    right: 0;
    overflow: auto;
}
.right-in{
    height: 1000px;
}
</style>

<div class="parent" id="parent" style="background-color: lightgrey;">
    <div class="top" style="background-color: lightblue;">
        <p>top</p>
    </div> 
    <div class="middle" style="background-color: pink;">
        <div class="left" style="background-color: orange;">
            <p>left</p>
        </div>     
        <div class="right" style="background-color: lightsalmon;">
            <div class="right-in">
                <p>right</p>
            </div>            
        </div>                    
    </div>              
    <div class="bottom" style="background-color: lightgreen;">
        <p>bottom</p>
    </div>        
</div>

 

absolute

【思路四】absolute

  absolute布局的瑕疵是由于父成分必要设置为relative,且子成分设置为absolute,所以父成分的冲天并不是由子成分撑开的,必要单独设置。

  [注意]IE6-不援救相对的偏移属性同时设置

<style>
p{margin: 0;}
.parent{position: relative;width:100%;height:40px;}
.left{position: absolute;left:0;width:100px;}
.right{position: absolute;left:120px;right:0;}
</style>

<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>        
</div>

 

flex

  flex常用来小范围的布局,使用全屏布局时会因为质量难点,现身卡顿现象。纵然要利用全屏自适应布局,则只有flex才能达到规定的标准效果

<style>
body,p{margin: 0;}
body,html,.parent{height: 100%;}
.parent{
    display: flex;
    flex-direction: column;
}
.top,.bottom{
    height: 50px;
}
.middle{
    display: flex;
    flex: 1;
}
.left{
    width: 100px;
    margin-right: 20px;
}
.right{
    flex: 1;
    overflow: auto;
}
.right-in{
    height: 1000px;
}
</style>

<div class="parent" id="parent" style="background-color: lightgrey;">
    <div class="top" style="background-color: lightblue;">
        <p>top</p>
    </div> 
    <div class="middle" style="background-color: pink;">
        <div class="left" style="background-color: orange;">
            <p>left</p>
        </div>     
        <div class="right" style="background-color: lightsalmon;">
            <div class="right-in">
                <p>right</p>
            </div>            
        </div>                    
    </div>              
    <div class="bottom" style="background-color: lightgreen;">
        <p>bottom</p>
    </div>        
</div>

 

flex

【思路五】flex

  flex弹性盒模型是丰硕有力的布局方式。但由于其性质消耗较大,适合于部分小范围的布局

  [注意]IE9-浏览器不支持

<style>
p{margin: 0;}
.parent{display: flex;}
.left{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="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>        
</div>

 

grid

<style>
body,p{margin: 0;}
body,html,.parent{height: 100%;}
.parent{
    display: grid;
    grid-template-rows:50px auto 50px;
}
.middle{
    overflow:hidden;
    display:grid;
    grid-template-columns: 100px 1fr;
    grid-gap:20px;
}
.right{
    overflow:auto;
}
.right-in{
    height: 1000px;
}
</style>

<div class="parent" id="parent" style="background-color: lightgrey;">
    <div class="top" style="background-color: lightblue;">
        <p>top</p>
    </div> 
    <div class="middle" style="background-color: pink;">
        <div class="left" style="background-color: orange;">
            <p>left</p>
        </div>     
        <div class="right" style="background-color: lightsalmon;">
            <div class="right-in">
                <p>right</p>
            </div>            
        </div>                    
    </div>              
    <div class="bottom" style="background-color: lightgreen;">
        <p>bottom</p>
    </div>        
</div>

 

grid

【思路六】: 使用栅格布局grid达成

  [注意]IE10-浏览器不扶助

<style>
p{margin: 0;}
.parent{display: grid;grid-template-columns: 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="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>        
</div>    

 

总结

  全屏布局实际上就是两列三列自适应布局的恢宏方式。由于完毕的是全屏效果,中度其实是定点的,所以思路并没有等高布局受制。水平方向成分之间的区间依照实际情状接纳margin、padding、border都足以兑现

 

相关文章

Your Comments

近期评论

    功能


    网站地图xml地图