Less css variable variables

Overview

Less (which stands for Leaner Style Sheets) is a backwards-compatible language extension for CSS. This is the official documentation for Less, the language and Less.js, the JavaScript tool that converts your Less styles to CSS styles.

Because Less looks just like CSS, learning it is a breeze. Less only makes a few convenient additions to the CSS language, which is one of the reasons it can be learned so quickly.

  • For detailed documentation on Less language features, see Features
  • For a list of Less Built-in functions, see Functions
  • For detailed usage instructions, see Using Less.js
  • For third-party tools for Less, see Tools

What does Less add to CSS? Here’s a quick overview of features.

Variables

These are pretty self-explanatory:

@width: 10px; @height: @width + 10px; #header < width: @width; height: @height; > 
#header < width: 10px; height: 20px; > 

Mixins

Mixins are a way of including («mixing in») a bunch of properties from one rule-set into another rule-set. So say we have the following class:

.bordered < border-top: dotted 1px black; border-bottom: solid 2px black; > 

And we want to use these properties inside other rule-sets. Well, we just have to drop in the name of the class where we want the properties, like so:

#menu a < color: #111; .bordered(); > .post a < color: red; .bordered(); > 

The properties of the .bordered class will now appear in both #menu a and .post a . (Note that you can also use #ids as mixins.)

Nesting

Less gives you the ability to use nesting instead of, or in combination with cascading. Let’s say we have the 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; > > 

The resulting code is more concise, and mimics the structure of your HTML.

You can also bundle pseudo-selectors with your mixins using this method. Here’s the classic clearfix hack, rewritten as a mixin ( & represents the current selector parent):

.clearfix < display: block; zoom: 1; &:after < content: " "; display: block; font-size: 0; height: 0; clear: both; visibility: hidden; > > 

Nested At-Rules and Bubbling

At-rules such as @media or @supports can be nested in the same way as selectors. The at-rule is placed on top and relative order against other elements inside the same 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; > > 
.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 on any number, color or variable. If it is possible, mathematical operations take units into account and convert numbers before adding, subtracting or comparing them. The result has leftmost explicitly stated unit type. If the conversion is impossible or not meaningful, units are ignored. Example of impossible conversion: px to cm or rad to %.

// numbers are converted into the same units @conversion-1: 5cm + 10mm; // result is 6cm @conversion-2: 2 - 3cm - 5mm; // result is -1.5cm // conversion is impossible @incompatible-units: 2 + 5px - 3cm; // result is 4px // example with variables @base: 5%; @filler: @base * 2; // result is 10% @other: @base + @filler; // result is 15% 

Multiplication and division do not convert numbers. It would not be meaningful in most cases — a length multiplied by a length gives an area and css does not support specifying areas. Less will operate on numbers as they are and assign explicitly stated unit type to the result.

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

You can also do arithmetic on colors:

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

However, you may find Less’s Color Functions more useful.

Читайте также:  Php замена всех строк

From 4.0, No division is performed outside of parens using / operator.

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

You can change Math setting, if you want to make it always work, but not recommended.

calc() exception

For CSS compatibility, calc() does not evaluate math expressions, but will evaluate variables and math in nested functions.

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

Escaping

Escaping allows you to use any arbitrary string as property or variable value. Anything inside ~»anything» or ~’anything’ is used as is with no changes except interpolation.

@min768: ~"(min-width: 768px)"; .element < @media @min768 < font-size: 1.2rem; > > 
@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; > > 

In 3.5+, many cases previously requiring «quote-escaping» are not needed.

Functions

Less provides a variety of functions which transform colors, manipulate strings and do maths. They are documented fully in the function reference.

Using them is pretty straightforward. The following example uses percentage to convert 0.5 to 50%, increases the saturation of a base color by 5% and then sets the background color to one that 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); > 

Namespaces and Accessors

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

#bundle() < .button < display: block; border: 1px solid black; background-color: grey; &:hover < background-color: white; > > .tab < . >.citation < . >> 

Now if we want to mixin the .button class in our #header a , we can do:

