flex & less

1 篇文章 0 订阅

flex

  1. 项目永远排列在主轴的正方向上
  2. flex分新旧两个版本
    ① -webkit-box
    ② -webkit-flex/flex
  3. 我们都知道新版本的flex要比老版本的flex强大很多,有没有必要学习老版本的flex?
    移动端开发者必须要关注老版本的flex,因为很多移动端设备内核低只认老版本的flex 。
  4. 老版本的box通过两个属性四个属性值控制了主轴的位置和方向
  5. 新版本的flex通过一个属性四个属性值控制了主轴的位置和方向

①老版本

容器

  1. 容器的布局方向
    -webkit-box-orient控制主轴是哪一根
    属性值:
    horizontal(水平的)
    vertical(垂直的)
  2. 容器的排列方向
    -webkit-box-orient 控制主轴的方向
    属性值:
    normal
    reverse
  3. 富裕空间管理
    只决定富裕空间的位置,不会给项目区分配空间
    主轴
    属性:-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)

②新版本

容器

  1. 容器的布局方向
  2. 容器的排列方向
    属性:flex-direction:控制主轴是哪一根,控制主轴的方向
属性值效果
row从左往右的 x轴
row-reverse从右往左的 x轴
column从上往下的y轴
column-reverse从下往上的y轴
  1. 富裕空间管理
    只决定富裕空间的位置,不会给项目区分配空间
    主轴
    属性:justify-content
属性值效果
flex-start在主轴的正方向
flex-end在主轴的反方向
center在项目两边
justify在项目之间
space-between在项目之间
space-around在项目两边

侧轴
属性:align-items

属性值效果
flex-start在侧轴的正方向
flex-end在侧轴的反方向
center在项目两边
baseline基线对齐
stretch等高布局(项目没有高度)

项目

弹性空间管理:
flex-grow:弹性因子 (默认值为0)

③新版本新增

容器

  1. flex-wrap:控制的是侧轴的方向
属性值效果
nowrap不换行,没有侧轴
wrap侧轴方向由上而下 (flex-shrink失效)
wrap-reverse侧轴方向由下而上 (flex-shrink失效)
  1. align-content:多行多列时,侧轴空间的管理(把多行多列看成一个整体)

  2. flex-flow:flex-direction和flex-wrap的简写属性
    本质上控制了主轴和侧轴分别是哪一根,以及他们的方向

项目

  1. order:控制项目的排列顺序
  2. align-self:项目自身富裕空间的管理
  3. flex-shrink:收缩因子(默认值为 1 )
  4. flex-basis:伸缩规则计算的基准值(默认拿width和height的值)

④伸缩规则

  • flex-basis(默认值为auto)

  • flex-grow(默认值为0)

      	 可用空间 = (容器大小 - 所有相邻项目flex-basis的总和)
      	 可扩展空间 = (可用空间/所有相邻项目flex-grow的总和)
      	 每项伸缩大小 = (伸缩基准值(flex-basis) + (可扩展空间 x flex-grow值))
    
  • flex-shrink (默认值为1)

  1. 计算收缩因子与基准值乘的总和
    var a = 每一项flex-shrink*flex-basis之和
  2. 计算收缩因数
    收缩因数=(项目的收缩因子项目基准值)/第一步计算总和
    var b = (flex-shrink
    flex-basis) / a
  3. 移除空间的计算
    移除空间= 项目收缩因数 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 带条件的媒体属性 {
//规则
}

  1. 媒体类型
    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>

  1. 媒体属性
  • 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(竖屏)

  1. 关键字
    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;

  1. 作为普通属性值只来使用:直接使用@color
  2. 作为选择器和属性名:#@{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;
    }
}
  1. 作为URL:@{url}
  2. 变量的延迟加载
    例:
    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中的嵌套规则

  1. 基本嵌套规则
  2. &的使用

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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值