LESS Tutorial on LESS CSS Guards

description

guards are used to match simple values or a number of arguments on expressions. it is applied to the css selectors. it is syntax for declaring mixin and calling it immediately. to successfully bring out the if type statement; join this with feature &, which allows you to group multiple guards.

example

the following example demonstrates the use of css guard in the less file −

css_guard.htm

<!doctype html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
   </head>

   <body>
      <div class = "cont">
         <h2>welcome to tutorialspoint</h2>
      </div>
		
      <div class = "style">
         <h3>the largest tutorials library on the web.</h3>
      </div>
   </body>
</html>

next, create the style.less file.

style.less

@usedscope: global;
.mixin() {
   @usedscope: mixin;
   .cont when (@usedscope = global) {
      background-color: red;
      color: black;
   }
   
   .style when (@usedscope = mixin) {
      background-color: blue;
      color: white;
   }
   @usedscope: mixin;
}
.mixin();

you can compile the style.less file to style.css by using the following command −

lessc style.less style.css

execute the above command; it will create the style.css file automatically with the following code −

style.css

.style {
   background-color: blue;
   color: white;
}

output

follow these steps to see how the above code works −

  • save the above html code in the css_guard.htm file.

  • open this html file in a browser, the following output will get displayed.

less css guard