彈性盒子(Flexible Box)是css3中盒子模型的彈性佈局,在傳統的佈局方式上增長了不少靈活性。css
在父盒子上定義display屬性:html
#box{ display: -webkit-flex; //webkit內核瀏覽器的兼容設置,下同 display: flex; }
固然還有行內佈局的彈性盒子css3
#box{ display: -webkit-inline-flex; display: inline-flex; }
注意,設爲Flex佈局之後,子元素的float、clear和vertical-align屬性將失效。web
彈性盒子兩個軸如圖:瀏覽器
在研究具體屬性前,先創建一個基本模板,必要時修改一下部分參數:佈局
<!-- html部分 --> <div id="flex-box"> <div class="flex-item">1</div> <div class="flex-item">2</div> <div class="flex-item">3</div> <div class="flex-item">4</div> <div class="flex-item">5</div> </div> <!-- css部分 --> #flex-box{ width: 200px; height: 200px; border: 1px solid #06f; display: flex; display: -webkit-flex; } .flex-item{ height: 30px; width: 30px; color: #f00; font-size: 20px; line-height: 30px; text-align: center; background-color: #8f0; border: 1px solid #f00; }
排列方向設置flex
#flex-box{ flex-direction: row | row-reverse | column | column-reverse; /* @value row 從左到右(默認)*/ /* @value row-reverse 從右到左*/ /* @value column 從上到下*/ /* @value column-reverse 從下到上*/ }
換行設置spa
.box{ flex-wrap: nowrap | wrap | wrap-reverse; /* @value nowrap 不換行(默認)*/ /* @value wrap 換行*/ /* @value wrap-reverse 換行 層倒序*/ }
flex-direction和flex-wrap的縮寫屬性code
.box { flex-flow: <flex-direction> || <flex-wrap>; }
水平方向上對齊htm
.box { justify-content: flex-start | flex-end | center | space-between | space-around; } /* value flex-start 左對齊(默認)*/ /* value flex-end:右對齊*/ /* value center: 居中*/ /* value space-between:兩端對齊,項目之間的間隔都相等*/ /* value space-around:每一個項目兩側的間隔相等。因此,項目之間的間隔比項目與邊框的間隔大一倍*/
垂直方向上對齊
.box { align-items: flex-start | flex-end | center | baseline | stretch; } /* value flex-start 交叉軸的起點對齊*/ /* value flex-end 交叉軸的終點對齊*/ /* value center 交叉軸的中點對齊*/ /* value baseline 項目的第一行文字的基線對齊*/ /* value stretch 若是項目未設置高度或設爲auto,將佔滿整個容器的高度(默認)*/
多根軸線的對齊方式
.box { align-content: flex-start | flex-end | center | space-between | space-around | stretch; } /* value flex-start 與交叉軸的起點對齊*/ /* value flex-end 與交叉軸的終點對齊*/ /* value center 與交叉軸的中點對齊*/ /* value space-between 與交叉軸兩端對齊,軸線之間的間隔平均分佈*/ /* value space-around 每根軸線兩側的間隔都相等。因此,軸線之間的間隔比軸線與邊框的間隔大一倍*/ /* value stretch 軸線佔滿整個交叉軸(默認值)*/
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>9 faces Dice</title> </head> <style> body{ padding: 0; margin: 0; } #demo{ display: flex; background-color: #444; } .dice{ width: 90px; height: 90px; border-radius: 22px; margin: 20px; padding: 10px; box-sizing: border-box; border: 2px solid #aaa; background-color: #eee; display: flex; flex-direction: column; justify-content: space-between; } .dot{ width: 20px; height: 20px; background-color: #222; border-top: 2px solid #000; box-sizing: border-box; /*box-shadow: 0px 0px 2px #000;*/ border-radius: 20px; } .line{ display: flex; justify-content: space-between; } #one{ justify-content: center; align-items: center; } #two{ align-items: center; } #three .line:nth-of-type(2){ justify-content: center; } #three .line:nth-of-type(3){ justify-content: flex-end; } #five .line:nth-of-type(2){ justify-content: center; } </style> <body> <div id="demo"> <div id="one" class="dice"> <div class="line"> <div class="dot"></div> </div> </div> <div id="two" class="dice"> <div class="line"> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> </div> </div> <div id="three" class="dice"> <div class="line"> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> </div> </div> <div id="four" class="dice"> <div class="line"> <div class="dot"></div> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> <div class="dot"></div> </div> </div> <div id="five" class="dice"> <div class="line"> <div class="dot"></div> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> <div class="dot"></div> </div> </div> <div id="six" class="dice"> <div class="line"> <div class="dot"></div> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> <div class="dot"></div> </div> </div> <div id="seven" class="dice"> <div class="line"> <div class="dot"></div> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> <div class="dot"></div> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> <div class="dot"></div> </div> </div> <div id="eight" class="dice"> <div class="line"> <div class="dot"></div> <div class="dot"></div> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> <div class="dot"></div> <div class="dot"></div> </div> </div> <div id="nine" class="dice"> <div class="line"> <div class="dot"></div> <div class="dot"></div> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> <div class="dot"></div> <div class="dot"></div> </div> <div class="line"> <div class="dot"></div> <div class="dot"></div> <div class="dot"></div> </div> </div> </div> </body> </html>
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>test</title> <style> ul{ width: 1000px; margin: 0 auto; padding: 0; columns: 4; list-style: none; column-gap: 2px; column-fill: balance; } ul li { margin: 4px 0; } ul li img{ width:240px; } </style> </head> <body> <ul> <li><img src="img/1.jpeg" alt=""></li> <li><img src="img/2.jpeg" alt=""></li> <li><img src="img/3.jpeg" alt=""></li> <li><img src="img/4.jpeg" alt=""></li> <li><img src="img/5.jpeg" alt=""></li> <li><img src="img/6.jpeg" alt=""></li> <li><img src="img/7.jpeg" alt=""></li> <li><img src="img/8.jpeg" alt=""></li> <li><img src="img/9.jpeg" alt=""></li> <li><img src="img/10.jpeg" alt=""></li> <li><img src="img/11.jpeg" alt=""></li> <li><img src="img/12.jpeg" alt=""></li> <li><img src="img/13.jpeg" alt=""></li> <li><img src="img/14.jpeg" alt=""></li> <li><img src="img/15.jpeg" alt=""></li> </ul> </body> </html>
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>test</title> <style> #grid-demo{ width: 1000px; margin: 0 auto; } .grid{ display: flex; margin: 20px 0; height: 60px; } .grid .grid-item{ flex-grow: 1; background-color: #eee; height: 60px; line-height: 60px; font-size: 26px; width: 5%; text-align: center; margin: 0 10px; } .grid .col-10{ flex-basis: 83.3333% } .grid .col-8{ flex-basis: 66.6666%; } .grid .col-6{ flex-basis: 50%; } .grid .col-4{ flex-basis: 33.3333%; } .grid .col-3{ flex-basis: 25%; } .grid .col-2{ flex-basis: 16.6667%; } </style> </head> <body> <div id="grid-demo"> <div class="grid"> <div class="grid-item">1</div> <div class="grid-item">1</div> <div class="grid-item">1</div> <div class="grid-item">1</div> <div class="grid-item">1</div> <div class="grid-item">1</div> <div class="grid-item">1</div> <div class="grid-item">1</div> <div class="grid-item">1</div> <div class="grid-item">1</div> <div class="grid-item">1</div> <div class="grid-item">1</div> </div> <div class="grid"> <div class="grid-item">2</div> <div class="grid-item">2</div> <div class="grid-item">2</div> <div class="grid-item">2</div> <div class="grid-item">2</div> <div class="grid-item">2</div> </div> <div class="grid"> <div class="grid-item">3</div> <div class="grid-item">3</div> <div class="grid-item">3</div> <div class="grid-item">3</div> </div> <div class="grid"> <div class="grid-item">4</div> <div class="grid-item">4</div> <div class="grid-item">4</div> </div> <div class="grid"> <div class="grid-item">6</div> <div class="grid-item">6</div> </div> <div class="grid"> <div class="grid-item col-8">8</div> <div class="grid-item col-4">4</div> </div> <div class="grid"> <div class="grid-item">12</div> </div> </div> </body> </html>
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>test</title> <style> body{ margin: 0; padding: 0; } #demo{ display: flex; flex-direction: column; min-height: 100vh; } header, footer{ height: 100px; background-color: darkblue; color: white; text-align: center; line-height: 100px; } .flex-body{ flex-grow: 1; } </style> </head> <body> <div id="demo"> <header>I am header!</header> <div class="flex-body">I am body!</div> <footer>I am footer!</footer> </div> </body> </html>