Less.js String % format() Function
Last Updated :
26 Apr, 2025
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. Since CSS is a dynamic style sheet language, it is preferred. Because LESS is adaptable, it can be used by a variety of browsers. Only CSS that has been created and processed using the CSS pre-processor, a computer language, can be used by web browsers. In addition to providing capabilities like variables, functions, mixins, and operations to help create dynamic CSS while maintaining backward compatibility, it is a CSS language extension.
In this article, we are going to discuss the string % format() function, this function is to format a string by putting in arguments that are passed into it. This function takes a string value and returns the escaped version of the string content without the quotes.
Syntax:
%(string, arguments...)
Parameters:
- string: This is the first compulsory parameter for this function which needs to be a string inside quotes.
- arguments: This is the second compulsory parameter for this function where the arguments are passed which are placed into the format given in the first string.
Placeholders:
- d, D, a, A: These are interchangeable with any argument (color, number, escaped value, expression, ...). When combined with a string, they will use the entire string, including any quotations. However, the quotes are not escaped by "/" or any other character, they are just included in the string as is.
- s, S: They can be changed with any other expression. If you use it with a string, the quotations are omitted and only the string value is used.
Return type: This method returns a formatted String.
Example 1: The code below demonstrates the usage and implementation of the string % format() function and the usage of if() and boolean logical functions and to show any proper functionality we will also use the String e() function.
HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css"
href="style.css" />
</head>
<body>
<h1 style="color:green">
GeeksforGeeks
</h1>
<h3><b>Less.js String % format() Function</b></h3>
<div class="c1">
<p>e(%("%a%s", 4+5, "rem"));
</p>
<p>9rem</p>
</div>
</body>
</html>
styles.less
CSS
@body-bg-color: #eeeeee;
@dark: hsl(25, 71%, 8%);
@light: rgb(253, 255, 146);
@str: %("%a%s", 4+5, "rem");
@estr: e(@str);
body {
background-color: @body-bg-color;
}
.c1 {
width: 300px;
height: @estr;
margin: 1rem;
background-color: @light;
padding: 40px 0px 0px 75px;
}
p {
color: @dark;
}
Now, to compile the above LESS code to CSS code, run the following command:
lessc styles.less styles.css
The compiled CSS file comes to be:
style.css: The CSS output of the above Less file was compiled.
CSS
body {
background-color: #eeeeee;
}
.c1 {
width: 300px;
height: 9rem;
margin: 1rem;
background-color: #fdff92;
padding: 40px 0px 0px 75px;
}
p {
color: hsl(25, 71%, 8%);
}
Output:
Example 2: The code below demonstrates the usage and implementation of the string % format() function and the usage of isstring() and ispercentage() type functions and to show any proper functionality we will also use the String e() function.
HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css"
href="style.css"/>
</head>
<body>
<h1 style="color:green">
GeeksforGeeks
</h1>
<h3><b>Less.js String % format() Function</b></h3>
<div class="c1">
<p>boolean(isstring(@str));<br>TRUE</p>
<p>boolean(ispercentage(@eStr))<br>FALSE</p>
</div>
</body>
</html>
styles.less
CSS
@body-bg-color: #eeeeee;
@dark: hsl(25, 71%, 8%);
@light: rgb(253, 255, 146);
@str: %("%a%s", 20+8, "%");
@estr: e(@str);
@cond1: boolean(isstring(@str));
@cond2: boolean(ispercentage(@estr));
body {
background-color: @body-bg-color;
}
.c1 {
width: @estr;
height: 150px;
margin: 1rem;
background-color: if(@cond1, @dark, @light);
padding: 40px 0px 0px 55px;
}
p {
color: if(@cond2, @dark, @light);
}
Now, to compile the above LESS code to CSS code, run the following command:
lessc styles.less styles.css
The compiled CSS file comes to be:
style.css: The CSS output of the above Less file was compiled.
CSS
body {
background-color: #eeeeee;
}
.c1 {
width: 28%;
height: 150px;
margin: 1rem;
background-color: hsl(25, 71%, 8%);
padding: 40px 0px 0px 55px;
}
p {
color: #fdff92;
}
Output:
Reference: https://lesscss.org/functions/#string-functions--format
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side : On client sid
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read