Less CEP Selectors

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

Combinatorial Explosion Parent Selectors

The & operator can be used to produce all possible permutation of selectors in a list separated by commas.

Combinatorial Explosion Example

Let's take an example to demonstrate the usage of combinatorial explosion parent selectors.

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

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="simple.css" type="text/css" />  

  <title>Combinatorial Explosion Example</title>  



<p>This is first paragraph.</p>  

<p>This is second paragraph which is adjacent to first paragraph ( i.e. p + p ). This will be highlighted.</p>  


This div is adjacent to second paragraph ( i.e. p + div ). This will be highlighted.  


<p>This is third paragraph adjacent to div ( i.e. p + div ). This will be highlighted.</p>  

<i>This is italic. This will not be highlighted since there is no (p + i) in CSS</i>  

<div>This is second div</div>  

<div>This is div adjacent to second div ( i.e. div + div ). This will be highlighted</div>  



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


div {  

  color: red;  

  font-family: Lucida Console;  


p + p,  

p + div,  

div + p,  

div + div {  

  color: brown;  

  background-color: aqua;  

  font-family: "Comic Sans MS";  

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. p,  

  2. div {  

  3.   color: red;  

  4.   font-family: Lucida Console;  

  5. }  

  6. p + p,  

  7. p + div,  

  8. div + p,  

  9. div + div {  

  10.   color: brown;  

  11.   background-color: aqua;  

  12.   font-family: "Comic Sans MS";  

  13. }