Less.js Misc get-unit() 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. LESS is adaptable, so it works with a wide range 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 Misc get-unit() function, its utility is to find the unit of a number that is passed into the function. This function returns the unit if the number has a unit and nothing if the number doesn’t contain a unit.
Syntax:
get-unit(number)
Parameters:
- number: The only compulsory parameter for this function and needs to be a number with or without a unit.
Please refer to the Compile LESS code into CSS code. article for a detailed description of the installation procedure.
Example 1: The code below demonstrates the usage and implementation of the Misc get-unit() function and uses it with the ispixel() function with the if() and boolean logical functions.
HTML
<!DOCTYPE html>
< html >
< head >
< link rel = "stylesheet"
type = "text/css"
href = "style.css" />
</ head >
< body >
< h1 style = "color:green" >GeeksforGeeks</ h1 >
< h3 >Less.js Misc get-unit() Function</ h3 >
< div class = "c1" >
< p >@uni: get-unit(@no)
< br >boolean(ispixel(@uni))
< br >TRUE
</ p >
</ div >
</ body >
</ html >
|
styles.less:
CSS
@body-bg- color : #eeeeee ;
@no: 250px ;
@uni: get-unit(@no);
@color 1: #95f3e7 ;
@color 2: #4b1905 ;
@cond: boolean(ispixel(@uni));
body {
background-color : @body-bg-color;
}
.c 1 {
width : 500px ;
height : @no;
background-color : if(@cond, @color 1 , @color 2 );
margin : 1 rem;
}
p {
color : if(@cond, @color 2 , @color 1 );
font-size : 1.5 rem;
padding : 5 rem 0 0 4.5 rem;
}
|
Now, to compile the above LESS code to CSS code, run the following command:
lessc styles.less styles.css
styles.css: The compiled CSS file comes to be:
CSS
body {
background-color : #eeeeee ;
}
.c 1 {
width : 500px ;
height : 250px ;
background-color : #4b1905 ;
margin : 1 rem;
}
p {
color : #95f3e7 ;
font-size : 1.5 rem;
padding : 5 rem 0 0 4.5 rem;
}
|
Output:
Example 2: The code below demonstrates the usage and implementation of the Misc get-unit() function and uses it with the isstring() function and the string % format() function.
HTML
<!DOCTYPE html>
< html >
< head >
< link rel = "stylesheet"
type = "text/css"
href = "style.css" />
</ head >
< body >
< h1 style = "color:green" >GeeksforGeeks</ h1 >
< h3 >Less.js Misc get-unit() Function</ h3 >
< div class = "c1" >
< p >
@uni: get-unit(@no)
< br >@estr: e(%("%a%d", 250, @uni));
< br >height: 250px
</ p >
</ div >
</ body >
</ html >
|
styles.less:
CSS
@body-bg- color : #eeeeee ;
@no: 250px ;
@uni: get-unit(@no);
@str: %( "%a%d" , 250 , @uni);
@estr: e(@str);
@color 1: #95f3e7 ;
@color 2: #4b1905 ;
@cond: boolean(isstring(@uni));
body {
background-color : @body-bg-color;
}
.c 1 {
width : 500px ;
height : @estr;
background-color : if(@cond, @color 2 , @color 1 );
margin : 1 rem;
}
p {
color : if(@cond, @color 1 , @color 2 );
font-size : 1.5 rem;
padding : 5 rem 0 0 4.5 rem;
}
|
Now, to compile the above LESS code to CSS code, run the following command:
lessc styles.less styles.css
styles.css: The compiled CSS file comes to be:
CSS
body {
background-color : #eeeeee ;
}
.c 1 {
width : 500px ;
height : 250px ;
background-color : #95f3e7 ;
margin : 1 rem;
}
p {
color : #4b1905 ;
font-size : 1.5 rem;
padding : 5 rem 0 0 4.5 rem;
}
|
Output:
Reference: https://lesscss.org/functions/#misc-functions-get-unit
Similar Reads
Less.js Misc image-width() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is preferable since CSS is a dynamic style sheet language. Since LESS is flexible, it can be used with many different browsers. Web browsers can only use CSS th
3 min read
Less.js Misc image-size() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. Given that CSS is a dynamic style sheet language, it is preferable. LESS is adaptable enough to work with a variety of browsers. Only CSS that has been created and
3 min read
Less.js Misc Functions
Less.js is a CSS preprocessor which basically means that it provides some additional features to the traditional CSS which helps us to write CSS code more efficiently. All the Misc functions are explained below. In this article, we are going to learn about the Misc or Miscellaneous functions that ar
4 min read
Less.js Misc data-uri() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is an extension of normal CSS which basically enhances the abilities of normal CSS and gives superpowers to it. LESS.js provides the built-in Math function that
3 min read
Less.js List Functions
Less.js (Leaner Style Sheets) is an extension to normal CSS which basically enhances the abilities of the normal CSS code and gives it more functionalities and programmable features. Less.js provides us with many List Functions which are basically functions that we can perform on a list of elements
7 min read
Less.jsMisc unit() Function
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. LESS is adaptable, so it works with a wide range of browsers. Only CSS that has been created and proc
3 min read
Less.js Misc image-height() Function
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. LESS is adaptable, so it works with a wide range of browsers. Only CSS that has been created and proc
3 min read
Less.js Math round() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is an extension to normal CSS which basically enhances the abilities of normal CSS and gives superpowers to it. LESS.js provides the built-in Math function that
3 min read
Less.js Math pi() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is chosen because CSS is a dynamic style sheet language. LESS is flexible, it works with a variety of browsers. Web browsers can only use CSS that has been writ
3 min read
Less.js Misc color() Function
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. LESS is adaptable, so it works with a wide range of browsers. Only CSS that has been created and proc
3 min read