How to introduce less

1. Introduce les.js to the page

<script src="/ / cdnjs.cloudflare.com/ajax/libs/less.js/2.7.2/less.min.js"></script>

<link rel="stylesheet/less" href="style.less">
<script src="less.min.js"></script>
Copy the code

2. Install NPM from the command line

npm install -g less
$ lessc styles.less > styles.css
Copy the code

Second, the variable

1. Value variable@Define variables at the beginning and type directly when you use them@The name.

/* Less */
// The following defines variables, which will be used later
@color: #999;
@bgColor: skyblue; // Do not add quotes
@width: 50%;
#wrap {
  color: @color; // Introduce the above custom variables
  background: @bgColor;
  width: @width;
}
// The above is essentially the same as the following
/* Generated CSS */
#wrap {
  color: #999;
  background: skyblue;
  width: 50%;
}
Copy the code

Selector variables: Make the selector dynamic

/* Less */
@mySelector: #wrap;
@Wrap: wrap;
@{mySelector}{ // Variable names must be wrapped in braces
  color: #999;
  width: 50%;
}
.@{Wrap}{
  color:#ccc;
}

/* Generated CSS */
#wrap{
  color: #999;
  width: 50%;
}
.wrap{
  color:#ccc;
}
Copy the code

3. Attribute variables

/* Less */
@borderStyle: border-style;
@Soild:solid;
#wrap{
  @{borderStyle}: @Soild; // Variable names must be wrapped in braces
}

/* Generated CSS */
#wrap{
  border-style:solid;
}
Copy the code

4. Url variables

/* Less */
@images: ".. /img";// Quotes are required
body {
  background: url("@{images}/dog.png");// Variable names must be wrapped in braces
}

/* Generated CSS */
body {
  background: url(".. /img/dog.png");
}
Copy the code

5. Declare variables

/* Less */
@background: {background:red; }; #main{ @background(); }/* Generated CSS */
#main{
  background:red;
}
Copy the code

6. Variable operation

/* Less */
@width:300px;
@color:#222;
#wrap{
  width:@width-20;
  height:@width-20*5;
  margin:(@width-20) *5;
  color:@color*2;
  background-color:@color + #111;
}

/* Generated CSS */
#wrap{
  width:280px;
  height:200px;
  margin:1400px;
  color:#444;
  background-color:#333;
}
Copy the code

7. Scope of variables (Proximity principle)

/* Less */
@var: @a;
@a: 100%;
#wrap {
  width: @var;
  @a: 9%;
}

/* Generated CSS */
#wrap {
  width: 9%;
}
Copy the code

8. Use variables to define variables

/* Less */
@fnord:  "I am fnord.";
@var:    "fnord";
#wrap::after{
  content: @ @var; // Replace @var with its value content:@fnord;
}
/* Generated CSS */
#wrap::after{
  content: "I am fnord.";
}
Copy the code

Second, the nesting

1, & : represents the name of the selector at the previous level

/* Less */
#header{
  &:after{ // The & stands for #header
    content:"Less is more!";
  }
  .title{
    font-weight:bold;
  }
  &_content{// Replace & with #header
    margin:20px; }}/* Generated CSS */
#header::after{
  content:"Less is more!";
}
#header .title{ / / nested
  font-weight:bold;
}
#header_content{// No nesting!
    margin:20px;
}
Copy the code

2, media query in the past we are the style and media query CCTV written separately, now only need to write as follows:

/* Less */
#main{
    //something...
    @media screen{
        @media (max-width:768px){
          width:100px;
        }
    }
    @media tv {
      width:2000px; }}/* Generated CSS */
@media screen and (maxwidth:768px){
  #main{
      width:100px; 
  }
}
@media tv{
  #main{
    width:2000px; }}Copy the code

3. Mixed methods

1. No parameter method

/* Less */
.card { // equivalent to.card()
    background: #f6f6f6;
    box-shadow: 0 1px 2px rgba(151.151.151.. 58);
}
#wrap{
  .card();// equivalent to.card();
}
/* Generated CSS */
#wrap{
  background: #f6f6f6;
  box-shadow: 0 1px 2px rgba(151.151.151.. 58);
}
Copy the code

2. Default parameter methods

/* Less */
.border(@a:10px,@b:50px,@c:30px,@color:#000) {// The unit must be passed in the argument.
    border:solid 1px @color;
    box-shadow: @arguments;// Refer to all parameters
}
#main{
    .border(0px,5px,30px,red);// You must bring the unit
}
#wrap{
    .border(0px);
}
#content{
  .border;// equivalent to.border()
}

