Getting Started
4 snippetsLESS basics and syntax
Comments
// Single line comment
/* Multi-line
comment */ Import
@import "variables";
@import "mixins.less"; Nesting
.nav {
background: blue;
a {
color: white;
&:hover {
color: yellow;
}
}
} Parent Selector (&)
.button {
&:hover { }
&.active { }
&-large { } // BEM modifier
} Variables
5 snippetsStoring and reusing values
Define Variables
@primary-color: #3498db;
@font-stack: Helvetica, sans-serif;
@base-spacing: 1rem; Use Variables
.header {
color: @primary-color;
font-family: @font-stack;
padding: @base-spacing;
} Variable Interpolation
@name: "primary";
.alert-@{name} {
color: red;
} Property Name Interpolation
@property: color;
.widget {
@{property}: blue;
background-@{property}: red;
} Lazy Loading
@var: 0;
.class {
@var: 1;
.nested {
@var: 2;
prop: @var; // 2
}
prop: @var; // 1
} Mixins
5 snippetsReusable style blocks
Simple Mixin
.border-radius(@radius: 5px) {
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
border-radius: @radius;
} Use Mixin
.button {
.border-radius(10px);
} Parametric Mixin
.box-shadow(@x: 0, @y: 2px, @blur: 4px, @color: rgba(0,0,0,0.3)) {
box-shadow: @x @y @blur @color;
} Mixin Guards
.mixin (@a) when (@a > 10) {
background-color: black;
}
.mixin (@a) when (@a <= 10) {
background-color: white;
} Mixin Pattern Matching
.mixin(dark; @color) {
color: darken(@color, 10%);
}
.mixin(light; @color) {
color: lighten(@color, 10%);
} Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Functions
4 snippetsBuilt-in color and math functions
Color Functions
darken(@color, 10%);
lighten(@color, 10%);
saturate(@color, 20%);
desaturate(@color, 20%);
fadein(@color, 10%);
fadeout(@color, 10%); Math Functions
ceil(2.1); // 3
floor(2.9); // 2
percentage(0.5); // 50%
round(1.67); // 2
sqrt(25); // 5 String Functions
e("\9"); // Escape
%("Rep %a", 1+2); // "Rep 3"
replace("Hi Tom", "Tom", "Bob"); Type Functions
isnumber(1234); // true
isstring("str"); // true
iscolor(#fff); // true
ispixel(10px); // true Operations
2 snippetsMath and string operations
Math Operations
@base: 5%;
@filler: @base * 2; // 10%
@other: @base + @filler; // 15%
width: 100% / 2; // 50% Color Operations
@color: #888 / 4; // #222
@light: #888 + #111; // #999
@dark: #888 - #111; // #777 Namespaces & Scope
2 snippetsOrganizing mixins and variables
Namespace
#bundle {
.button {
display: block;
border: 1px solid black;
}
}
.header a {
#bundle.button(); // Use mixin
} Scope
@var: red;
#page {
@var: white;
color: @var; // white
#header {
color: @var; // white
}
} Extend
2 snippetsInheriting styles
Extend Selector
.a {
color: red;
}
.b {
&:extend(.a);
background: blue;
} Extend All
.a.b {
color: red;
}
.test {
&:extend(.a all);
} Guards
4 snippetsConditional logic
When Guard
.mixin (@a) when (@a >= 10) {
background-color: black;
} Multiple Conditions
.mixin (@a) when (@a > 10) and (@a < 20) {
background-color: red;
} OR Conditions
.mixin (@a) when (@a = 1), (@a = 2) {
background-color: blue;
} NOT Condition
.mixin (@a) when not (@a = 0) {
background-color: green;
}