Less Basics
Leaner Style Sheets - a dynamic stylesheet language that extends CSS with variables, mixins, and functions.
What is Less CSS?
Less is a CSS preprocessor similar to Sass. It adds variables (@brand), nesting, mixins and operations, then compiles to normal CSS. It was popularised by Bootstrap 3. Today Sass is more widely used, and native CSS covers many of Less's features.
@brand: #3b82f6;
.rounded(@r: 8px) { border-radius: @r; }
.button {
background: @brand;
.rounded(12px);
&:hover { background: darken(@brand, 10%); }
}Complete Less Example
Code Editor
<p>Browsers can't read Less directly. This is the <b>Less source</b>:</p>
<pre>@brand: #2563eb;
.rounded(@r: 8px) { border-radius: @r; }
.button {
background: @brand;
.rounded(12px);
&:hover { background: darken(@brand, 10%); }
}</pre>
<p>...and this button uses the <b>compiled CSS</b> (see the CSS tab):</p>
<button class="button">Compiled button</button>Preview
Less Fundamentals
Variables with @ Symbol
// Define variables with @
@primary-color: #007bff;
@secondary-color: #28a745;
@font-size-base: 16px;
@line-height: 1.6;
@border-radius: 4px;
// Computed values
@font-size-lg: @font-size-base * 1.25;
@font-size-sm: @font-size-base * 0.875;
// String interpolation
@image-path: "/images";
@base-url: "https://example.com";
.logo {
background-image: url("@{image-path}/logo.png");
}
// Use variables
.button {
background: @primary-color;
font-size: @font-size-base;
border-radius: @border-radius;
}Variables in Less use the @ symbol and support calculations and string interpolation.
Operations
// Mathematical operations
@base: 16px;
@multiplier: 1.5;
.header {
font-size: @base * @multiplier; // 24px
padding: @base / 2; // 8px
margin: @base + 4px; // 20px
width: 100% - @base; // calc expression
}
// Color operations
@primary: #007bff;
.button {
background: @primary;
border: 1px solid (@primary - #111);
}Perform calculations on numbers, colors, and dimensions
Nesting
// Nested rules
.navigation {
background: white;
padding: 1rem;
ul {
list-style: none;
margin: 0;
li {
display: inline-block;
a {
text-decoration: none;
// Parent selector
&:hover {
color: @primary-color;
}
}
}
}
}Less supports CSS nesting with parent selector (&) references
Mixins & Advanced Features
Mixins - Reusable Code
// Basic mixins
.border-radius(@radius: 5px) {
border-radius: @radius;
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
}
.box-shadow(@shadow) {
box-shadow: @shadow;
-webkit-box-shadow: @shadow;
-moz-box-shadow: @shadow;
}
// Parametric mixins
.button-style(@bg-color, @text-color: white, @padding: 10px 15px) {
background: @bg-color;
color: @text-color;
padding: @padding;
border: none;
.border-radius(4px);
.box-shadow(0 2px 4px rgba(0,0,0,0.1));
&:hover {
background: lighten(@bg-color, 10%);
}
}
// Use mixins
.primary-button {
.button-style(@primary-color, white, 12px 20px);
}
.card {
.border-radius(8px);
.box-shadow(0 4px 8px rgba(0,0,0,0.15));
}Mixins in Less accept parameters and can be called like functions.
Built-in Functions
// Color functions
@base-color: #007bff;
.element {
// Lighten/darken
background: lighten(@base-color, 20%);
border: 1px solid darken(@base-color, 15%);
// Saturate/desaturate
color: saturate(@base-color, 30%);
border-color: desaturate(@base-color, 20%);
// Fade/alpha
box-shadow: 0 2px 4px fade(@base-color, 30%);
}
// Math functions
.container {
width: percentage(3/4); // 75%
height: round(15.6px); // 16px
margin: ceil(14.1px); // 15px
}Less provides many built-in functions for colors and math
Guards & Conditionals
// Mixin guards
.button-variant(@type) when (@type = primary) {
background: @primary-color;
color: white;
}
.button-variant(@type) when (@type = secondary) {
background: @secondary-color;
color: white;
}
.button-variant(@type) when (@type = outline) {
background: transparent;
border: 1px solid @primary-color;
color: @primary-color;
}
// Conditional logic
.theme(@mode) when (@mode = dark) {
background: #333;
color: white;
}
.theme(@mode) when (@mode = light) {
background: white;
color: #333;
}Guards enable conditional logic in mixins
Setup & Best Practices
Installation & Setup
- • Node.js: npm install -g less
- • Command Line: lessc styles.less styles.css
- • Watch Mode: lessc --watch styles.less styles.css
- • Minification: lessc --clean-css styles.less
- • Source Maps: lessc --source-map styles.less
- • Browser Usage: Include less.js for client-side
- • Build Tools: Webpack, Gulp, Grunt plugins
- • VS Code: Easy LESS extension
Best Practices
- • Use meaningful variable names (@primary vs @blue)
- • Organize code with imports (@import "variables.less")
- • Limit nesting depth to 3-4 levels maximum
- • Use mixins for repeated patterns with parameters
- • Leverage guards for conditional styling
- • Compile to CSS for production (don't use less.js)
- • Follow consistent naming conventions
- • Document complex mixins and functions
File Organization
less/
├── variables.less // Color palette, dimensions
├── mixins.less // Reusable mixins
├── reset.less // Normalize/reset styles
├── typography.less // Font styles
├── components/
│ ├── buttons.less
│ ├── cards.less
│ ├── navigation.less
│ └── forms.less
├── layout/
│ ├── grid.less
│ ├── header.less
│ └── footer.less
└── main.less // Import all filesLess CSS — step by step
Set up Less compilation
Install the less package with npm or use a build tool plugin. Less files use the .less extension and compile to standard CSS.
🎯 Quick Check
How do you declare a variable in Less?
Less: summary
Key points
- Less variables start with
@; Sass variables start with$. - Any class can be used as a mixin in Less.
- Less can run in the browser via JavaScript, but compile it at build time for production.
Frequently asked questions
›Should I use Less or Sass?
For new projects, Sass has the larger ecosystem, or you can use plain modern CSS. Use Less if you maintain a project that already relies on it.