Thursday, June 3, 2021

What is LESS


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.


LESS


@width: 10px;

@height: @width + 10px;


#header {

  width: @width;

  height: @height;

}


Output

#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();

}



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;

  }

}



Parent Selectors

The & operator represents the parent selectors of a nested rule and is most commonly used when applying a modifying class or pseudo-class to an existing selector:


a {

  color: blue;

  &:hover {

    color: green;

  }

}


esults in:

a {

  color: blue;

}


a:hover {

  color: green;

}


another typical use of the & is to produce repetitive class names:


.button {

  &-ok {

    background-image: url("ok.png");

  }

  &-cancel {

    background-image: url("cancel.png");

  }


  &-custom {

    background-image: url("custom.png");

  }

}


output:


.button-ok {

  background-image: url("ok.png");

}

.button-cancel {

  background-image: url("cancel.png");

}

.button-custom {

  background-image: url("custom.png");

}



& may appear more than once within a selector. This makes it possible to repeatedly refer to a parent selector without repeating its name.


.link {

  & + & {

    color: red;

  }


  & & {

    color: green;

  }


  && {

    color: blue;

  }


  &, &ish {

    color: cyan;

  }

}


will output:


.link + .link {

  color: red;

}

.link .link {

  color: green;

}

.link.link {

  color: blue;

}

.link, .linkish {

  color: cyan;

}


Note that & represents all parent selectors (not just the nearest ancestor) so the following example:


.grand {

  .parent {

    & > & {

      color: red;

    }


    & & {

      color: green;

    }


    && {

      color: blue;

    }


    &, &ish {

      color: cyan;

    }

  }

}


results in:

.grand .parent > .grand .parent {

  color: red;

}

.grand .parent .grand .parent {

  color: green;

}

.grand .parent.grand .parent {

  color: blue;

}

.grand .parent,

.grand .parentish {

  color: cyan;

}


It can be useful to prepend a selector to the inherited (parent) selectors. This can be done by putting the & after current selector. For example, when using Modernizr, you might want to specify different rules based on supported features:



.header {

  .menu {

    border-radius: 5px;

    .no-borderradius & {

      background-image: url('images/button-background.png');

    }

  }

}


The selector .no-borderradius & will prepend .no-borderradius to its parent .header .menu to form the.no-borderradius .header .menu on output:


.header .menu {

  border-radius: 5px;

}

.no-borderradius .header .menu {

  background-image: url('images/button-background.png');

}



& can also be used to generate every possible permutation of selectors in a comma separated list:


p, a, ul, li {

  border-top: 2px dotted #366;

  & + & {

    border-top: 0;

  }

}


This expands to all possible (16) combinations of the specified elements:


p,

a,

ul,

li {

  border-top: 2px dotted #366;

}

p + p,

p + a,

p + ul,

p + li,

a + p,

a + a,

a + ul,

a + li,

ul + p,

ul + a,

ul + ul,

ul + li,

li + p,

li + a,

li + ul,

li + li {

  border-top: 0;

}




references:

https://lesscss.org/


No comments:

Post a Comment