Jump to content

Less (style sheet language): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
Rfl (talk | contribs)
m making the spelling of "LESS" consistent in the article
Line 16: Line 16:
}}
}}


'''LESS''' is a dynamic stylesheet language designed by Alexis Sellier. It is influenced by [[Sass (stylesheet language)|Sass]] and has influenced the newer "SCSS" syntax of Sass, which adapted its CSS-like block formatting syntax.<ref name="compare">[http://nex-3.com/posts/83-sass-and-less Sass and Less] Sass and Less</ref> LESS is [[open-source software|open-source]]. Its first version was written in [[Ruby (programming language)|Ruby]], however in the later versions, use of [[Ruby (programming language)|Ruby]] has been [[Deprecation|deprecated]] and replaced by [[JavaScript]]. The indented syntax of Less is a [[metalanguage#Nested_metalanguage|nested metalanguage]], as valid CSS is valid Less code with the same [[semantics of programming languages|semantics]]. LESS provides the following mechanisms: [[variable (programming)|variable]]s, [[nesting (computing)#In_programming|nesting]], [[mixin]]s, [[operator (programming)|operators]] and [[function (computer science)|functions]].<ref name="main">[http://lesscss.org/ Official LESS website] Official LESS website</ref> LESS can run on the client-side ([[Internet Explorer 6]]+, [[WebKit]], [[Firefox]]) and server-side, with [[Node.js]] or [[Rhino (JavaScript engine)]].<ref name="main" />
'''LESS''' is a dynamic stylesheet language designed by Alexis Sellier. It is influenced by [[Sass (stylesheet language)|Sass]] and has influenced the newer "SCSS" syntax of Sass, which adapted its CSS-like block formatting syntax.<ref name="compare">[http://nex-3.com/posts/83-sass-and-less Sass and Less] Sass and Less</ref> LESS is [[open-source software|open-source]]. Its first version was written in [[Ruby (programming language)|Ruby]], however in the later versions, use of [[Ruby (programming language)|Ruby]] has been [[Deprecation|deprecated]] and replaced by [[JavaScript]]. The indented syntax of LESS is a [[metalanguage#Nested_metalanguage|nested metalanguage]], as valid CSS is valid LESS code with the same [[semantics of programming languages|semantics]]. LESS provides the following mechanisms: [[variable (programming)|variable]]s, [[nesting (computing)#In_programming|nesting]], [[mixin]]s, [[operator (programming)|operators]] and [[function (computer science)|functions]].<ref name="main">[http://lesscss.org/ Official LESS website] Official LESS website</ref> LESS can run on the client-side ([[Internet Explorer 6]]+, [[WebKit]], [[Firefox]]) and server-side, with [[Node.js]] or [[Rhino (JavaScript engine)]].<ref name="main" />


== Variables ==
== Variables ==
Less allows variables to be defined. Less variables are defined with an [[at sign]](@). Variable [[assignment (computer programming)|assignment]] is done with a [[colon (punctuation)|colon]] (:).
LESS allows variables to be defined. LESS variables are defined with an [[at sign]](@). Variable [[assignment (computer programming)|assignment]] is done with a [[colon (punctuation)|colon]] (:).


During translation, the values of the variables are inserted into the output CSS document.<ref name="main" />
During translation, the values of the variables are inserted into the output CSS document.<ref name="main" />
Line 79: Line 79:


== Nesting ==
== Nesting ==
CSS does support logical nesting, but the code blocks themselves are not nested. Less allows to nest selectors inside other selectors. This makes inheritance clear and style sheets shorter.<ref name="main" />
CSS does support logical nesting, but the code blocks themselves are not nested. LESS allows to nest selectors inside other selectors. This makes inheritance clear and style sheets shorter.<ref name="main" />
<pre>
<pre>
#header {
#header {
Line 117: Line 117:


== Functions and operations ==
== Functions and operations ==
Less allows operations and functions. Operations allows addition, subtraction, division and multiplication of property values and colors, which can be used to create complex relationships between properties. Functions map one-to-one with JavaScript code, allowing manipulation of values.
LESS allows operations and functions. Operations allows addition, subtraction, division and multiplication of property values and colors, which can be used to create complex relationships between properties. Functions map one-to-one with JavaScript code, allowing manipulation of values.


<pre>
<pre>

Revision as of 12:00, 24 October 2012

LESS
Designed byAlexis Sellier
DeveloperAlexis Sellier, Dmitry Fadeyev
First appeared2009
Stable release
1.3.0 / March 28, 2012 (2012-03-28)
Typing disciplinedynamic
OSCross-platform
LicenseApache License 2
Filename extensions.less
Websitelesscss.org
Influenced by
CSS, Sass
Influenced
Sass, Less Framework

LESS is a dynamic stylesheet language designed by Alexis Sellier. It is influenced by Sass and has influenced the newer "SCSS" syntax of Sass, which adapted its CSS-like block formatting syntax.[1] LESS is open-source. Its first version was written in Ruby, however in the later versions, use of Ruby has been deprecated and replaced by JavaScript. The indented syntax of LESS is a nested metalanguage, as valid CSS is valid LESS code with the same semantics. LESS provides the following mechanisms: variables, nesting, mixins, operators and functions.[2] LESS can run on the client-side (Internet Explorer 6+, WebKit, Firefox) and server-side, with Node.js or Rhino (JavaScript engine).[2]

Variables

LESS allows variables to be defined. LESS variables are defined with an at sign(@). Variable assignment is done with a colon (:).

During translation, the values of the variables are inserted into the output CSS document.[2]

@color: #4D926F;

#header {
  color: @color;
}
h2 {
  color: @color;
}

The above code in LESS would compile to following CSS code.

#header {
  color: #4D926F;
}
h2 {
  color: #4D926F;
}

