文章目录
less注释
less的注释 //
css的注释是/**/,css注释也可以在less中起作用,不同的是css注释会被编译到css文件中,而less注释(//)不会被编译到css文件中。
即,在less中:
以//开头的注释,不会被编译到css文件中
以/**/包裹的注释会被编译到css文件中
eg:
less文件:
/*这是想暴露出去的注释*/
// 这是见不得人的注释
对应的css文件:
/*这是想暴露出去的注释*/
less变量
变量的定义
变量的定义格式:@变量名:变量值;
变量的调用:@变量名
eg:
less:
/*这是想暴露出去的注释*/
// 这是见不得人的注释
@color:pink;
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
.inner {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: @color;
height: 100px;
width: 100px;
}
}
css:
/*这是想暴露出去的注释*/
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 100px;
width: 100px;
}
/*# sourceMappingURL=./index.css.map */
注意: 变量的作用域是块级作用域,即一个{}代表一个作用域。
属性值或选择器做变量
变量的定义格式:@变量名:变量值;
变量的调用:@{变量名}
less:
/*这是想暴露出去的注释*/
// 这是见不得人的注释
@color:pink;
@m:margin;
@selector:#wrap;
* {
@{m}: 0;
padding: 0;
}
@{selector} {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
@{m}: 0 auto;
.inner {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: @color;
height: 100px;
width: 100px;
}
}
对应的css
/*这是想暴露出去的注释*/
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 100px;
width: 100px;
}
/*# sourceMappingURL=./index.css.map */
变量作为url
变量的定义格式:@url:url地址;
变量的调用:@{url}
变量的延时加载
即需要将其他语句都执行完,最后加载变量的具体值。
eg:
less
@var: 0;
.class{
@var: 1;
.brass{
@var: 2;
three:@var;
@var: 3;
}
one:@var
}
对应的css
.class {
one: 1;
}
.class .brass {
three: 3;
}
less:块级作用域,一个{}代表一个作用域。
three: 3;的值是3不是2,先将@var的值加载完成之后再赋值,这就是变量的延时调用。
less嵌套规则
普通嵌套:父子关系,两个选择器之间自动添加空格
&:代表外层父元素,代表对氟元素进行操作
eg:
less
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
.inner {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 100px;
width: 100px;
&:hover {
background-color: aqua;
}
}
}
css
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 100px;
width: 100px;
}
#wrap .inner:hover {
background-color: aqua;
}
/*# sourceMappingURL=./初始less.css.map */
less混合(minix)
混合的作用:避免大量重复代码。
即如果有两个类有相同的样式,如果重复写就会造成很多重复代码,使用混合可以将重复代码提取,谁需要谁再进行调用即可。(调用的过程就是ctrl+c 和ctrl+v的过程)
混合就是将一系列属性从一个规则集引入到另一个规则集的方式
普通混合
混合定义:
在less规范中有明确的指定,使用.定义
.混合名{
混合内容
}
混合调用:.混合名
eg:
less
.center{
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 100px;
width: 100px;
}
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
.inner {
.center;
}
.inner2{
.center;
}
}
对应的css
.center {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 100px;
width: 100px;
}
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 100px;
width: 100px;
}
#wrap .inner2 {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 100px;
width: 100px;
}
/*# sourceMappingURL=./01.css.map */
不带输出的混合
上述代码.center也会被编译成css,即html中的元素可以使用类center的样式。但是有些情况下我们只希望它做混合用,不希望它被编译成css代码,我们只需要在混合名后面加上()即可,这样css中就不会有.center了。
这就是不带输出的混合。
注意如果混合加(),调用的时候应该也加()。(如果括号内没有参数,调用的时候加不加括号是一样的但是还是加上吧)
eg:
less
.center(){
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 100px;
width: 100px;
}
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
.inner {
.center();
}
.inner2{
.center();
}
}
对应的css
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 100px;
width: 100px;
}
#wrap .inner2 {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 100px;
width: 100px;
}
/*# sourceMappingURL=./01.css.map */
带参数的混合
()中可以传参数,格式有点像函数,但是他不是函数。
eg:
less
.center(@h,@w,@color){
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: @color;
height: @h;
width: @w;
}
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
.inner {
.center(100px,100px,pink);
}
.inner2{
.center(200px, 200px, pink);
}
}
对应的css
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 100px;
width: 100px;
}
#wrap .inner2 {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 200px;
width: 200px;
}
/*# sourceMappingURL=./01.css.map */
参数带默认值的混合
参数可以设置默认值,格式:@参数名:参数默认值
less:
.center(@h:100px,@w:100px,@color:pink){
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: @color;
height: @h;
width: @w;
}
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
.inner {
.center();
}
.inner2{
.center(200px, 200px, deeppink);
}
}
对应css
* {
margin: 0;
padding: 0;
}
#wrap {
position: relative;
width: 300px;
height: 400px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
height: 100px;
width: 100px;
}
#wrap .inner2 {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: deeppink;
height: 200px;
width: 200px;
}
/*# sourceMappingURL=./01.css.map */
命名参数
如果混合有多个参数但是我们只想给其中一个指定的参数传参就需要用到命名参数了。
即给指定名字的参数传值。
eg:
less:
.center(@h:100px,@w:100px,@color:pink){
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: @color;
height: @h;
width: @w;
}
#wrap {
.inner {
.center(@color:yellow);
}
.inner2{
.center(200px, 200px, deeppink);
}
}
css:
#wrap .inner {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: yellow;
height: 100px;
width: 100px;
}
#wrap .inner2 {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background-color: deeppink;
height: 200px;
width: 200px;
}
/*# sourceMappingURL=./01.css.map */
匹配模式
匹配模式就相当于是同名混合使用不同的参数,调用不同的混合函数。
想要通过匹配模式更灵活的调用混合。(有点像java中的重载)
规则:
- 第一个参数一般代表匹配模式。
即不同的参数根据第一个参数决定调用哪一个混合 - 如果混合的第一个参数为
@_,那么其他的与之同名的混合被调用的时候都会先调用该混合。
(注意: 其他的同名的混合可以自动调用该混合还有一个前提就是:该混合的参数与自动调用该混合的混合的参数个数是一样的,这样才能匹配到)
eg:
triangle.less
.triangle(@_,@w,@c){
width: 0;
height: 0;
overflow: hidden;
}
.triangle(L,@w, @c) {
border-width: @w;
border-style: dashed solid dashed dashed;
border-color: transparent @c transparent transparent;
}
.triangle(T,@w, @c) {
border-width: @w;
border-style: dashed dashed solid dashed;
border-color: transparent transparent @c transparent;
}
.triangle(R,@w, @c) {
border-width: @w;
border-style: dashed dashed dashed solid;
border-color: transparent transparent transparent @c;
}
.triangle(B,@w, @c) {
border-width: @w;
border-style: solid dashed dashed dashed;
border-color: @c transparent transparent transparent;
}
03.less
@import "./triangle.less";
#wrap .sjx {
.triangle(L,40px,red);
}
03.css
#wrap .sjx {
width: 0;
height: 0;
overflow: hidden;
border-width: 40px;
border-style: dashed solid dashed dashed;
border-color: transparent red transparent transparent;
}
/*# sourceMappingURL=./03.css.map */
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./03.css">
</head>
<body>
<div id='wrap'>
<div class="sjx"></div>
</div>
</body>
</html>
输出:

