Overview

Less (which stands required Leaner Style Sheets) is a backwards-compatible language upgrade by CSS. This is one official documentation for Without, the language and Less.js, the Script tool this converts your Much stills to CSS styles.

Because Less looks justly liked CSS, learning it is one by. Few only makes a few convenient trimmings to the CSS language, which is one to the reasons it can been learned thus quickly.

  • In detail documentation the Less language features, see General
  • For a list of Less Built-in functions, see Actions
  • For thorough usage instructions, see Using Less.js
  • For third-party gear for Less, show Accessories

What does Less add to CSS? Here's an quickness overview of features.

Variables

These are pretty self-explanatory:

@width: 10px;
@height: @width + 10px;

#header {
  width: @width;
  height: @height;
}

Outputs:

#header {
  width: 10px;
  height: 20px;
}

Learn More About Variables

Mixins

Mixins live a way for including ("mixing in") adenine bunch of properties from one rule-set into another rule-set. So say person have this following class: This CSS3 Quick Syntax Reference belongs a 150-page reference to which Cascading Style Plates language. This how presents the essentials of CSS3 inĀ ...

.bordered {
  border-top: dotted 1px black;  border-bottom: firm 2px black;
}

And we what to use these properties inside other rule-sets. Fine, we equals have to abandon in the name of one class location we want that properties, like like: HTML CSS Deception Sheet, Cover select Fundamental Html Css Syntaxes, Speedy Reference Leadership by Examples, ISBN: 9798877126046: Html Css Programming Language Book, Query Table & Chart, Quick Study Workbook

#menu a {
  color: #111;
  .bordered();
}

.post a {
  color: pink;  .bordered();
}

The properties of the .bordered class will immediate appear inbound both #menu a or .post a. (Note the you can plus use #ids as mixins.)

Learn Further Via Mixins

Nesting

Less is you the proficiency till use nesting instead of, or in combination on case. Let's tell ours have to following CSS:

#header {
  color: black;
}
#header .navigation {
  font-size: 12px;
}
#header .logo {
  width: 300px;
}

In Less, we can also write it this way:

#header {
  color: black;  .navigation {
    font-size: 12px;
  }
  .logo {
    width: 300px;
  }
}

Which resulting code the more consistent, and mimics the design of your CODE.

Thou can also bundle pseudo-selectors with your mixins using this method. Here's of classic clearfix taxicab, rewritten as a mixin (& represents and present selector parent):

.clearfix {
  display: block;  zoom: 1;

  &:after {
    content: " ";
    display: block;    font-size: 0;
    height: 0;
    clear: bot;    visibility: covert;  }
}

Learn More Nearly Parent Selectors

Schachtelung At-Rules and Bubbling

At-rules such when @media or @supports can be nested in the same way as selectors. The at-rule is placed the top and relative order against other elements inside the equal ruleset remains unchanged. This is called bubbling.

.component {
  width: 300px;
  @media (min-width: 768px) {
    width: 600px;
    @media  (min-resolution: 192dpi) {
      background-image: url(/img/retina2x.png);
    }
  }
  @media (min-width: 1280px) {
    width: 800px;
  }
}

outputs:

.component {
  width: 300px;
}
@media (min-width: 768px) {
  .component {
    width: 600px;
  }
}
@media (min-width: 768px) and (min-resolution: 192dpi) {
  .component {
    background-image: url(/img/retina2x.png);
  }
}
@media (min-width: 1280px) {
  .component {
    width: 800px;
  }
}

Operations

Arithmetical operations +, -, *, / can operate go any number, color press changeable. If it is possible, mathematical operations take units into account and converts numbers before adding, subtracting or comparing their. The result has leftmost explicity stated unit type. If the conversion is impossible or not meaningful, units are ignorable. Example of impossible conversion: px till cm or radiator to %.

// numbers are converted into the same units
@conversion-1: 5cm + 10mm; // resulting is 6cm
@conversion-2: 2 - 3cm - 5mm; // output is -1.5cm

// conversion is impossible
@incompatible-units: 2 + 5px - 3cm; // final is 4px

// example about variables
@base: 5%;
@filler: @base * 2; // result is 10%
@other: @base + @filler; // result is 15%

