less基础

语法

定义变量

body {
  padding:0;
  margin:0;
}

@color:blue;   //定义一个color变量
@size:3px;
@nice-blue: #5B83AD;
@light-blue: @nice-blue + #111;

.box {
  width: 500px;
  height: 300px;
  border:1px solid blue;
  .one {
    height: @size * 50 ;   //使用变量 50*3 = 150
    color:red;
    border: @size solid red;  //3px solid blue
  }
}

编译之后

body {
  padding: 0;
  margin: 0;
}
.box {
  width: 500px;
  height: 300px;
  border: 1px solid blue;
}
.box .one {
  height: 150px;
  color: red;
  border: 3px solid red;
}

伪类

.clearfix {
  display: block;
  zoom: 1;

  &:after {
    content: " ";
    display: block;
    font-size: 0;
    height: 0;
    clear: both;
    visibility: hidden;
  }

}

编译之后

.clearfix {
  display: block;
  zoom: 1;
}
.clearfix:after {
  content: " ";
  display: block;
  font-size: 0;
  height: 0;
  clear: both;
  visibility: hidden;
}

局部变量

@clr:red;

.box {
  @clr:blue;
  color:@clr;
}

.one {
  background-color: @clr;
}

@var: red;

#page {
  #header {
    color: @var; // white
  }
  @var: white;
}

编译之后

.box {
  color: blue;
}
.one {
  background-color: red;
}

运算

@base: 2cm * 3mm; // result is 6cm

@base: 5%;
@filler: @base * 2; // result is 10%
@other: @base + @filler; // result is 15%

// 数字会转换为相同的单位
@conversion-1: 5cm + 10mm; // result is 6cm
@conversion-2: 2 - 3cm - 5mm; // result is 1.5cm

// 单位转换
@incompatible-units: 2 + 5px - 3cm; // result is 4px

引入文件

@import "library"; // library.less
@import "typo.css";

继承

### &写法

@base: #f04615;
@width: 0.5;

.box {
  width: percentage(@width); // returns `50%`
  color: saturate(@base, 5%);
  background-color: spin(lighten(@base, 25%), 8);
}

.one {
  &:extend(.box);
  color:red;
}

编译

.box , .one {
  width: 50%;
  color: #f6430f;
  background-color: #f8b38d;
}
.one {
  color: red;
}

:写法

.box {
  color:blue;
  font-size: 22px;
}

.one:extend(.box) {
  background-color: pink;
}

编译

.box , .one {
  color: blue;
  font-size: 22px;
}
.one {
  background-color: pink;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值