<div class="container"> <div class="item1"></div> <div class="item2"></div> <div class="item3"></div> ... </div>
<view class="container"> <view class="item1"></view> <view class="item2"></view> <view class="item3"></view> ... </view>
當咱們要用時這個佈局的時候對於外層的結構,咱們對他的css樣式設定:php
<style type="text/css"> .container{ display: flex; /*or inline-flex*/ } </style>
接下來咱們就須要來規定這個item排列的方向了,依舊對外層結構css設定:css
.container{
display: flex; /*or inline-flex*/
flex-direction: row;
}
flex-direction這個屬性是用來規定flex項目在軸方向上面排列的順序,html
.container{
display: flex; /*or inline-flex*/
flex-direction: row;
flex-wrap:wrap;
}
flex-wrap是決定項目是否多行顯示的屬性,項目默認狀況下是在一行下顯示的。小程序
.container{ display: flex; /*or inline-flex*/ flex-flow:row wrap; }
在咱們一些須要構建的佈局裏,咱們還須要去讓他可以自由的伸縮,這也是flex佈局的優點之一,可以極大的方便咱們去提高效率。微信小程序
.container{ width: 100%; height: 400px; background-color: #ccc; display: flex; /*or inline-flex*/ flex-flow:row wrap; justify-content:flex-start; } .item{ flex:0 0 30%; } .item1{background-color:#0074e0;} .item{background-color:#008c00;} .item3{background-color:#be0000;}
flex-start微信
.container{ width: 100%; height: 400px; background-color: #ccc; display: flex;/*or inline-flex*/ flex-flow:row wrap; justify-content:flex-start; align-content: flex-start; } .item{ flex:0 0 30%; min-height: 100px; } .item1,.item6{background-color:#0074e0;} .item2{background-color:#008c00;} .item5{background-color:#234567;} .item3,.item4{background-color:#be0000;}
flex-startapp
.container{ width: 100%; height: 400px; background-color: #ccc; display: flex;/*or inline-flex*/ flex-flow:row wrap; justify-content:flex-start; align-items: flex-start; } .item{ flex:0 0 30%; min-height: 100px; }
align-items: flex-start佈局
.container{ width: 100%; height: 400px; background-color: #ccc; display: flex;/*or inline-flex*/ flex-flow:row wrap; justify-content:flex-start; /*align-items: flex-start;*/ } .item{ flex:0 0 30%; min-height: 100px; max-height: 300px; } .item:nth-child(2){ max-height: 200px; align-self: flex-start; }
align-items: flex-endflex
<div class="container"> <div class="item1"></div> </div>
在微信小程序裏面能夠是這樣的結構:flexbox
<view class="container">
<view class="item1"></view>
</view>
給他設定才css樣式,
.container{ width: 100%; height: 400px; background-color: #ccc; display: flex; flex-flow:row wrap; justify-content:flex-start; } .item1{ background-color: #0074e0; width: 50px; height: 50px; }
顯示是這樣的:
.container{ width: 100%; height: 400px; background-color: #ccc; display: flex; flex-flow:row wrap; justify-content:center; /*樣式修改在這裏*/ align-items: center; /*樣式修改在這裏*/ } .item1{ background-color: #0074e0; width: 100px; height:100px; }
如今讓他在右下角顯示以下:
.container{ width: 100%; height: 400px; background-color: #ccc; display: flex; flex-flow:row wrap; justify-content:flex-end; /*樣式修改在這裏*/ align-items:flex-end; /*樣式修改在這裏*/ } .item1{ background-color: #0074e0; width: 100px; height:100px; }
在加上一個項目:(後面新增再也不贅述)
<div class="container"> <div class="item1"></div> <div class="item2"></div> </div>
在微信小程序裏面能夠是這樣的結構:
<view class="container"> <view class="item1"></view> <view class="item2"></view> </view>
左上橫排
.container{ width: 100%; height: 400px; background-color: #ccc; display: flex; flex-flow:row wrap; justify-content:flex-start; align-items:flex-start; }
水平方向居中
.container{ width: 100%; height: 400px; background-color: #ccc; display: flex; flex-flow:row wrap; justify-content:center; align-items:flex-start; }
兩個項目不貼在一塊兒
.container{ width: 100%; height: 400px; background-color: #ccc; display: flex; flex-flow:row wrap; justify-content:space-around; align-items:flex-start; }
從上面的列子看來,僅僅只是就該某些css的屬性,就能達到之前須要花大量css樣式的聲明才能達到的效果。
<div class="container"> <div class="row"> <div class="item1"></div> <div class="item2"></div> <div class="item1"></div> </div> <div class="row"> <div class="item2"></div> <div class="item1"></div> <div class="item2"></div> </div> <div class="row"> <div class="item1"></div> <div class="item2"></div> <div class="item1"></div> </div> </div>
css樣式以下:
.container{ width: 400px; height: 400px; background-color: #ccc; display: flex; flex-wrap: wrap; align-content: space-around; } .row{ display:flex; flex-basis: 100%; justify-content:space-around; } .item1, .item2{ width: 100px; height:100px; } .item1{ background-color: #0074e0; } .item2{ background-color: #008c00; }
僅僅只是添加下一條css樣式,而後增長項目個數,修改下外框的寬高度就有這樣的效果顯示。