前端开发Web前端之路让前端飞

CSS3 弹性盒子(Flex Box)

2017-11-28  本文已影响91人  墨马

当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局方式。按比例分配父标签的宽度(或高度)空间。

子元素实现按比例分配

.son1 {
    flex: 2;
}

父元素也是需要添加必要的声明的

.father {
    display: flex;
 }

下面是具体实例:

.father{
     display: flex; 
     width:600px;
     height:200px;
}
.son1 {
    flex: 2;
    background:red;
}
.son2{
    flex: 1;
    background:blue;
}
 .son3{
    flex: 1;
    background:deeppink;
}
<div class="father">          
    <div class="son1"></div>  
    <div class="son2"></div>  
    <div class="son3"></div>  
</div>                        
2:1:1

如下盒子3实现具体宽度300px,而剩余空间由1和2按2:1实现分配

.son3 {
     width: 300px; 
     background:deeppink;
}
2:1:300px

flex-direction(指定了弹性子元素在父容器中的位置顺序)

flex-direction:column

justify-content (指定弹性容器的对齐方式)

下面定义父盒子的背景为灰色

align-items (决定了垂直方向上的空间利用,也就是垂直方向上的对齐表现)

其中stretch为默认值,为拉伸,也就是父标签高度过高,其孩子元素的高度就多高

align-self(用于设置弹性元素自身在垂直方向上的对齐方式)

.father{                        
    background:gray;            
    display: flex;              
    width:300px;                
    height:200px;               
}                               
.son1 {                         
    flex:1;                     
    height: 100px;              
    align-self: flex-start;     
    background:red;             
}                               
.son2{                          
    flex:1;                     
    height: 100px;              
    align-self: flex-end;       
    background:blue;            
}                               
 .son3{                         
    flex:1;                     
    height: 100px;              
    align-self: center;         
    background:deeppink;        
}                               
 .son4{                         
    flex:1;                     
    height: 100px;              
    align-self: baseline;       
    background:greenyellow;     
}                               
<div class="father">         
    <div class="son1"></div> 
    <div class="son2"></div> 
    <div class="son3"></div> 
    <div class="son4"></div> 
</div>                       

flex-wrap 属性(指定弹性盒子的子元素换行方式)

.father{                 
    background:gray;     
    display: flex;       
    flex-wrap:nowrap;    
    width:300px;         
    height:200px;        
}                        
.son1 {                  
    width:100px;         
    background:red;      
}                        
.son2{                   
    width:100px;         
    background:blue;     
}                        
 .son3{                  
    width:200px;         
    background:deeppink; 
}                        .father{                 
    background:gray;     
    display: flex;       
    flex-wrap:wrap  ;    
    width:300px;         
    height:200px;        
}                        
.son1 {                  
    width:100px;         
    background:red;      
}                        
.son2{                   
    width:100px;         
    background:blue;     
}                        
 .son3{                  
    width:200px;         
    background:deeppink; 
}                        

实际效果75px:75px:150px

弹性子元素排序(order: Num)

用整数值来定义排列顺序,数值越小,位置就越靠前。可以为负值。

.father{                    
    background:gray;        
    display: flex;          
    width:300px;            
    height:200px;           
}                           
.son1 {                         
    flex:2;                 
    order:2;                
    background:red;         
}                           
.son2{                       
    flex:1;                 
    order:1;                
    background:blue;        
}                           
 .son3{                     
    order:1;                
    flex:1;                 
    background:deeppink;    
}                           
<div class="father">         
    <div class="son1"></div> 
    <div class="son2"></div> 
    <div class="son3"></div> 
</div>                       

完美的居中

使用弹性盒子,居中变的很简单,只想要设置 margin: auto; 可以使得弹性子元素完全居中:

.father{             
    background:gray; 
    display: flex;   
    width:300px;     
    height:200px;    
}                    
.son1 {              
    width:100px;     
    height: 100px;   
    margin: auto;    
    background:red;  
}                    
<div class="father">               
    <div class="son1"></div>       
</div>                             
上一篇 下一篇

猜你喜欢

热点阅读