arguments
arguments装的是实参伪数组。
伪数组:具有length属性的对象。
eg:
less
.border(@1,@2,@3){
border: @arguments;
// 等价于border: @1 @2 @3;
}
#wrap .sjx {
.border(1px,solid,black);
}
对应的css
#wrap .sjx {
border: 1px solid black;
}
/*# sourceMappingURL=./04.css.map */
less运算
在less中可以进行加减乘除的运算
less:
#wrap .sjx {
width: (100+100px);
// 计算双方只需要带单位
}
css
#wrap .sjx {
width: 200px;
}
/*# sourceMappingURL=./05.css.map */
less继承
继承的产生:
问题的产生,只用混合可能会造成重复代码。
eg:
center.less
.center(@w,@h,@c){
position:absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto;
width: @w;
height: @h;
background-color: @c;
}
minix.less
@import "./center.less";
*{
padding: 0;
margin: 0;
}
#wrap{
position: relative;
width: 300px;
height: 300px;
border: 1px solid;
margin: 0 auto;
.inner{
&:nth-child(1) {
.center(200px,200px,pink);
}
&:nth-child(2) {
.center(100px, 100px, yellow);
}
}
}
minix.css
* {
padding: 0;
margin: 0;
}
#wrap {
position: relative;
width: 300px;
height: 300px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner:nth-child(1) {
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto;
width: 200px;
height: 200px;
background-color: pink;
}
#wrap .inner:nth-child(2) {
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto;
width: 100px;
height: 100px;
background-color: yellow;
}
/*# sourceMappingURL=./mixin.css.map */
wrap .inner:nth-child(1) 和 wrap .inner:nth-child(2)代码会有很多重复。
这就需要继承了。
继承
继承就是一个选择器将另一个类选择器的属性拿过来使用。
继承的格式:
定义:
.类选择器{
}
调用:
选择器:extend(.类选择器)
eg:
center2.less
.center{
position:absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto;
}
extend.less
@import "./center2.less";
* {
padding: 0;
margin: 0;
}
#wrap {
position: relative;
width: 300px;
height: 300px;
border: 1px solid;
margin: 0 auto;
.inner:extend(.center) {
&:nth-child(1) {
width: 200px;
height: 200px;
background-color: pink;
}
&:nth-child(2) {
width: 100px;
height: 100px;
background-color: yellow;
}
}
}
extend.css
.center,
#wrap .inner {
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto;
}
* {
padding: 0;
margin: 0;
}
#wrap {
position: relative;
width: 300px;
height: 300px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner:nth-child(1) {
width: 200px;
height: 200px;
background-color: pink;
}
#wrap .inner:nth-child(2) {
width: 100px;
height: 100px;
background-color: yellow;
}
/*# sourceMappingURL=./extend.css.map */
注意
继承不能传参数,即类选择器后面不能有()
如果加括号就不会被继承
eg:
center.less
.center(){
position:absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto;
}
extend.css
* {
padding: 0;
margin: 0;
}
#wrap {
position: relative;
width: 300px;
height: 300px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner:nth-child(1) {
width: 200px;
height: 200px;
background-color: pink;
}
#wrap .inner:nth-child(2) {
width: 100px;
height: 100px;
background-color: yellow;
}
/*# sourceMappingURL=./extend.css.map */
.center()样式没有被继承
all
默认情况下不会继承类选择器上的的伪类不会被继承(如:hover),但是如果集成的时候写上all就会全部继承。
格式: 加在类选择器名后面
eg: &:extend(.center all);
eg:
center2.less
.center{
position:absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto;
}
.center:hover{
background-color: red!important;
}
extend.less
@import "./center2.less";
* {
padding: 0;
margin: 0;
}
#wrap {
position: relative;
width: 300px;
height: 300px;
border: 1px solid;
margin: 0 auto;
.inner {
&:extend(.center all);
&:nth-child(1) {
width: 200px;
height: 200px;
background-color: pink;
}
&:nth-child(2) {
width: 100px;
height: 100px;
background-color: yellow;
}
}
}
extend.css
.center,
#wrap .inner {
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto;
}
.center:hover,
#wrap .inner:hover {
background-color: red!important;
}
* {
padding: 0;
margin: 0;
}
#wrap {
position: relative;
width: 300px;
height: 300px;
border: 1px solid;
margin: 0 auto;
}
#wrap .inner:nth-child(1) {
width: 200px;
height: 200px;
background-color: pink;
}
#wrap .inner:nth-child(2) {
width: 100px;
height: 100px;
background-color: yellow;
}
/*# sourceMappingURL=./extend.css.map */
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./extend.css">
</head>
<body>
<div id="wrap">
<div class="inner">
inner1
</div>
<div class="inner">
inner2
</div>
</div>
</body>
</html>
输出:

继承和混合对比
继承的性能比混合高,但是灵活度比混合低。
less避免编译
less会自动编译,如:如果传递的是一个计算式,less会直接计算出来
less:
*{
// 除法记得加括号
margin: 100*10px;
padding: cacl(200px+100);
}
对应的css:
* {
margin: 1000px;
padding: cacl(300px);
}
/*# sourceMappingURL=./text.css.map */
但是让雨果我们不希望less自动编译,而是交给浏览器进行编译,这就涉及到less的避免编译,即less不进行编译。
格式:~"避免编译的内容"
eg:
less:
*{
// 除法记得加括号
margin: 100*10px;
padding:~"cacl(200px+100)";
}
css:
* {
margin: 1000px;
padding: cacl(200px+100);
}
/*# sourceMappingURL=./text.css.map */
本文详细介绍了Less预处理器的基本特性,包括其特有的注释方式、变量定义与使用(包括属性值和选择器作为变量、变量的延时加载)、嵌套规则、混合功能(普通混合、不带输出的混合、带参数混合、参数带默认值的混合、命名参数混合)以及运算和继承。此外,还探讨了避免编译的技巧,展示了如何利用Less提高CSS代码的复用性和组织性。

701

被折叠的 条评论
为什么被折叠?



