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 are available in Less.js, which can be used to perform various operations related to color, images, data, units, etc for the CSS code only.
1. color: This method is used to parse a string to the color if it already represents a color.
Syntax:
color("#ababab);
Parameter:
- string: It accepts a string of any specific color.
2. image-size: This method is used to find out the dimensions of the image from a file.
Syntax:
image-size("gfg.png");
Parameter:
- string: It accepts the file name as a string in order to return the dimensions.
3. image-width: This method also works as an image-size method but it only returns the width of the image from a file.
Syntax:
image-width("gfg.png");
Parameter:
- string: It accepts the file name as a string to get the width of the image.
4. image-height: This method is used to get the image height from a file.
Syntax:
image-height("gfg.png");
Parameter:
- string: It accepts the file name of an image to get the height of the image.
5. convert: This method is used to convert the unit of a number. It supports various unit groups for example lengths, time, and angle.
Syntax:
convert(100cm, mm);
Parameter:
- number: it accepts the real number along with units. Note: number without a unit will not be valid at all.
- units: it will be the second parameter in which the number is going to be converted.
6. data-uri: This method is used to inline the resources and put them back into the url() method and the type of that data is also included with the MIME type package that node usage.
Syntax:
data-uri("gfg.png");
Parameters:
- url: The URL of the file.
- mimetype: The type of data as a string. (optional)
7. default: If this method is available only inside the mixin guard conditions then it will return only true if does not match with any other mixin, otherwise it will return false.
Syntax:
.mixin(<mixin-parameter>) when default() )
{
...
}
Parameter:
- It does not accept any parameter.
8. unit: This method is used to change or remove the unit of a dimension.
Syntax:
unit(15px);
Parameter:
- dimension: It accepts the number regardless of the dimensions.
- unit: It also accepts the unit, if there is a need to change the existing unit to another.
9. get-unit: This method is used to get the unit of the number if it is passed with the unit included otherwise it will return nothing.
Syntax:
get-unit("10vh");
Parameter:
- number: It will accept the number regardless of the unit.
10. svg-gradient: This method is used to generate the multi-stop gradient which basically means it will generate gradients with more than two colors,
Syntax:
background-image: svg-gradient(to bottom, blue, skyblue 20%, green);
Parameter:
- direction: It accepts the direction as a first parameter as to right, to bottom, to bottom right, etc.
- first-color: Accepts the color in pairs and the percentage.
- last-color: The last color to complete the gradient.
Example 1: In this example, we are going to use the svg-gradient() method to generate the multi-stop gradient.
index.html:
HTML
<!DOCTYPE html>
< head >
< title >Svg-gradient</ title >
< style >
div
{
background-image: url(
'data:image/svg+xml,%3Csvg%20xmlns%3D%22https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttp%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201%201%22%3E%3ClinearGradient%20id%3D%22g%22%20x1%3D%220%25%22%20y1%3D%220%25%22%20x2%3D%22100%25%22%20y2%3D%220%25%22%3E%3Cstop%20offset%3D%220%25%22%20stop-color%3D%22%230000ff%22%2F%3E%3Cstop%20offset%3D%22100%25%22%20stop-color%3D%22%2387ceeb%22%2F%3E%3Cstop%20offset%3D%22100%25%22%20stop-color%3D%22%230000ff%22%2F%3E%3C%2FlinearGradient%3E%3Crect%20x%3D%220%22%20y%3D%220%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22url(%23g)%22%20%2F%3E%3C%2Fsvg%3E');
height: 500px;
width: 500px;
}
</ style >
</ head >
< body >
< h1 style = "color: green;" >GeeksforGeeks</ h1 >
< div >
</ div >
</ body >
</ html >
|
style.less:
CSS
div
{
@list: red , green 30% , blue ;
background-image : svg-gradient(to right , blue , skyblue 100% , blue );
height : 500px ;
width : 500px ;
}
|
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
div
{
background-image : url (
'data:image/svg+xml,%3Csvg%20xmlns%3D%22https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttp%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201%201%22%3E%3ClinearGradient%20id%3D%22g%22%20x1%3D%220%25%22%20y1%3D%220%25%22%20x2%3D%22100%25%22%20y2%3D%220%25%22%3E%3Cstop%20offset%3D%220%25%22%20stop-color%3D%22%230000ff%22%2F%3E%3Cstop%20offset%3D%22100%25%22%20stop-color%3D%22%2387ceeb%22%2F%3E%3Cstop%20offset%3D%22100%25%22%20stop-color%3D%22%230000ff%22%2F%3E%3C%2FlinearGradient%3E%3Crect%20x%3D%220%22%20y%3D%220%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22url(%23g)%22%20%2F%3E%3C%2Fsvg%3E' );
height : 500px ;
width : 500px ;
}
|
Output:
Example 2: In this example, we are going to use the color method, we will pass a string of a specified color and in the result, we can see that it returns as the color.
index.html:
HTML
<!DOCTYPE html>
< head >
< title >Color method</ title >
< style >
div
{
background-color: #abcabc;
height: 500px;
width: 500px;
}
</ style >
</ head >
< body >
< h1 style = "color:green;" >GeeksforGeeks</ h1 >
< div >
</ div >
</ body >
</ html >
|
style.less:
CSS
div
{
background-color : color( "#abcabc" );
height : 500px ;
width : 500px ;
}
|
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
div
{
background-color : #abcabc ;
height : 500px ;
width : 500px ;
}
|
Output:
Reference: https://lesscss.org/functions/#misc-functions
Similar Reads
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.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
Less.js Math Functions
In this article, we will see the various Math functions that are provided by Less.js to perform various mathematical functions as per the user's requirements within CSS code only. Less.js (Leaner Style Sheets) is an extension to normal CSS which basically enhances the abilities of normal CSS and giv
6 min read
Less.js Logical Functions
In this article, we will see the Logical Functions in LESS.js. Logical functions are a way to perform operations and evaluate code based on logical conditions. Basically, these functions provide the power of "if-else" conditions in CSS and help us do things based on logic. There are 2 logical functi
3 min read
Less.js String Functions
Less.js (Leaner Style Sheets) is an extension to normal CSS which basically enhances the abilities of the normal CSS and gives it programmable powers like variables, functions, loops, etc. String functions are one such type of function provided by Less.js to perform operations on a string like repla
5 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 default() 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 convert() 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, thus it functions with a variety of browsers. Web browsers can only use CSS that has
4 min read
Less.js Type Functions
In this article, we will see the Type Functions provided by Less.js. Less.js (Leaner Style Sheets) is an extension to normal CSS which basically enhances the abilities of normal CSS and gives it superpowers. Basically, Type Functions are used to check whether a particular argument (provided to the f
6 min read
Less.js Math min() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. Because CSS is a dynamic style sheet language, it was chosen. Because LESS is adaptable, it works with a wide range of browsers. Only CSS that has been written and
3 min read