百分比布局:使用场景比较传统,经过百分比控制盒子宽度
demo :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> .p { width: 100%; height: 50px; border: 1px solid #000; } .p div { float: left; } .s_1 { width: 30%; height: 100%; background-color: red; } </style> </head> <body> <div class="p"> <div class="s_1"></div> </div> </body> </html>
PC端浏览器支持状况比较差:IE11或更低版本不支持flex或仅支持部分;web
使用: 若是是PC端页面布局,采用传统方式;若是是移动端或者是不考虑兼容的pc则采用flex;浏览器
注意 : 当为父盒子设为 flex 布局之后,子元素的 float、clear 和 vertical-align 属性将失效。bash
基本介绍:svg
flex-direction:改变主轴的选择,元素就是按照不一样的主轴进行排布布局
justify-content:控制主轴上的元素的对齐方式,相似word里的左对齐,右对齐,居中对齐,分散对齐
语法值 :flex
flex-wrap:控制子元素是否换行ui
flex-wrap:nowrap;
flex-wrap:wrap;
flex-flow:复合属性:设置主轴与换行spa
flex-flow:row wrap;
code
补充之秀起来---->线性渐变
语法:
起始方向,颜色1,颜色2,…
background: -webkit-linear-gradient(30deg, red, blue);
1.必须有私有前缀
2.起始方向:能够为方向名词left 或 deg度数,默认从上到下
3.颜色个数:最少2两个颜色 `
彩蛋---------常见布局之圣杯布局
<!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> /* ********************************************************定位方式办法 */ .box_1 { width: 100%; height: 100px; border: 1px solid #000; position: relative; box-sizing: border-box; padding: 0 100px; } .box_1 .left { width: 100px; height: 100px; background-color: red; position: absolute; top: 0; left: 0; } .box_1 .right { width: 100px; height: 100px; background-color: red; position: absolute; top: 0; right: 0; } .box_1 .mid { width: 100%; height: 100%; background-color: #ccc; } /* *****************************************************************flex布局 */ .box_2 { margin-top: 10px; width: 100%; height: 100px; border: 1px solid #000; /* flex */ display: flex; /* 默认主轴 行 */ } .box_2 .left { width: 100px; height: 100px; background-color: red; } .box_2 .right { width: 100px; height: 100px; background-color: red; } .box_2 .mid { flex: 1; /* */ /* align-self: auto; 当父亲没有设置侧轴上的对齐,auto自动变为拉伸 */ background-color: #ccc; } /* *-------------------大盒子不设置宽度,中间采用margin值的方法------------------------------------------------ */ .box_3 { margin-top: 10px; height: 100px; border: 1px solid #000; position: relative; } .box_3 .left { width: 100px; height: 100px; background-color: red; position: absolute; top: 0; left: 0; } .box_3 .right { width: 100px; height: 100px; background-color: red; position: absolute; top: 0; right: 0; } .box_3 .mid { height: 100%; margin: 0 100px; background-color: #ccc; } </style> </head> <body> <div class="box_1"> <div class="left"></div> <div class="mid"></div> <div class="right"></div> </div> <div class="box_2"> <div class="left"></div> <div class="mid"></div> <div class="right"></div> </div> <div class="box_3"> <div class="left"></div> <div class="mid"></div> <div class="right"></div> </div> </body> </html>
效果图