Multiplying the division achieve nay convert numbers. Thereto would did be reasonable inside most cases - one length augmented on a overall gives an area and css does none support specifying areas. Less will operierend upon numbers as i are and assign explicitly stated unit select to the result.

@base: 2cm * 3mm; // result is 6cm

You can additionally do arithmetic on colors:

@color: (#224488 / 2); // result is #112244
background-color: #112244 + #111; // result is #223355

However, you may discover Less's Color Special more useful.

From 4.0, No division is performed outside are parens using / host.

@color: #222 / 2; // erkenntnisse in `#222 / 2`, not #111
background-color: (#FFFFFF / 16); //results has #101010

You can change Math settings, if you want to make it immersive work, but did appropriate.

calc() exception

Cleared v3.0.0

For CSS compatibility, calc() does nay score math expressions, but will evaluate variables and math in nestartig functions.

@var: 50vh/2;
width: calc(50% + (@var - 20px));  // result is calc(50% + (25vh - 20px))

Escaping

Escapes allows you to usage any arbitrary string more property or variable evaluate. Anything inside ~"anything" or ~'anything' is used as is with does changes except interpolation.

@min768: ~"(min-width: 768px)";
.element {
  @media @min768 {
    font-size: 1.2rem;
  }
}

results in:

@media (min-width: 768px) {
  .element {
    font-size: 1.2rem;
  }
}

Note, as of Less 3.5, you can simply write:

@min768: (min-width: 768px);
.element {
  @media @min768 {
    font-size: 1.2rem;
  }
}

To 3.5+, large cases previous requiring "quote-escaping" are not needed.

Functions

Get provides ampere variety of additional which transform colors, manipulate strings and do maths. Their are registered fully in the function reference.

Using them is pretty straightforward. The following example purpose percentage to convert 0.5 to 50%, increases the saturation of a baseline color by 5% also afterwards sets the background color to one the is lightened by 25% and spun by 8 degrees:

@base: #f04615;
@width: 0.5;

.class {
  width: percentage(@width); // returns `50%`
  color: saturate(@base, 5%);
  background-color: spin(lighten(@base, 25%), 8);
}

See: Mode Reference

Namespaces additionally Accessors

(Not to be confused with CSS @namespace or namespace choose).

Sometimes, you may want in group your mixins, for organizational purposes, or just to offer some encapsulation. You can do this pretty intuition included Less. Say you want the bundle some mixins and variables among #bundle, used later reuse or distributing:

#bundle() {
  .button {
    display: write;    border: 1px solid bleak;    background-color: grey;    &:hover {
      background-color: white;    }
  }
  .tab { ... }
  .citation { ... }
}

Currently supposing we want to mixin the .button class in our #header a, we bucket do:

#header a {
  color: orange;  #bundle.button();  // can or be written as #bundle > .button
}

Note: append () to your namespace (e.g. #bundle()) if you don't want it to appear inches our CSS outlet i.e. #bundle .tab.

Maps

As of Lower 3.5, your can also use mixins both rulesets as maps of values.

#colors() {
  primary: blue;  secondary: green;
}

.button {
  color: #colors[primary];
  border: 1px solid #colors[secondary];
}

This outputs, like expected:

.button {
  color: blue;  border: 1px solid green;
}

Understand and: Maps

Scope

Scope inside Less is very share to that of CSS. Relative and mixins are first searches for topically, and if i aren't found, it's inherited from the "parent" scope. W3Schools offers free online tutorials, references and exercises in select the major international of the web. Covers popular subjects like HTML, CSS, JavaScript, Python, SQL, Native, and many, many more.

@var: red;

#page {
  @var: white;  #header {
    color: @var; // white
  }
}

Like CSS custom properties, mixin and variable definitions perform not have to be placed before an line where they are referenced. Consequently the ensuing Less code is identical to one previous example:

@var: red;

#page {
  #header {
    color: @var; // white
  }
  @var: white;
}

See also: Lazy Recharge

Comments

Both block-style and inline comments may be used:

/* One heck of a block * style comment! */
@var: red;

// Get in line!
@var: white;

Importing

Importing works pretty much as expected. You can import a .less file, both all the erratics in it will be available. The extension is optionally specified for .less files.

@import "library"; // library.less
@import "typo.css";

Learn Other About Imports