/* Generated CSS */
#main{
    border:solid 1px red;
    box-shadow:0px,5px,30px,red;
}
#wrap{
    border:solid 1px #000;
    box-shadow: 0px 50px 30px #000;
}
#content{
    border:solid 1px #000;
    box-shadow: 10px 50px 30px #000;
}
Copy the code

3. Matching mode of method

/* Less */
.triangle(top,@width:20px,@color:#000){
    border-color:transparent  transparent @color transparent ;
}
.triangle(right,@width:20px,@color:#000){
    border-color:transparent @color transparent  transparent ;
}

.triangle(bottom,@width:20px,@color:#000){
    border-color:@color transparent  transparent  transparent ;
}
.triangle(left,@width:20px,@color:#000){
    border-color:transparent  transparent  transparent @color;
}
.triangle(@_,@width:20px,@color:#000){
    border-style: solid;
    border-width: @width;
}
#main{
    .triangle(left, 50px, #999)}/* Generated CSS */
#main{
  border-color:transparent  transparent  transparent #999;
  border-style: solid;
  border-width: 50px;
}
Copy the code

4. Method namespaces

/* Less */
#card(){
    background: #723232;
    .d(@w:300px){
        width: @w;
      
        #a(@h:300px){
            height: @h;// You can use the method passed in from the previous layer
        }
    }
}
#wrap{
    #card > .d > #a(100px); // The parent element cannot be parenthesized
}
#main{
    #card .d();
}
#con{
    // Do not use namespace methods alone
    //.d() will report an error if the namespace #card is not introduced
    
    #card; // equivalent to #card();
    .d(20px); // You must first introduce #card
}
/* Generated CSS */
#wrap{
  height:100px;
}
#main{
  width:300px;
}
#con{
  width:20px;
}
Copy the code

5, the method of condition screening: comparison operation: > >= = =< <

/* Less */
#card{
    
    The and operator, the equivalent of &&, is executed only if all conditions are met
    .border(@width,@color,@style) when (@width>100px) and(@color=#999){
        border:@style @color @width;
    }

    // Not operator, equivalent to non-operation! Is executed only when the conditions are not met
    .background(@color) when not (@color>=#222) {background:@color;
    }

    / /, comma separator: is equal to or operation | |, as long as there is a qualified will be executed
    .font(@size:20px) when (@size>50px) , (@size<100px){
        font-size: @size;
    }
}
#main{
    #card>.border(200px,#999,solid);
    #card .background(#111);
    #card > .font(40px);
}
/* Generated CSS */
#main{
  border:solid #999 200px;
  background:#111;
  font-size:40px;
}
Copy the code

6, the number of variable parameters:… Like the extension operator in ES6

/* Less */.boxShadow(...) { box-shadow: @arguments; } .textShadow(@a,...) { text-shadow: @arguments;
}
#main{
    .boxShadow(1px,4px,30px,red);
    .textShadow(1px,4px,30px,red);
}

/* Generated CSS */
#main{
  box-shadow: 1px 4px 30px red;
  text-shadow: 1px 4px 30px red;
}
Copy the code

7, method use! important

/* Less */
.border{
    border: solid 1px red; margin: 50px; } #main{ .border() ! important; }/* Generated CSS */
#main {
    border: solid 1px red ! important; margin: 50px ! important; }Copy the code

8. Circular method

/* Less */
.generate-columns(4);

.generate-columns(@n, @i: 1) when (@i =< @n) {
  .column-@{i} {
    width: (@i * 100% / @n);
  }
  .generate-columns(@n, (@i + 1));
}
/* Generated CSS */
.column-1 {
  width: 25%;
}
.column-2 {
  width: 50%;
}
.column-3 {
  width: 75%;
}
.column-4 {
  width: 100%;
}
Copy the code

9. Attribute splicing method: +_ represents space; The + stands for comma.

  • The comma
/* Less */
.boxShadow() {
    box-shadow+: inset 0 0 10px #555;
}
.main {
  .boxShadow();
  box-shadow+: 0 0 20px black;
}
/* Generated CSS */
.main {
  box-shadow: inset 0 0 10px #555.0 0 20px black;
}
Copy the code
  • The blank space
/* Less */
.Animation() {
  transform+_: scale(2);
}
.main {
  .Animation();
  transform+_: rotate(15deg);
}

/* Generated CSS */
.main {
  transform: scale(2) rotate(15deg);
}
Copy the code

Four, inheritance,

1. Use of the extend keyword

/* Less */
.animation{
    transition: all .3s ease-out;
    .hide{
      transform:scale(0);
    }
}
#main{
    &:extend(.animation);
}
#con{
    &:extend(.animation .hide);
}

