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.

HTML file: simple.html

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.    <link rel="stylesheet" href=" C:\ \simple.css" type="text/css" />  
  5.    <title>Changing Selector Order Example</title>  
  6. </head>  
  7. <body>  
  8.   <div class="header">  
  9.     <div class="menu">  
  10.       <h2>JavaTpoint: A solution of all technology.</h2>  
  11.       <p>It is possible to reference the parent selector by using &(ampersand) operator.</p>  
  12.     </div>  
  13.   </div>  
  14. </body>  
  15. </html>  

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

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

Less Changing selector order parent selector1

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

For example:

Less Changing selector order parent selector2

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. }   

Output:

Less Changing selector order parent selector3