Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptLess.js Variables Multiple & Selector

Less.js Variables Multiple & Selector

LESS (Leaner Style Sheets) is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is a dynamic style sheet language that enhances the working power of CSS. LESS supports cross-browser compatibility. CSS pre-processor is a scripting language that improves CSS and gets compiled into regular CSS syntax so that it can be used by the web browser. It is also a backward-compatible language extension for CSS that provides functionalities like variables, functions, mixins, and operations that enable us to build dynamic CSS.

Description for variables: The variables in LESS.js govern the common values used in a single location, ie, they are known to keep values stored in them and can be used anywhere within the definition of code. LESS allows you to use these variables to change the specific value in the entire code. It might get troublesome when we need to change one particular value in our CSS code, but with the help of variables, it can easily be done.

Multiple & Parent selector:

In Multiple & selector, the & operator is used to refer to a parent selector repeatedly without using its name. So the multiple & parent selector specifies that a selector & operator can be used more than once. It can be useful to prepend a selector to the inherited (parent) selectors. This can be done by putting the & after the current selector. For example, when using Modernizr, you might want to specify different rules based on supported features:

Syntax:

.link 
{
      & + & {
      }
}

Example 1: The following examples demonstrates the use of  Less.js Variables Multiple & Selector in the Less file.

HTML




<!DOCTYPE html>  
<html>  
<head>  
    <title>Multiple & Example</title>  
    <link rel="stylesheet" href="style.css" 
          type="text/css" />  
</head>  
<body>  
    <h1 class="link">
      Welcome to neveropen!!</h1>  
    <h3 class="linksh"><b>
      Less.js Variables Multiple & Selector</b></h3>
</body>  
</html>


style. less: Create the Less file.

CSS




@rd: green;
@blue: blue;
@bg: cyan;
  
.link {
    & + & {
        color: @bg;
    }
    & & {
        color: @blue;
    }
    && {
        color: @rd;
    }
    &,
    &ish {
        color: @bg;
    }
}


Now, to compile the above LESS code to CSS code, run the following command:

lessc style.less style.css

The compiled CSS file comes to be: 

style.css:

CSS




.link + .link {
    color: cyan;
}
.link .link {
    color: blue;
}
.link.link {
    color: green;
}
.link,
.linkish {
    color: cyan;
}


Output:

 

Example 2: The following examples demonstrates the use of  Less.js Variables Multiple & Selector in the Less file.

HTML




<!DOCTYPE html>  
<html>  
<head>  
   <link rel="stylesheet" href="style.css" 
         type="text/css" />  
   <title>Changing Selector Order Example</title>  
</head>  
<body>  
    <div class="header">  
        <div class="menu">  
            <h1>neveropen</h1>  
            <h3>Less.js Variables Multiple & Selector</h3>  
        </div>  
    </div>  
</body>  
</html>


style. less:  Create the Less file.

CSS




@color: red;
@bg: blue;
@rd: green;
.header {
    .menu {
        border-radius: 1px;
        border: 5px solid @bg;
        & {
            padding-left: 200px;
        }
    }
}
h1 {
    color: @rd;
    text-align: center;
}
h3 {
    color: @color;
    text-align: center;
}


Now, to compile the above LESS code to CSS code, run the following command:

lessc style.less style.css

The compiled CSS file comes to be: 

style.css

CSS




.header .menu {
    border-radius: 1px;
    border: 5px solid blue;
    padding-left: 200px;
}
h1 {
    color: green;
    text-align: center;
}
h3 {
    color: red;
    text-align: center;
}


Output:

 

Reference: https://lesscss.org/features/#parent-selectors-feature-multiple-

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments