html代碼:css
<div class="container"> <header>header...</header> <main>內容</main> <footer>footer...</footer> </div>
css代碼html
header{ height:100px; background:#ccc; } footer{ height:100px; background:#ccc; } .container{ display:flex; flex-direction:column; height:100vh; } main{ flex-grow:1; }
1:使用flex-direction使總體佈局從上向下佈局web
2:flex-grow:1使得main自動填充剩餘的空間ide
css代碼佈局
ul{ display:flex; flex-wrap:wrap; } li{ list-style:none; flex:0 0 25%; background:#ddd; height:100px; border:1px solid red; }
1:flex:0 0 25%,至關於flex-basis:25%,使得每個列表項的寬度佔外層容器(本例中的ul元素)的25%,所以每行最多可以排開四個列表項。post
2:flex-wrap:wrap,使得每行填滿時會自動換行flex
css代碼spa
ul{ display:flex; flex-wrap:wrap; justify-content:space-between; border:1px solid black; } li{ list-style:none; width:120px; background:#ddd; height:100px; border:1px solid red; }
1:justify-content:space-between;使主軸方向的多餘空間平均分配在兩兩item之間code
html代碼htm
<div class="row">
<div class="column">column1</div>
<div class="column">colum22</div>
<div class="column">colum322</div>
</div>
css代碼
.row{ display:flex; flex-wrap:wrap; border:1px solid black; } .column{ list-style:none; background:#ddd; flex:1; height:100px; border:1px solid red; }
html代碼
<div class="container"> <main>main</main> <aside>aside</aside> <nav>nav</nav> </div>
css代碼
.container{ display:flex; height:100vh; } aside{ width:50px; background:#ccc; } main{ flex-grow:1; background:#def; } nav{ width:80px; background:#ccc; order:-1; }
html代碼
<div class="container"> <div class="inner">我是中間的內容</div> </div>
css代碼
.container{ height:300px; width:300px; border:1px solid red; display:flex; justify-content:center; align-items:center; } .inner{ border:1px solid black; }
這麼多場景都難不倒flex有木有,果真名不虛傳~( 想更詳細的瞭解flex相關屬性的話,請戳flex入門—瞭解這些flex屬性~)