Less 中的混合、避免编译以及继承

less中的混合

混合就是将一系列属性从一个规则集引入到另一个规则集的方式

  1. 普通混合:会被编译到CSS中

    .center{
    	position: absolute;
    	left: 0;
    	top: 0;
    	margin: auto;
    }
    
    #box1{
    	width: 100px;
    	height: 50px;
    	background: red;
    	.center;
    }
    #box2{
    	width: 50px;
    	height: 100px;
    	background: orange;
    	.center;
    }
    
  2. 不带输出的混合:不会被编译到CSS中

    .center(){
    	position: absolute;
    	left: 0;
    	top: 0;
    	margin: auto;
    }
    
    #box1{
    	width: 100px;
    	height: 50px;
    	background: red;
    	.center;
    }
    #box2{
    	width: 50px;
    	height: 100px;
    	background: orange;
    	.center;
    }
    
  3. 带参数的混合

    .center(@w,@h,@c){
    	position: absolute;
    	left: 0;
    	top: 0;
    	margin: auto;
    	width: @w;
    	height: @h;
    	background: @c;
    }
    
    #box1{
    	.center(100px,50px,red);
    }
    #box2{
    	.center(50px,100px,orange);
    }
    
  4. 带参数并且有默认值的混合

    .center(@w:200px,@h:100px,@c:blue){
    	position: absolute;
    	left: 0;
    	top: 0;
    	margin: auto;
    	width: @w;
    	height: @h;
    	background: @c;
    }
    
    #box1{
    	.center();
    }
    #box2{
    	.center(50px,100px,orange);
    }
    
    
  5. 带多个参数的混合

  6. 命名参数

    #box2{
    	// .center(orange); // 会被编译成 width: orange;
    	.center(@c:orange);
    }
    
  7. 匹配模式

    // triangle.less
    .triangle(@_,@w:10px,@c:pink){
    	width: 0;
    	height: 0;
    	overflow: hidden;
    }
    /*向上的三角*/
    .triangle(top,@w:10px,@c:pink){
    	border-width:@w;
    	border-color: transparent  transparent  @c transparent ;
    	border-style:dashed dashed solid dashed  ;
    }
    /*向下的三角*/
    .triangle(bottom,@w:10px,@c:pink){
    	border-width:@w;
    	border-color: @c transparent  transparent   transparent ;
    	border-style:solid dashed dashed  dashed  ;
    }
    /*向左的三角*/
    .triangle(left,@w:10px,@c:pink){
    	border-width:@w;
    	border-color:  transparent @c  transparent   transparent ;
    	border-style: dashed solid dashed  dashed  ;
    }
    /*向右的三角*/
    .triangle(right,@w:10px,@c:pink){
    	border-width:@w;
    	border-color:  transparent   transparent   transparent @c;
    	border-style: dashed  dashed  dashed  solid;
    }
    
    #box1 {
    	.triangle(right)
    }
    #box1 {
    	.triangle(top,0,red)
    }
    
  8. arguments变量

    .border(@1,@2,@3) {
    	border: @arguments;
    }
    
    .box {
    	.border(1px,solid,black)
    }
    

less继承

性能比混合高
灵活度比混合低
.common:hover{
    background: red;
}
.common{
    position: absolute;
    left: 0;
    top: 0;
    margin: auto;
}
.center(@w:100px,@h:100px,@c:pink){
    &:extend(.common all);
    width: @w;
    height: @h;
    background: @c;
}

备注:会将CSS代码编译成并集选择器的形式,后面的 all 可以将.common后的编译出来(比如.common:hover

less避免编译

~"需要避免编译的数据"

// 例如
.box {
	width:~"calc(200px + 700px)";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凡小多

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值