Mixins

Mixins allow embedding all the properties of a class into another class by including the class name as one of its properties, thus behaving as a sort of constant or variable. They can also behave like functions, and take arguments. CSS does not support Mixins. Any repeated code must be repeated in each location. Mixins allow for more efficient and clean code repetitions, as well as easier alteration of code.[2]

.rounded-corners (@radius: 5px) {
  border-radius: @radius;
  -webkit-border-radius: @radius;
  -moz-border-radius: @radius;
}

#header {
  .rounded-corners;
}
#footer {
  .rounded-corners(10px);
}

The above code in LESS would compile to the following CSS code:

#header {
  border-radius: 5px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
}
#footer {
  border-radius: 10px;
  -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
}

LESS has a special type of ruleset called parametric mixins which can be mixed in like classes, but accepts parameters.

Nesting

CSS does support logical nesting, but the code blocks themselves are not nested. LESS allows to nest selectors inside other selectors. This makes inheritance clear and style sheets shorter.[2]

#header {
  h1 {
    font-size: 26px;
    font-weight: bold;
  }
  p { 
    font-size: 12px;
    a { 
      text-decoration: none;
      &:hover { 
        border-width: 1px;
      }
    }
  }
}

The above code in LESS would compile to the following CSS code:

#header h1 {
  font-size: 26px;
  font-weight: bold;
}
#header p {
  font-size: 12px;
}
#header p a {
  text-decoration: none;
}
#header p a:hover {
  border-width: 1px;
}

Functions and operations

LESS allows operations and functions. Operations allows addition, subtraction, division and multiplication of property values and colors, which can be used to create complex relationships between properties. Functions map one-to-one with JavaScript code, allowing manipulation of values.

@the-border: 1px;
@base-color: #111;
@red:        #842210;

#header {
  color: @base-color * 3;
  border-left: @the-border;
  border-right: @the-border * 2;
}
#footer { 
  color: @base-color + #003300;
  border-color: desaturate(@red, 10%);
}

The above code in LESS would compile to the following CSS code:

#header {
  color: #333;
  border-left: 1px;
  border-right: 2px;
}
#footer { 
  color: #114411;
  border-color: #7d2717;
}

Comparison to Sass

Both Sass and LESS are CSS preprocessors, which allow writing clean CSS in a programming construct instead of static rules.[3]

LESS is inspired by Sass.[4] Sass was designed to both simplify and extend CSS, so things like curly braces were removed from the syntax. LESS was designed to be as close to CSS as possible, so the syntax is identical to existing CSS code. As a result, existing CSS can be used as valid LESS code.

The newer versions of Sass also introduced a CSS-like syntax called SCSS (Sassy CSS).[1]

For more syntax comparison see https://gist.github.com/674726

Comparison to ZUSS

ZUSS is inspired by LESS. The syntax is similar, except it is tailored for the Java programming language. Unlike LESS, it doesn't require the JavaScript interpreter (Rhino), and it allows ZUSS to invoke Java methods directly.

Use on sites

LESS can be applied to sites in a number of ways. One option is to include the less.js JavaScript file to convert the code on-the-fly. The browser then renders the output CSS. Another option is to render the LESS code into pure CSS and upload the CSS to a site. With this option no .less files are uploaded and the site does not need the less.js JavaScript converter.

References

  1. ^ a b Sass and Less Sass and Less
  2. ^ a b c d e Official LESS website Official LESS website
  3. ^ What's Wrong With CSS What's Wrong With CSS
  4. ^ About LESS About