浮動在移動佈局中再也不重要,flex盒模型愈來愈重要。javascript
flexbox經歷過三個版本,主要區別是2009年到2012年之間的語法變化。
最新的語法和如今規範是同步的(例display:flex和「flex-{*}」屬性)。
在這之間的語法是2011年出現的非官方語法,只能被IE識別(例display:flexbox;display: -ms-flexbox)。
最老的語法產生於2009年(例display: box;或者「box-{*}」屬性)
2、flex經常使用屬性css
-webkit-box模型【舊】html
flex模型 【新】java
【舊】box-flex:0|任意數字;該屬性讓子容器針對父容器的寬度按必定規則進行劃分。 css3
【新】flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ],默認值 0 1 auto。web
.parent寬度500px,其子元素水平排列,child-one佔1/6,child-two佔2/6,child-three佔了3/6。app
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .parent{ width: 500px; height: 200px; display: flex; flex-direction: row;/* 雖然默認的排列方式是水平的,可是爲了區分起見,加上該屬性 */ } .child-one{ background: lightblue; flex: 1; } .child-two{ background: lightgray; flex: 2; } .child-three{ background: lightgreen; flex: 3; } </style> </head> <div style="display:flex;flex-direction:row;justify-content:center;border: 1px solid #000;"><!---box-pack:center讓.parent水平居中--> <div class="parent"> <div class="child-one">1</div> <div class="child-two">2</div> <div class="child-three">3</div> </div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .parent{ width: 500px; height: 200px; display: flex; background-color:pink; flex-direction: row;/* 雖然默認的排列方式是水平的,可是爲了區分起見,加上該屬性 */ } .child-one{ background: lightblue; flex: 1; } .child-two{ background: lightgray; flex: 2; } .child-three{ background: lightgreen; /*定寬,並加上左右margin,父元素加上粉色背景色更好理解*/ width:150px; margin:0 15px; } </style> </head> <div style="display: flex;justify-content: center;border: 1px solid #000;"><!--justify-content:center讓.parent水平居中--> <div class="parent"> <div class="child-one">1</div> <div class="child-two">2</div> <div class="child-three">3</div> </div> </div> </body> </html>
.parent中的子元素垂直排列,因此每一個子元素寬度佔100%。ide
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .parent{ width: 400px; height: 600px; display: flex; background-color:pink; flex-direction: column;/*子元素垂直排列 */ } .child-one{ background: lightblue; flex: 1; } .child-two{ background: lightgray; flex: 2; } .child-three{ background: lightgreen; /*定高,有上下margin,父元素加上粉色背景色更好理解*/ height:200px; margin:15px 0; } </style> </head> <body> <div style="display: flex;justify-content: center;border: 1px solid #000;"><!---webkit-box-pack:center讓.parent水平居中--> <div class="parent"> <div class="child-one">1</div> <div class="child-two">2</div> <div class="child-three">3</div> </div> </div> </body> </html>
父元素中子元素水平排列,子元素定高時設置垂直方向對齊方式爲居中對齊。佈局
重點:align-item:center,之前佈局用box-align:center;學習
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .parent{ width: 400px; height: 200px; display: flex; background-color:pink; flex-direction: row; align-items: center; } .child-one{ background: lightblue; flex: 1; height:100px; } .child-two{ background: lightgray; flex: 2; height:110px; } .child-three{ background: lightgreen; flex: 2; height:120px; } </style> </head> <body> <div style="display: flex;justify-content: center;border: 1px solid #000;"><!--justify-content:center讓.parent水平居中--> <div class="parent"> <div class="child-one">1</div> <div class="child-two">2</div> <div class="child-three">3</div> </div> </div> </body> </html>
flexbox經典的佈局應用是垂直等高,水平均分,按比例劃分,水平垂直居中,還能夠實現移動端的彈窗。
.parent{display: -webkit-box; display: -webkit-flex; display: flex;} .child{-webkit-box-flex: 1; -webkit-flex: 1; flex: 1;}
完整代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .parent{ display: flex; height:100px; width:150px; background-color:pink;} .child{ flex: 1; border:1px solid green; } </style> <div class="parent"> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div> </body> </html>
.parent{display: -webkit-box; display: -webkit-flex; display: flex; -webkit-box-pack: center; -webkit-justify-content: center; justify-content: center;}
完整demo:
<style> .parent{display: -webkit-box; display: -webkit-flex; display: flex; -webkit-box-pack: center; -webkit-justify-content: center; justify-content: center; height:100px; width:150px; background-color:pink;} .child{ width:50px; height:50px; border:1px solid green; } </style> <div class="parent"> <div class="child"></div> </div>
.parent{display: -webkit-box; display: -webkit-flex; display: flex; -webkit-box-align: center; -webkit-align-items: center; align-items: center;}
完整demo
<style> .parent{display: -webkit-box; display: -webkit-flex; display: flex; -webkit-box-align: center; -webkit-align-items: center; align-items: center; height:100px; width:150px; background-color:pink;} .child{ width:50px; height:50px; border:1px solid green; } </style> </head> <body> <div class="parent"> <div class="child"></div> </div>
如今移動端不少彈窗組件使用flexbox來實現,直接嵌套div.overlay>div.pop。
<style> .overlay{ /*flex style*/ display:-webkit-box; -webkit-box-orient:horizontal; -webkit-box-pack:center; -webkit-box-align:center; display:-moz-box; -moz-box-orient:horizontal; -moz-box-pack:center; -moz-box-align:center; display:-o-box; -o-box-orient:horizontal; -o-box-pack:center; -o-box-align:center; display:-ms-box; -ms-box-orient:horizontal; -ms-box-pack:center; -ms-box-align:center; display:box; box-orient:horizontal; box-pack:center; box-align:center; display: -webkit-flex; -webkit-align-items: center; -webkit-justify-content: center; display: flex; align-items: center; justify-content: center; /*other style*/ width:100%; max-width:750px; height:100%; position:fixed; top:0; left:0; background:rgba(0,0,0,0.5); } .popup{ width:90%; max-width:650px; border:1px solid green; padding:20px 4% 4% 4%; box-sizing:border-box; height:auto; background:#fff; border-radius:4px; position:relative; } .popup-close{ width:15px; height:14px; background:url(image/close.png) no-repeat; background-size:100% 100%; position:absolute; top:8px; right:8px; } </style> 主頁面的文字 <div class="overlay"> <div class="popup"> <a href="javascript:;" class="popup-close"></a> 彈層的文字 </div> </div>
PC端:
-webkit-
提示:舊版本的Firefox(22-27)支持除了flex-wrap
和flex-flow
以外的新語法。Opera (12.1+ - 17+)使用flex
能夠沒有私有前綴,可是中間的15和16版本須要私有前綴。
移動端:
-webkit-
本文做者starof,因知識自己在變化,做者也在不斷學習成長,文章內容也不定時更新,爲避免誤導讀者,方便追根溯源,請諸位轉載註明出處:http://www.cnblogs.com/starof/p/4894140.html有問題歡迎與我討論,共同進步。