在移动端中的flex布局
1 2 3 4 5 6 7 | display : flex//声明该父元素为弹性布局 flex-direction : row | row-reverse | column | column-reverse//控制子元素排列方向:从左到右(默认),从右到左,从上到下,从下到上 flex-wrap : nowrap | wrap | wrap-reverse//控制子元素换行情况:不换行(默认),换行(下一行在下方),换行(下一行在上方) flex-flow : flex-direction||flex-wrap//direction和wrap的简写属性 justify-content : flex-start | flex-end | center | space-between | space-around//控制子元素的对齐方式(以从左到右为例):左对齐(默认),右对齐,居中,两端对齐,项目间间隔相等 align-items : flex-start | flex-end | center | baseline | stretch//控制交叉方向上(水平->垂直)的对齐方式,其中stretch为默认值,子元素将占满交叉方向的全部高度/宽度 align-content : flex-start | flex-end | center | space-between | space-around | stretch//当出现换行的时候,这个属性控制多行之间的对齐方式(与justify-content相似) |
常用属性的介绍
元素垂直水平居中(登录弹框)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Document</title> <style type="text/css"> *{ margin: 0; padding: 0; } img{ border:0; } ol, ul ,li{list-style: none;} .box { display: flex; width: 400px; height:400px; border:1px solid #ccc; /* 主轴对齐 */ justify-content:center; /* 副轴对齐 */ align-items:center; } .box div { width:100px; height: 100px; background: red; } </style> </head> <body> <div class="box"> <div>1</div> </div> </body> </html> |
垂直两端居中(轮播图两边的按钮)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Document</title> <style type="text/css"> *{ margin: 0; padding: 0; } img{ border:0; } ol, ul ,li{list-style: none;} .box { display: flex; /* 元素垂直水平居中 */ width: 400px; height:400px; border:1px solid #ccc; /* 两端对齐 */ justify-content:space-between; align-items:center; /* 副轴对齐到底部 */ /* align-items:flex-end; */ } .box div { width:100px; height: 100px; background: red; } </style> </head> <body> <div class="box"> <div>1</div> <div>1</div> </div> </body> </html> |
元素位于底部(移动端的选项框)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Document</title> <style type="text/css"> *{ margin: 0; padding: 0; } img{ border:0; } ol, ul ,li{list-style: none;} .box { display: flex; width: 400px; height:400px; border:1px solid #ccc; /* 副轴对齐到底部 */ align-items:flex-end; } .box div { width:100px; height: 100px; background: red; } </style> </head> <body> <div class="box"> <div>1</div> <div>1</div> <div>1</div> <div>1</div> </div> </body> </html> |
超出区域换行(9宫格)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Document</title> <style type="text/css"> *{ margin: 0; padding: 0; } img{ border:0; } ol, ul ,li{list-style: none;} .box { width:300px; height: auto; border:1px solid #ccc; margin:10px auto; } .box ul { display: flex; flex-wrap:wrap; } ul>li { width: 100px; height: 100px; border:1px solid #ccc; box-sizing:border-box; } </style> </head> <body> <div class="box"> <ul> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> </div> </body> </html> |
参考文档:
http://www.ruanyifeng.com/blog/2015/07/flex-examples.html
7