How to Use Functions in Less CSS

Less CSS is a robust and dynamic CSS preprocessor that has garnered significant attention and popularity in recent years. As a preprocessor, it serves as an extension of “Vanilla CSS,” the traditional CSS styling language used in web development, by providing an array of additional features and functionalities that enhance the overall styling experience.

If you are well-versed in writing standard CSS, you could seamlessly transition to using Less CSS without a steep learning curve. This compatibility makes it easy to maintain your existing CSS knowledge while leveraging the advanced capabilities of Less.

4

What Are Functions and Why Are They Important?

In programming, a function is a block of code that performs a specific task. You can also reuse it somewhere else in the program. Functions usually take in data, process it, and return the results.

They make it easy to reduce duplicate code within a program. For example, let’s say you have a program that calculates the discount based on the price the user enters. In alanguage like JavaScript, you could implement it like this:

iCloud+ Website on MacBook Sitting on Kitchen Island

Then you’re able to call the function and pass in thepriceand thethreshold.

By abstracting away the logic for checking discounts, the program is not only readable, but you now have a reusable block of code that processes the discount and returns the result. There is a lot more that functions can do, but this is just the basics.

Using hidden Windows menu

Understanding Functions in Less CSS

In traditional CSS, there is a very limited set of functions available to you as a developer. One of the most popular functions in CSS is thecalc()math functionwhich does exactly what it seems—it performs calculations and uses the result as a property value in CSS.

In Less CSS, there are several functions that do more than just arithmetic operations. One function that you may come across in Less is theiffunction. Theiffunction takes in three parameters: a condition and two values. The code block below shows how you can use this function:

A laptop with an Excel spreadsheet open and the Excel logo with a checkbox in front of the screen.

In the code block above, the Less compiler checks if the variablewidth(defined by the@symbol) is greater than the variableheight. If the condition is true, the function returns the first value after the condition (10px). Otherwise, the function returns the second value (20px).

After the compilation, the CSS output should look something like this:

Less CSS logo overlaying an image of a person using a MacBook

How to Use a Boolean in Less

A boolean is a variable that has two possible values:trueorfalse. With theboolean()function in Less, you can store the true or false value of an expression in a variable for later use. Here’s how it works.

In the code block above, the Less compiler checks iftext-coloris red. Then, thebg-colorvariable stores the value.

The code block above compiles down to something like this:

Replacing Text Within a String With the replace() Function

The syntax for thereplace()function looks like this:

stringrepresents the string that you want to search and replace in.patternis the string to search for.patterncan also be a regular expression, but it is usually a string. After a successful match, the Less CSS compiler replaces thatpatternwith thereplacement.

Optionally, thereplace()function can also contain theflagsparameter for regular expression flags.

The code block above should result in the following after compilation:

List Functions in Less CSS

In Less CSS, you use commas or spaces to define a list of values. For example:

You can use thelength()function to evaluate the number of elements in the list.

it’s possible to also use theextract()function to extract the value at a particular position. For example, if you want to get the third element in thegrocerieslist, you do the following:

Unlike regular programming languages where the list index starts from 0, the start index of a list in Less CSS is always 1.

Another list function that could come in handy when building websites with Less is therange()function. It takes in three parameters. The first parameter specifies the starting position in the range. The second parameter indicates the end position and the last parameter specifies the increment or decrement value between each item in the range. If not provided, the default value is 1.

The code block above should compile down to the following:

As you can see, the Less CSS compiler starts from 10px, increasing the previous value by 10, until it reaches the end position (40px).

Building a Simple Website With Less CSS Functions

It’s time to bring together everything you’ve learned and create a simple project with Less CSS. Create a folder and addindex.htmandstyle.lessfiles.

Open theindex.htmfile and add the following HTML code.

In the code block above, there is a parent"container"divwith an emptyh1element. Thesrcattribute on thescripttag points to the path to the Less CSS Compiler.

Without thisscripttag, the browser will not be able to understand your code. Alternatively, you may install Less CSS on your computer viaNode Package Manager (NPM), by running the following command in the terminal:

Whenever you are ready to compile the.lessfile, simply run the command below, making sure you specify the file to which the compiler should write the output.

In thestyle.lessfile, create two variables namely:container-widthandcontainer-bg-colorto represent the width and background color of the"container"divrespectively.

Next, create three variables namely:string,pattern,andreplacement. Then add the styles for the"container"divand theh1element.

In the code block above, therange()function sets thepaddingproperty on the"container"div. The Less compiler should compile thestyle.lessfile into the following:

When you open theindex.htmfile in the browser, this is what you should see:

Improve Your Productivity With CSS Preprocessors

In regular programming languages, functions reduce the amount of code you need to write and minimize errors. CSS preprocessors like Less, provide several features that make it easier to write CSS code.

CSS preprocessors come in handy when working with large files. They make it easier to debug issues, thereby improving the productivity of the developer.

Looking for a way to enhance productivity in Microsoft Visual Studio? These tips, tricks, and tools might be just what you need to code efficiently.

The fix was buried in one tiny toggle.

Turn these settings on, and your iPhone will be so much better than before.

Your phone’s camera app doesn’t show this, so it’s easy to miss.

You don’t need to fork out for expensive hardware to run an AI on your PC.

Goodbye sending links via other apps.

Technology Explained

PC & Mobile