Less CSOP Selector

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Changing Selector Order Parent Selector

It is useful to prepend a selector to the inherited (parent) selectors when selector ordering is changed. The & operator is placed after the selector to achieve this.

For example: when you use Modernizer, you may wish to specify different rules depending on supported features.

Changing Selector Order Example

Let's take an example to demonstrate the usage of changing selector order parent selectors.

Create a HTML file named "simple.html", having the following data.


Now create a file named "simple.less". It is similar to CSS file. The only one difference is that it is saved with ".less" extension.

LESS file: simple.less

  1. .header {  

  2.   .menu {  

  3.     border-radius: 5px;  

  4.     border: 1px solid red;  

  5.     & {  

  6.       padding-left: 200px;  

  7.     }  

  8.   }  

  9. }   

Put the both file "simple.html" and "simple.less" inside the root folder of Node.js

HTML file: simple.html

<!DOCTYPE html>  



   <link rel="stylesheet" href=" C: simple.css" type="text/css" />  

   <title>Changing Selector Order Example</title>  



  <div class="header">  

    <div class="menu">  

      <h2>JavaTpoint: A solution of all technology.</h2>  

      <p>It is possible to reference the parent selector by using &(ampersand) operator.</p>  





Now, execute the following code: lessc simple.less simple.css

This will compile the "simple.less" file. A CSS file named "simple.css" will be generated.

For example:

The generated CSS "simple.css", has the following code:

  1. .header .menu {  

  2.   border-radius: 5px;  

  3.   border: 1px solid red;  

  4.   padding-left: 200px;  

  5. }