CSS preprocessing Less and Sass summary

Insert picture description here
Insert picture description here
less is written based on nodejs

sass is written in ruby, and the node-sass package is used to solve the compilation problem

Insert picture description here
Variables in
Insert picture description here
less are defined in mixin@less. Block
Insert picture description here
use:
Insert picture description here
lessccommand to compile less files. Method:
Insert picture description here
"mixin" attribute can only be based on existing style rules! You can use class selector and id selector in mixin:

.a,#b{
    
    
    color: rebeccapurple;
}
.mixin-class{
    
    
    .a();
}
.mixin-id{
    
    
    #b();
}

Output:

.a,
#b {
    
    
  color: rebeccapurple;
}
.mixin-class {
    
    
  color: rebeccapurple;
}
.mixin-id {
    
    
  color: rebeccapurple;
}

Note that when calling mixin, parentheses are optional:

.a()  //此种写法和下面写法无区别
.a;

Mixin that does not output :

If you want to create a mixin, but you don't want the mixin to output in the generated css, you can add parentheses () after it:

.my-mixin{
    
    
    color: black;
}
.my-other-mixin(){
    
    
    background: wheat;
}
.class{
    
    
    .my-mixin;
    .my-other-mixin;
}

Output:

.my-mixin {
    
    
  color: black;
}
//此处并没有输出 .my-other-mixin
.class {
    
    
  color: black;
  background: wheat;
}

lessNeutral extend:

Here is a block called mixin:
Insert picture description here
If multiple places in less are quoted mixin:
Insert picture description here

Then there will be a lot of repetitive codes in the final generated css:
Insert picture description here
if there are a lot of mixinmiddle codes, then the final generated repetitive codes will be more, we obviously don’t want this,solution:

Use extend
Insert picture description here
Result: The
common style is extracted in one place a
Insert picture description here
separate style there
Insert picture description here

lessThe loopcycle

Look at an example, the principle is recursion: the
Insert picture description here
result:
Insert picture description here
Insert picture description here

less modular

Insert picture description here

Variables in sass use $

Insert picture description here

The mixinwording of sass is different from less:

Insert picture description here

First of all, sass mixinmust be displayed with a @mixindeclaration, and the other mixinname is not preceded by a dot. For example, here is
@mixin block, which cannot be written as @mixin
. The mixin in block Less looks like both a class and a mixin.

When using sass, you need to add@include

Insert picture description here

node-sassHow to use command-line compilation

Insert picture description here

sassIn, use extend like this:

Insert picture description here

sassThe loopcycle

sass writes a grid layout recursively. Note that sass is used for quoting variables #{$n}, and less is used for quoting variables.@{n}
Insert picture description here
Insert picture description here

but! ! ! ! ! ! ! ! ! ! ! ! ! ! !

In fact, sass doesn't have to be so troublesome, because sass supports for loops!
Insert picture description here

sass modular

Insert picture description here

to sum up:

Insert picture description here

CSS preprocessor framework

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/dyw3390199/article/details/113864093