#header a < color: orange; #bundle.button(); // can also be written as #bundle > .button > 

Note: append () to your namespace (e.g. #bundle() ) if you don’t want it to appear in your CSS output i.e. #bundle .tab .

Читайте также:  Kotlin sort list int

Maps

As of Less 3.5, you can also use mixins and rulesets as maps of values.

#colors() < primary: blue; secondary: green; > .button < color: #colors[primary]; border: 1px solid #colors[secondary]; > 
.button < color: blue; border: 1px solid green; > 

Scope

Scope in Less is very similar to that of CSS. Variables and mixins are first looked for locally, and if they aren’t found, it’s inherited from the «parent» scope.

@var: red; #page < @var: white; #header < color: @var; // white > > 

Like CSS custom properties, mixin and variable definitions do not have to be placed before a line where they are referenced. So the following Less code is identical to the previous example:

@var: red; #page < #header < color: @var; // white > @var: white; > 

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, and all the variables in it will be available. The extension is optionally specified for .less files.

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

Less and these docs are maintained by the core Less team.

Documentation source code released under the MIT License, documentation under CC BY 3.0.

Источник

LESS — Variables

This tutorial covers variables defining and using them across styles in LESS with examples..

Why Variables in LESS

In CSS, if you are using the same value like color, font names, or any CSS values across multiple places or files, you have to use the same value repeatedly in multiple places.

For example, Let’s see declare a CSS file with blue as the primary color for headers.

In the above, the color: blue is repeatedly used in three selectors — header1,header2, and header3.

If you want to test or change the primary color to grey , You need to change it in three places.

To avoid this, LESS introduced the variables feature. Variables are declared and defined once, assigned the value to a variable and these can be used in multiple places or files.

variables provide reusability and maintainability for multiple modules

This enables Declare variable with value once and, reuse many times.

Читайте также:  How to separate string python

When you are designing styles or templates, All the variables are defined in a file, reuse this in files by import.

This provides variable values to be changed and the changes are reflected in multiple files wherever variables are being the user.

Variables in LESS are declared using at the rate of (@) symbol

Variables are prefixed with the @ symbol and separated by colon with the value of the variable, the end of the line must be the semicolon (;) symbol.

  • variable names contains hyphen(-) or underscore)(_) are interchangeable
  • Variable declaration and assignment must end with a semicolon
  • variable scope is nested only
  • can be used as a property names
  • variable values replaced by new values in the local scope
  • variable value contains font names color codes,

Let’s see how a variable is declared.

In the below example, the color-green variable is declared and assigned with the value green.

whenever blue color is required, We can reuse the value across multiple places

When the LESS compiler complied with the above codes, variables are replaced with their values in CSS snippets, The output is a CSS file with the below content as follows

color variables assigned other variables

Instead of assigning values, we can also assign other variables.

Generated CSS output after compilation

multiple font values assigned to a single variable

values can be color strings or numbers but also can be assigned with multiple values

interpolation with CSS properties names

variables also can be used as HTML property names using interpolation syntax #<>

Enclose the variable with parenthesis prefixed with a hash symbol

< #-radius:5px; font-family:$primary-font; > 

Arithmetic operations on numbers not on strings

Arithmetic operations like division, addition, etc can be applied to variables of number types, operations on strings are not allowed

LESS constants

LESS modules define the predefined variable, called constants, these variables are not changed and act as constants For example, the math module has variables called constants which are not changeable, and can only be used in multiple places.

The compilation gives a failure message

Error: Cannot modify the built-in variable.

LESS variables Scope

Scope is the usage of a variable at multiple places with limitations. It can be default or global scope. Variables are scoped in the nested level of their declaration.

Variables can be of two types.

  • Global variable is declared with! global keyword, ie global scope
  • local variables are declared under block-level ie declared inside parenthesis<> over written top variable value

Default scope variable usage:

Global variables can be overwritten with the local variable of the same name Example

Global variable scope usage:

Variable Advantages

  • Reusability
  • declare or update once, change in multiple places
  • uniform style design
  • clear separation of components when multiple teams are working

Источник

Оцените статью