Description
The Less misc functions consists of the following group functions, they are:
Description
The string is used to represent the color.
Framework: string: a string to the specified color.
Returns: color
Example
[c]color( “#FFFFFF”);[/c]
Result
[c]#FFFFFF[/c]
Description
Used to get the image dimensions from a file.
Framework: string: the file to get the image dimensions.
Returns: dimension
Example
[c]image-size (“file.png”);[/c]
Result
[c]10px 10px[/c]
Description
Used to get the image height from the file.
Framework: string: the file to get the image dimensions for.
Returns: dimension
Example
[c]image-height (“file.png”);[/c]
Result
[c]10px[/c]
Description
Used to convert numbers from one unit to another unit.
Suppose when the first argument contains a number with units and the second argument contains only units. The number is converted only if the units are compatible, if not the argument is returned as unmodified.
The following are the compatible unit groups.
- length: m, cm, mm, in, pt and pc
- time: s and ms
- angle: rad, deg, grad and turn
Example
Below example explains the simple snippet code.
style.less
[c]
body{
meter: convert( 20cm, mm);
time: convert(4s, “ms”);
no-unit: convert(6, mm);
}
[/c]
Compile the code to generate the following CSS code.
style.css
[c]
body{
meter: 200mm;
time: 4000ms;
no-unit: 6;
}
[/c]
Description
The term uri stands for uniform resource identifier (URI). The data-uri is used to inline a resource in a web page. It is a MIME type string, node uses the mime package to determine the correct mime type when not given.
Framework:
url: inline the file URL.
mimetype: it is a MIME type string.
Example
[c]data-uri (‘../data/image.jpg’);[/c]
Result
[c]url (‘data: image/jpeg;base64,hbhasdyhHdrtdyGCtDYuUF==’);[/c]
Output in browser
[c] url (‘.../data/image.jpg’);[/c]
Description
The default function available in the guard condition returns true and does not match with any other mixins. If it matches, it returns false.
Example
Below example explains the misc default function in Less.
Create a simple less file as shown below.
style.less
[c]
mixin(1) {x: 15}
.mixin(2) {y: 20}
.mixin(@x) when (default()) {z: @x}
div {
.mixin(3);
}
div.special {
.mixin(1);
}
[/c]
Compile the above code using the command
[c]lessc style.less style.css[/c]
Result
style.css
[c]
div {
z: 3;
}
div.special {
x: 15;
}
[/c]
Description
Used to change or remove the dimension units. The absolute units such as inches, centimeters, points etc. are supported by Less.
Framework:
dimension: defines a number with or without a dimension.
unit: it removes or changes the unit (optional).
Example 1
[c]unit (7, px)[/c]
Result
[c]7px[/c]
Example 2
[c]unit (8em)[/c]
Result
[c]8[/c]
Description
This function is used to return units of a number. When the argument contains a number with unit, it returns units. If argument does not contain a unit, it returns an empty value.
Framework:
numbers: for a number with or without units.
Example 1
[c]get-unit (6px)[/c]
Result
[c]px[/c]
Example 2
[c]get-unit(5)[/c]
Result
[c]//nothing[/c]
Description
The svg-gradient function generates multi-stop svg gradients.
To generate multi-stop gradient, the function must have 3 parameters. One parameter indicates the gradient type and its direction. The rest two parameters specify the colors and positions.
The positions of first and last colors are optional and the remaining colors must have a specified position.
Set the directions like
- to bottom
- to right
- to bottom right
- to top right
- ellipse
- ellipse at center
The directions are followed by two or more color stops. Each color specified stops in separate argument and can be supplied inside a list.
Framework: colors stop in list.
- list of identifiers or escaped value: It is used to set the direction.
- list: It lists the position of all colors.
Framework: color stops in arguments.
- list of identifiers or escaped: it is used to set the direction.
- Color [percentage] pair: first color and its relative position (position optional).
- Color percent pair: second color (color optional) and its relative position.
This repeats up to the last color.
Returns: url with “URI-ENCODED” svg gradient.
Example
Below example explains the svg-gradient in Less misc functions.
Create a simple html file in Nodejs folder as shown below.
Svg_gradient.hmtl
[c]
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="style">
<h2>Welcome to SPLessons</h2>
<p>the best tutorial site.</p>
</div>
</body>
</html>
[/c]
Create a simple less file in the same Node js folder as shown below.
style.less
[c]
.style {
@style: white, blue 100%, #DAA520;
background-image: svg-gradient(ellipse, @style);
}
body{
text-align: center;
font-family: 'lato';
}
[/c]
Compile the above less code in command prompt using the below command.
[c]lessc style.less style.css[/c]
By compiling the code, it automatically generates the following CSS code.
style.css
[c].style {
background-image: url('data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20%3F%3E%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20width%3D%22100%25%22%20height%3D%22100%25%22%20viewBox%3D%220%200%201%201%22%20preserveAspectRatio%3D%22none%22%3E%3CradialGradient%20id%3D%22gradient%22%20gradientUnits%3D%22userSpaceOnUse%22%20cx%3D%2250%25%22%20cy%3D%2250%25%22%20r%3D%2275%25%22%3E%3Cstop%20offset%3D%220%25%22%20stop-color%3D%22%23ffffff%22%2F%3E%3Cstop%20offset%3D%22100%25%22%20stop-color%3D%22%230000ff%22%2F%3E%3Cstop%20offset%3D%22100%25%22%20stop-color%3D%22%23daa520%22%2F%3E%3C%2FradialGradient%3E%3Crect%20x%3D%22-50%22%20y%3D%22-50%22%20width%3D%22101%22%20height%3D%22101%22%20fill%3D%22url(%23gradient)%22%20%2F%3E%3C%2Fsvg%3E');
}
body {
text-align: center;
font-family: 'lato';
}
[/c]
Result
After making all the required changes, you need to run the html file in a browser to get the following output.