Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Less.js Detached Rulesets Property / variable accessors
Next article icon

Less.js Variables Properties as Variables

Last Updated : 23 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 the web browser can use it. 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.

Properties as variables: In the properties as variables (NEW), You can easily treat properties like variables using the $prop syntax. Sometimes this can make your code a little lighter.

You have to note that, like variables, Less will choose the last property within the current/parent scope as being the "final" value.

Syntax:

color:$color;

Example 1: The following examples, demonstrate the use of Less.js variable properties as variables. First, we have to create an HTML file with the following code.

HTML
<!DOCTYPE html>   <head>       <link rel="stylesheet"            href="style.css" type="text/css" />   </head>   <body>      <div><br><br>         <h1 class="color">GeeksforGeeks</h1>           <h2><b>Less.js Variables Properties as Variables</b></h2>         <h3>Thank you!!!...</h3>     </div> </body>   </html>   

style.less: Create the less file with the following code.

CSS
@color:green; @text:black;  .color  {     color: @color;     text-align: center; } h2  {     color: @text;     text-align: center; } h3  {     color:$color;     text-align:$text-align;     .color(); } 

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
.color {     color: green;     text-align: center; } h2 {     color: black;     text-align: center; } h3 {     color: green;     text-align: center; } 

Output:

 

Example 2: The following examples demonstrate the use of Less.js variable properties as variables. In this example, when the cursor moves to the upside then the background changes on hover.

HTML
<!DOCTYPE html>   <head>       <link rel="stylesheet"            href="style.css" type="text/css" />   </head>   <body>      <div><br><br>         <h1 class="block">GeeksforGeeks</h1>           <h2><b>Less.js Variables Properties as Variables</b></h2>         <h3 class="block .inner">Thank you!!!...</h3>     </div> </body>   </html>   

style.less: Create the less file with the following code.

CSS
@text:green; @bg:black; .block  {     color: @bg;      .inner      {       background-color: $color;      }     color: @text;      width: 2px dashed blue;      height: 30px solid green; }  body {     &:hover     {            background-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
.block {     color: black;       color: green;       width: 2px dashed blue;       height: 30px solid green; } .block .inner  {       background-color: green; } body:hover  {       background-color: black; } 

Output:

2


Reference: https://lesscss.org/features/#variables-feature-properties-as-variables-new-


Next Article
Less.js Detached Rulesets Property / variable accessors

K

kumarbalit8
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • LESS
  • Less.js-Functions

Similar Reads

  • Less.js Variables Default Variables
    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 s
    3 min read
  • Less.js Variables Overview
    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
    2 min read
  • Less.js Maps-Using variable variables in lookups
    LESS is a preprocessor of CSS that provide it with some more features to work and with the help of that, we can write more efficient CSS code. Let us have brief information about the lookups, it's just a pair of capital brackets [@lookups], the content inside the bracket is called lookups which are
    2 min read
  • Less.js Detached Rulesets Property / variable accessors
    LESS is a simple CSS pre-processor that makes it possible to create manageable, customizable, and reusable style sheets for websites. LESS is a dynamic style sheet language that increases the working power of CSS. LESS is cross-browser compatible. CSS pre-processor is a scripting language that impro
    3 min read
  • 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 s
    3 min read
  • Less.js Variables Changing Selector Order
    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 s
    3 min read
  • How to represent a variable in Less ?
    LESS stands for Leaner Style Sheets. It is a backward-compatible language extension for CSS. One of its features is that it lets you use variables in the style sheet. Variables can be used to store CSS values that may be reused. This makes it easier for the user by letting them define commonly used
    2 min read
  • Less.js Options Rewrite URLs
    Less.js is a CSS pre-processor that allows developers to use variables, mixins, and other programming concepts in their CSS code. One of the useful features of Less.js is the ability to rewrite URLs in CSS, which can make it easier to manage and maintain large projects. This can be useful when migra
    8 min read
  • Less.js Browser Modify Variables
    LESS (Leaner Style Sheets)  is an extension or superset to the CSS, which basically enhances the abilities of normal CSS and provides it with programmable superpowers like variables, functions, loops, various methods to do certain tasks, etc. In this article, we are going to take a look at updating
    3 min read
  • Less.js Variables
    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 s
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences