文章目录
flex
- 项目永远排列在主轴的正方向上
- flex分新旧两个版本
① -webkit-box
② -webkit-flex/flex - 我们都知道新版本的flex要比老版本的flex强大很多,有没有必要学习老版本的flex?
移动端开发者必须要关注老版本的flex,因为很多移动端设备内核低只认老版本的flex 。 - 老版本的box通过两个属性四个属性值控制了主轴的位置和方向
- 新版本的flex通过一个属性四个属性值控制了主轴的位置和方向
①老版本
容器
- 容器的布局方向
-webkit-box-orient
控制主轴是哪一根
属性值:
horizontal(水平的)
vertical(垂直的) - 容器的排列方向
-webkit-box-orient
控制主轴的方向
属性值:
normal
reverse - 富裕空间管理
只决定富裕空间的位置,不会给项目区分配空间
主轴
属性:-webkit-box-pack
若主轴是x轴:
属性值 | 效果 |
---|---|
start | 在右边 |
end | 在左边 |
center | 在项目两边 |
justify | 在项目之间 |
若主轴是y轴:
属性值 | 效果 |
---|---|
start | 在下边 |
end | 在上边 |
center | 在项目两边 |
justify | 在项目之间 |
侧轴
属性:-webkit-box-align
若侧轴是x轴:
属性值 | 效果 |
---|---|
start | 在右边 |
end | 在左边 |
center | 在项目两边 |
若侧轴是y轴:
属性值 | 效果 |
---|---|
start | 在下边 |
end | 在上边 |
center | 在项目两边 |
项目
弹性空间管理:
-webkit-box-flex
:弹性因子 (默认值为0)
②新版本
容器
- 容器的布局方向
- 容器的排列方向
属性:flex-direction
:控制主轴是哪一根,控制主轴的方向
属性值 | 效果 |
---|---|
row | 从左往右的 x轴 |
row-reverse | 从右往左的 x轴 |
column | 从上往下的y轴 |
column-reverse | 从下往上的y轴 |
- 富裕空间管理
只决定富裕空间的位置,不会给项目区分配空间
主轴
属性:justify-content
属性值 | 效果 |
---|---|
flex-start | 在主轴的正方向 |
flex-end | 在主轴的反方向 |
center | 在项目两边 |
justify | 在项目之间 |
space-between | 在项目之间 |
space-around | 在项目两边 |
侧轴
属性:align-items
属性值 | 效果 |
---|---|
flex-start | 在侧轴的正方向 |
flex-end | 在侧轴的反方向 |
center | 在项目两边 |
baseline | 基线对齐 |
stretch | 等高布局(项目没有高度) |
项目
弹性空间管理:
flex-grow
:弹性因子 (默认值为0)
③新版本新增
容器
flex-wrap
:控制的是侧轴的方向
属性值 | 效果 |
---|---|
nowrap | 不换行,没有侧轴 |
wrap | 侧轴方向由上而下 (flex-shrink失效) |
wrap-reverse | 侧轴方向由下而上 (flex-shrink失效) |
-
align-content
:多行多列时,侧轴空间的管理(把多行多列看成一个整体) -
flex-flow
:flex-direction和flex-wrap的简写属性
本质上控制了主轴和侧轴分别是哪一根,以及他们的方向
项目
- order:控制项目的排列顺序
- align-self:项目自身富裕空间的管理
- flex-shrink:收缩因子(默认值为 1 )
- flex-basis:伸缩规则计算的基准值(默认拿width和height的值)
④伸缩规则
-
flex-basis(默认值为auto)
-
flex-grow(默认值为0)
可用空间 = (容器大小 - 所有相邻项目flex-basis的总和) 可扩展空间 = (可用空间/所有相邻项目flex-grow的总和) 每项伸缩大小 = (伸缩基准值(flex-basis) + (可扩展空间 x flex-grow值))
-
flex-shrink (默认值为1)
- 计算收缩因子与基准值乘的总和
var a = 每一项flex-shrink*flex-basis之和 - 计算收缩因数
收缩因数=(项目的收缩因子项目基准值)/第一步计算总和
var b = (flex-shrinkflex-basis) / a - 移除空间的计算
移除空间= 项目收缩因数 x 负溢出的空间
var c = b * 溢出的空间
⑤侧轴富裕空间的管理
单行单列:
align-items
:
align-self
(优先级高):align-self 会对齐当前 flex 行中的 flex 元素,并覆盖 align-items 的值. 如果任何 flex 元素的侧轴方向 margin 值设置为 auto,则会忽略 align-self。
多行多列:
align-content
:属性定义弹性容器的侧轴方向上有额外空间时,如何排布每一行/列。当弹性容器只有一行/列时无作用
⑥flex的简写属性
flex:1;
即(flex-basis:0%; flex-grow:1; flex-shrink:1;)
等分布局:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#wrap{
width: 400px;
height: 300px;
border: 1px solid;
margin: 100px auto;
display: flex;
}
#wrap > .item{
height: 50px;
background: pink;
text-align: center;
line-height: 50px;
flex-shrink: 1;
flex-grow: 1;
flex-basis: 0;
}
</style>
</head>
<body>
<div id="wrap">
<div class="item">1</div>
<div class="item">22</div>
<div class="item">333</div>
<div class="item">4444</div>
<div class="item">55555</div>
</div>
</body>
</html>
效果图:
flex案例(简单淘宝导航界面)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
a{
text-decoration: none;
color: gray;
display: block;
}
#nav > .row{
display: flex;
}
#nav > .row > .item{
flex: 1;
text-align: center;
}
#nav > .row > .item > a:before{
content: "";
display: block;
width: 86px;
height: 86px;
margin: 0 auto;
}
#nav > .row:nth-child(1) > .item:nth-child(1) > a:before{
background: url(img/01.png) no-repeat;
}
#nav > .row:nth-child(1) > .item:nth-child(2) > a:before{
background: url(img/02.png) no-repeat;
}
#nav > .row:nth-child(1) > .item:nth-child(3) > a:before{
background: url(img/03.png) no-repeat;
}
#nav > .row:nth-child(1) > .item:nth-child(4) > a:before{
background: url(img/04.png) no-repeat;
}
#nav > .row:nth-child(1) > .item:nth-child(5) > a:before{
background: url(img/05.png) no-repeat;
}
#nav > .row:nth-child(2) > .item:nth-child(1) > a:before{
background: url(img/06.png) no-repeat;
}
#nav > .row:nth-child(2) > .item:nth-child(2) > a:before{
background: url(img/07.png) no-repeat;
}
#nav > .row:nth-child(2) > .item:nth-child(3) > a:before{
background: url(img/08.png) no-repeat;
}
#nav > .row:nth-child(2) > .item:nth-child(4) > a:before{
background: url(img/09.png) no-repeat;
}
#nav > .row:nth-child(2) > .item:nth-child(5) > a:before{
background: url(img/10 .png) no-repeat;
}
</style>
</head>
<body>
<div id="nav">
<div class="row">
<div class="item">
<a href="javascript:;">天猫</a>
</div>
<div class="item">
<a href="javascript:;">聚划算</a>
</div>
<div class="item">
<a href="javascript:;">天猫国际</a>
</div>
<div class="item">
<a href="javascript:;">外卖</a>
</div>
<div class="item">
<a href="javascript:;">天猫超市</a>
</div>
</div>
<div class="row">
<div class="item">
<a href="javascript:;">充值中心</a>
</div>
<div class="item">
<a href="javascript:;">飞猪旅行</a>
</div>
<div class="item">
<a href="javascript:;">领金币</a>
</div>
<div class="item">
<a href="javascript:;">拍卖</a>
</div>
<div class="item">
<a href="javascript:;">分类</a>
</div>
</div>
</div>
</body>
</html>
效果图:
⑦css3媒体查询选择器
@media 媒体类型 and(关键字) 带条件的媒体属性 and 带条件的媒体属性 {
//规则
}
- 媒体类型
all
screen:只有彩色屏幕的时候,才会有作用
print:只有打印的时候,才会有作用
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#wrap{
width: 100px;
height: 100px;
border: 1px solid;
margin: 100px auto;
}
@media screen {
/*规则*/
#wrap{
border: 10px solid;
}
}
</style>
</head>
<body>
<div id="wrap">
</div>
</body>
</html>
- 媒体属性
-
width:浏览器窗口的尺寸(min、max前缀)
min-width:800px; >=800px
max-width:800px; <=800px -
device-width:设备独立像素(min、max前缀)
pc端:分辨率
移动端:具体看机器的参数 -
device-pixel-ratio(必须加webkit前缀)
pc端:1
移动端:具体看机器的参数 -
横竖屏切换:
orientation:landscape(横屏)
orientation:portrait(竖屏)
- 关键字
only:处理浏览器尽量问题
and:连接一条查询规则,代表“与”的意思
,
:连接多条查询规则,代表“或”的意思
not :取反
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#wrap{
width: 100px;
height: 100px;
border: 1px solid;
margin: 100px auto;
}
@media only screen and (-webkit-device-pixel-ratio:2) and (orientation:landscape){
/*规则*/
#wrap{
border: 10px solid;
}
}
</style>
</head>
<body>
<div id="wrap">
</div>
</body>
</html>
css2媒体查询
示例:只有在打印时才能显示出来
index.css语句:
#wrap{
width: 100px;
height: 100px;
margin: 100px auto;
border: 10px solid;
}
html语句:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="index.css" media="print"/>
</head>
<body>
<div id="wrap">
</div>
</body>
</html>
⑧多列布局(分栏布局)
column-width:指定每一栏的宽度
column-count:指定要多少栏
column-gap:栏目距离
column-rule:栏目间隔线
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#wrap{
border: 1px solid;
width: 600px;
margin: 0 auto;
/*column-width: 200px;*/
column-count: 2;
column-gap: 30px;
column-rule: 1px solid red;
}
</style>
</head>
<body>
<div id="wrap">
锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。<br />
锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。<br />
锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。<br />
锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。<br />
锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。<br />
锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。<br />
锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。<br />
锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。<br />
锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。<br />
锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。<br />
锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。锄禾日当午,汗滴禾下土。谁之盘中餐?粒粒皆辛苦。<br />
</div>
</body>
</html>
效果图:
less
less是一种动态样式语言,属于CSS预处理器的范畴,它扩展了 CSS 语言,增加了变量、Mixin、函数等特性,使 CSS 更易维护和扩展。less既可以在 客户端 上运行 ,也可以借助Node.js在服务端运行。
①less中的注释
- 以
//
开头的注释,不会被编译到css文件中 - 以
/**/
包裹的注释会被编译到css文件中
②less中的变量
使用@来申明一个变量:@color:pink;
- 作为普通属性值只来使用:直接使用@color
- 作为选择器和属性名:#@{selector的值}的形式
less语句:
@color:pink;
@m:margin;
@selector:wrap;
*{
@{m}: 0;
padding: 0;
}
#@{selector}{
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
.inner{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background: @color;
height: 100px;
width: 100px;
}
}
- 作为URL:@{url}
- 变量的延迟加载
例:
less语句:
@var:0;
.class{
@var:1;
.brass{
@var:2;
three:@var; //3
@var:3;
}
one:@var; //1
}
CSS语句:
.class {
one: 1;
}
.class .brass {
three: 3;
}
③less中的嵌套规则
- 基本嵌套规则
- &的使用
less语句:
@color:pink;
*{
margin: 0;
padding: 0;
}
#wrap{
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
.inner{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background: @color;
height: 100px;
width: 100px;
&:hover{
background: deeppink;
}
}
}
所对应的CSS语句:
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background: #ffc0cb;
height: 100px;
width: 100px;
}
#wrap .inner:hover {
background: deeppink;
}
④less中的混合
混合是什么?
混合就是将一系列属性从一个规则集引入到另一个规则集的方式。
普通混合(编译到原生css中)
*{
margin: 0;
padding: 0;
}
#wrap{
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
.inner{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background: pink;
height: 100px;
width: 100px;
}
.inner2{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background: pink;
height: 100px;
width: 100px;
}
}
不带参数混合(加括号)
.juzhong(){
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background: pink;
height: 100px;
width: 100px;
}
*{
margin: 0;
padding: 0;
}
#wrap{
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
.inner{
.juzhong;
}
.inner2{
.juzhong;
}
}
带参数的混合
.juzhong(@w,@h,@c){
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background: @c;
height: @h;
width: @w;
}
*{
margin: 0;
padding: 0;
}
#wrap{
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
.inner{
.juzhong(100px,100px,pink);
}
.inner2{
.juzhong(200px,200px,deeppink);
}
}
带默认值的混合
.juzhong(@w:10px,@h:10px,@c:pink){
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background: @c;
height: @h;
width: @w;
}
*{
margin: 0;
padding: 0;
}
#wrap{
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
.inner{
.juzhong(100px,100px,pink);
}
.inner2{
.juzhong();
}
}
命名参数
less语句:
.juzhong(@w:10px,@h:10px,@c:pink){
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background: @c;
height: @h;
width: @w;
}
*{
margin: 0;
padding: 0;
}
#wrap{
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
.inner{
.juzhong(100px,100px,pink);
}
.inner2{
.juzhong(@c:black);
}
}
所对应的css语句:
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background: #ffc0cb;
height: 100px;
width: 100px;
}
#wrap .inner2 {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background: #000000;
height: 10px;
width: 10px;
}
匹配模式
示例:画三角形
triangle.less语句:
.triangle(@_){
width: 0px;
height: 0px;
overflow: hidden;
}
.triangle(L,@w,@c){
border-width: @w;
border-style:dashed solid dashed dashed;
border-color: transparent @c transparent transparent;
}
.triangle(R,@w,@c){
border-width: @w;
border-style:dashed dashed dashed solid;
border-color: transparent transparent transparent @c;
}
.triangle(T,@w,@c){
border-width: @w;
border-style:dashed dashed solid dashed;
border-color:transparent transparent @c transparent;
}
.triangle(B,@w,@c){
border-width: @w;
border-style: solid dashed dashed dashed;
border-color: @c transparent transparent transparent;
}
less的应用语句:
@import "./triangle.less";
#wrap .sjx{
// .triangle();
.triangle(R,40px,yellow);
}
html语句:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/03匹配模式.css"/>
</head>
<body>
<div id="wrap">
<div class="sjx">
</div>
</div>
</body>
</html>
效果图:
⑤less计算
- 在less中可以进行加减乘除的运算
- 计算的双方,只需要有一方带单位
⑥less继承
示例:
juzhong-extend.less语句:
.juzhong{
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto;
}
.juzhong:hover{
background: red!important;
}
extend.less语句:
*{
margin: 0;
padding: 0;
}
@import "mixin/juzhong-extend.less";
#wrap{
position: relative;
width: 300px;
height: 300px;
border: 1px solid;
margin: 0 auto;
.inner{
&:extend(.juzhong all);
&:nth-child(1){
width: 100px;
height: 100px;
background: pink;
}
&:nth-child(2){
width: 50px;
height: 50px;
background: deeppink;
}
}
}
extend.css语句:
* {
margin: 0;
padding: 0;
}
.juzhong,
#wrap .inner {
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto;
}
.juzhong:hover,
#wrap .inner:hover {
background: red!important;
}
#wrap {
position: relative;
width: 300px;
height: 300px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner:nth-child(1) {
width: 100px;
height: 100px;
background: pink;
}
#wrap .inner:nth-child(2) {
width: 50px;
height: 50px;
background: deeppink;
}
extend.html语句:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/extend.css"/>
</head>
<body>
<div id="wrap">
<div class="inner">
inner1
</div>
<div class="inner">
inner2
</div>
</div>
</body>
</html>
效果图:hover的时候颜色变为红色
⑦less避免编译
less语句:
*{
margin: 100 *10px;
padding: ~"cacl(100px + 100)";
}
所对应的CSS语句:
* {
margin: 1000px;
padding: cacl(100px + 100);
}