Open In App

Less.js Mixins

In this article, we will see the Mixins in LESS, along with understanding their implementation & usage through the example. A CSS preprocessor is a program that generates the CSS code by using the defined preprocessor ruleset & facilitates reducing the overall complexity of CSS code. LESS is a CSS preprocessor that is a CSS backward-compatible language extension. Mixins in LESS, are a way to include a bunch of CSS properties from one ruleset to another i.e multiple times in the same file.

Syntax:



.bordered-box{
 background: lightgreen;
}
.class {
 .bordered-box();
}

Syntax:

.bordered_box(){
   &:hover{
      background-color: black;
  }
}
div {
    .bordered_box();
}

Syntax:



#hello() {
   background:black;
   .inner {
      color: blue;
   }
}

C {
   #hello > .inner;
}

Syntax:

#namespace when (@color = black) {
  .mixin() {  }
}

#namespace {
  .mixin() when (@color = black) {  }
}

Syntax:

.bordered-box(@width: 2;@color: red){
    border:@width solid blue;
   
}

#one-image{
    .bordered-box() !important;
}

Syntax:

.bordered-box(@width){
    border:@width solid blue;
    padding: @width * 4;
}

Syntax:

.mixin(dark; @color) {
   color: darken(@color, 15%);
}

.mixin(light; @color) {
   color: lighten(@color, 15%);
}

@color-new: dark;

.line {
   .mixin(@color-new; #FF0000);
}

Syntax:

.average(@a, @b) {
  @result: ((@a + @b) / 2);
}

div {
    padding: .average(3px, 5px)[@result];
}

Syntax:

.while(@a) when (@a >0 ) and (@a < 11){
    .while((@a  - 1));   
    width: (1px * @a); 
  }
  
  Hello GeeksforGeeks {
    .while(10); 
  }

Syntax:

.mixin(@a) when (lightness(@a) >= 50%) {
  background-color: blue;
}
.mixin(@a) when (lightness(@a) < 50%) {
  background-color: white;
}
.mixin(@a) {
  color: @a;
}

Syntax:

#school() {
  .colors() {
    background: blue;
  }
}
.box {
  @alias: #school.colors();
  @alias();
}

Example 1: This example describes the basic usage of the LESS.js Mixins. Here, we can call the below mixin, just like other mixins, and at the time of calling we pass the values to the mixins arguments.




.bordered-box(@width) {
    border:@width solid blue;
    padding: @width * 4;
    color:green;
}
  
.para {
    .bordered-box(12px);
}

When the above LESS code is compiled  then it results in the following CSS code:




.para {
    border: 12px solid blue;
    padding: 48px;
    color: green;
}




<!DOCTYPE html>
<html>
  
<head>
    <link rel="stylesheet" 
          href="para.css" 
          type="text/css" />
    <title>LESS Mixins</title>
</head>
  
<body>
    <div class="para">
        <h1>GeeksForGeeks</h1>
        <h2>We are learning mixin in less</h2>
    </div>
</body>
  
</html>

Output: When you run the HTML code then you will get the output which is shown like this:

 

Example 2: This is another example that demonstrates the use of mixins in the LESS.js.




.hello {
    color:blue;
}
   
.h22 {
    background : #64d9c0;
    .hello();
}
   
.h3 {
    background :red;
    .hello;
}

To compile the less code to CSS code write the following command:

lessc hello.less hello.css

The following Compiled CSS code will be generated:




.hello {
    color: blue;
}
.h22 {
    background: #64d9c0;
    color: blue;
}
.h3 {
    background: red;
    color: blue;
}




<!DOCTYPE html>
<html>
  
<head>
    <link rel="stylesheet" href="hello.css" type="text/css" />
    <title>LESS Mixins</title>
</head>
  
<body>
    <h1 class="hello">GeeksForGeeks</h1>
    <h2 class="h22">We are learning mixin in less</h2>
    <h3 class="h3">
        LESS is a dynamic style sheet language 
        that extends the capability of CSS.
    </h3>
</body>
  
</html>

Output:

 

Reference:https://lesscss.org/features/#mixins-feature


Article Tags :