/* Generated CSS */
.animation,#main{
  transition: all .3s ease-out;
}
.animation .hide , #con{
    transform:scale(0);
}

Copy the code

2. All Global search and replace

/* Less */
#main{
  width: 200px;
}
#main {
  &:after {
    content:"Less is good!";
  }
}
#wrap:extend(#main all) {}

/* Generated CSS */
#main,#wrap{
  width: 200px;
}
#main:after, #wrap:after {
    content: "Less is good!";
} 
Copy the code

Five, import,

  1. You can omit the suffix when importing less files
import "main"; 
/ / equivalent to the
import "main.less";
Copy the code
  1. @importThe position can be placed at will
#main{
  font-size:15px;
}
@import "style";
Copy the code

3, the reference

Use @import (reference) to import external files, but do not add to compile the imported files into the final output, only reference them.

/* Less */
@import (reference) "bootstrap.less"; 
#wrap:extend(.navbar all){}
Copy the code

4, once

Default behavior for @import statements. This indicates that the same file will only be imported once, and that duplicate code from subsequent imported files will not be parsed.

@import (once) "foo.less";
@import (once) "foo.less"; // this statement will be ignored
Copy the code

5, multiple

Using @import (multiple) allows you to import multiple files with the same name.

/* Less */

// file: foo.less
.a {
  color: green;
}
// file: main.less
@import (multiple) "foo.less";
@import (multiple) "foo.less";

/* Generated CSS */
.a {
  color: green;
}
.a {
  color: green;
}
Copy the code

Six, functions,

1. Determine the type

  • Isnumber () : Determines whether the given value is a number.
isnumber(blue);     // false
isnumber("string"); // false
isnumber(1234);     // true
isnumber(56px);     // true
isnumber(7.8%);     // true
isnumber(keyword);  // false
Copy the code
  • Iscolor () : Determines whether the given value is a color.
  • Surl () : Determines whether the given value is a URL.

2. Color operation

  • Saturate (): Increases color saturation by a certain amount.
  • Lighten (): Increases color brightness by a certain amount.
  • Darken (): Reduces the color brightness by a certain amount.
  • Fade (): Gives the color a certain amount of transparency.
  • Mix (): Mix two colors in proportion.

3. Mathematical functions

  • Ceil () : rounded up.
  • Floor () : round down.
  • Percentage () : converts a floating point number to a percentage string.
  • Round () : round.
  • SQRT () : Calculates the square root of a number.
  • Abs () : calculates the absolute value of the number, keeping the unit as is.
  • Pow () : Calculates the power of a number.

Seven, other

1, comments,

  • /* */ CSS native comments, which are compiled in CSS files.
  • / / Less provides a comment that is not compiled in CSS files.

2, avoid compiling: ~’ value ‘

/* Less */
#main{
  width: ~'calc(300px-30px)';
}

/* Generated CSS */
#main{
  width:calc(300px-30px);
}
Copy the code

3, variable string: ~” character @{variable} character “;

.judge(@i) when(@i=1){
  @size:15px;
}
.judge(@i) when(@i>1){
  @size:16px;
}
.loopAnimation(@i) when (@i<16) {
  
  .circle:nth-child(@{i}){
      .judeg(@i);
      border-radius:@size @size 0 0;
      animation: ~"circle-@{i}" @duration infinite @ease;
      transition-delay:~"@{i}ms";
  }
  @keyframes ~"circle-@{i}" {
      // do something...
  }
  .loopAnimation(@i + 1);
}
Copy the code

4. Use JS nested

/* Less */
@content:`"aaa".toUpperCase()`;
#randomColor{
  @randomColor: ~"rgb(`Math.round(Math.random() * 256)`,`Math.round(Math.random() * 256)`,`Math.round(Math.random() * 256)`)";
}
#wrap{
  width: ~"`Math.round(Math.random() * 100)`px";
  &:after{
      content:@content;
  }
  height: ~"`window.innerHeight`px";
  alert:~"`alert(1)`";
  #randomColor();
  background-color: @randomColor;
}
/* Generated CSS */

/ / pop up 1
#wrap{
  width: Random value (0~100) the px; height: 743px;// Depending on the computerBackground: random color; } #wrap::after{content:"AAA";
}
Copy the code

5, override the original component library component style: /deep/” class name (selector)”

/deep/.ant-skeleton-content {
    background-color: #fff;
    padding: 12px 24px;
}
Copy the code

Read other people’s articles, moved to their own notes to use, if there is infringement, please contact me, I will delete, thank you.Original author: SimonMa