SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Downloaden Sie, um offline zu lesen
{less}
It's CSS, with just a little more.
LESS Framework
Overview, pros and cons..
Sanjoy K. Paul
Lead Application Developer, DevOps
SanjoyPaul.com | hello@skpaul.me | +880-1511-927992
What is Less Framework?
Less (Leaner Style Sheets) is a backwards-compatible language extension for CSS.
- Less.js, the JavaScript tool that converts your Less styles to CSS styles.
- Less looks just like CSS, so it is easy to learn.
- Less only makes a few convenient additions to the CSS language, which is one of the reasons it can be learned
so quickly.
- Cleaner and more readable code can be written in an organized way.
- We can define styles and it can be reused throughout the code.
- LESS is an agile tool that sorts out the problem of code redundancy.
LESS was designed by Alexis Sellier in 2009 and published as an open-source. The first version of LESS was written in
Ruby; in the later versions, the use of Ruby was replaced by JavaScript.
What does Less add to CSS?
We will go through a quick overview of features.
- Comments
- Importing
- Variables
- Mixins
- Nesting
- Nested At-Rules and Bubbling
- Operations
- Escaping
- Namespaces and Accessors
- Maps
- Scope
Comments in Less
Both block-style and inline comments may be used
/* One heck of a block
* style comment! */
@var: red;
// Get in line!
@var: white;
Importing in Less
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";
Variables in Less
Variables are pretty self-explanatory, these are same as in other programming languages (i.e.: JavaScript/C/C++/Java).
@width: 10px;
@height: @width + 10px;
#header {
width: @width;
height: @height;
}
:root{
--width: 10px;
--height: var(--width) +
10px;
}
#header {
width: var(--width);
height: var(--height);
}
Mixins in Less
A way of including ("mixing in") a bunch of properties from one rule-set into another rule-set.
.bordered {
border-top: dotted 1px black;
border-bottom: solid 2px black;
}
#menu a {
color: #111;
.bordered();
}
.post a {
color: red;
.bordered();
}
.bordered {
border-top: dotted 1px black;
border-bottom: solid 2px black;
}
Parametric Mixins:
.border(@width; @style; @color) {
border: @width @style @color;
}
.myheader {
.border(2px; dashed; green);
}
Functions, Guards, Passing Rulesets
Nesting in Less
Less gives you the ability to use nesting instead of, or in combination with cascading.
CSS Code
#header {
color: black;
}
#header .navigation {
font-size: 12px;
}
#header .logo {
width: 300px;
}
LESS Code
#header {
color: black;
.navigation {
font-size: 12px;
}
.logo {
width: 300px;
}
}
Code is more concise, and mimics the structure of the HTML.
Nesting in Less
We can also bundle pseudo-selectors with our 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;
}
}
a {
color: blue;
&:hover {
color: green;
}
}
a {
color: blue;
}
a:hover {
color: green;
}
Nested At-Rules and Bubbling in Less
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.
.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 in Less
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.
- 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 operates on numbers as they are and assign explicitly stated unit type to the result.
- Less allows us to do arithmetical operation on colors (#224488 / 2 = #112244)
Let’s see some example..
Operations in Less
Example: 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%
Example: Multiplication and division do not convert numbers.
@base: 2cm * 3mm; // result is 6cm
Example: Arithmetic on colors
@color: #224488 / 2; //results in #112244
background-color
: #112244 + #111; // result is #223355
Example: Colors functions
saturate(hsl(90, 80%, 50%), 20%) =>#80ff00 // hsl(90, 100%, 50%)
lighten(hsl(90, 80%, 50%), 20%) => #b3f075 // hsl(90, 80%, 70%)
darken(hsl(90, 80%, 50%), 20%) => #4d8a0f // hsl(90, 80%, 30%)
fade(hsl(90, 90%, 50%), 10%) => rgba(128, 242, 13, 0.1) //hsla(90,
90%, 50%, 0.1)
Operations (calc() exception) in Less
calc() function does not evaluate math expressions for CSS compatibility, but it evaluates variables and math in nested
functions.
@var: 50vh/2;
width: calc(50% + (@var - 20px)); // output is calc(50% + (25vh - 20px))
Functions in Less
Less provides a variety of functions which transform colors, manipulate strings and do maths
@base: #f04615;
@width: 0.5;
.class {
width: percentage(@width); // returns `50%`
color: saturate(@base, 5%);
background-color
: spin(lighten(@base, 25%), 8);
}
Using them is pretty straightforward.
This 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.
Namespaces and Accessors in Less
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.
Example: if we 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.
Escaping in Less
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;
}
}
In Less 3.5, you can simply write. In 3.5+, many cases previously requiring "quote-escaping" are not needed.
@min768: (min-width: 768px);
.element {
@media @min768 {
font-size: 1.2rem;
}
}
Maps in Less
We 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 in Less
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
}
}
@var: red;
#page {
#header {
color: @var; // white
}
@var: white;
}
Installing, configuring and usages of Less
- Command Line Usag
- https://lesscss.org/usage/#command-line-usage
- Browser Usage
- https://lesscss.org/usage/#using-less-in-the-browser
Supports:
- Less.js supports all modern browsers (recent versions of Chrome, Firefox, Safari, IE11+, and Edge).
- It is possible to use Less on the client side in production, please be aware that there are performance implications for
doing so (although the latest releases of Less are quite a bit faster).
- Sometimes cosmetic issues can occur if a JavaScript error occurs. This is a trade off of flexibility vs. speed.
- For the fastest performance possible for a static web site,
- We recommend compiling Less on the server side.
Installing, configuring and usages of Less
To start off, link your .less stylesheets with the rel attribute set to "stylesheet/less"
<link rel="stylesheet/less" type="text/css" href="styles.less" />
Next, download less.js and include it in a <script></script> tag in the <head> element of your page
<script src="less.js" type="text/javascript" ></script>
Need to keep in mind:
- Make sure you include your stylesheets before the script.
- When you link more than one .less stylesheet each of them is compiled independently. So any variables, mixins or namespaces you
define in a stylesheet are not accessible in any other.
- Due to the same origin policy of browsers, loading external resources requires enabling CORS
Installing, configuring and usages of Less
You can set options either programmatically, by setting them on a less object before the script tag - this then affects all initial link tags and
programmatic usage of less.
<script>
less = {
env: "development",
async: false,
fileAsync: false,
poll: 1000,
functions: {},
dumpLineNumbers
: "comments",
relativeUrls: false,
rootpath: ":/a.com/"
};
</script>
<script src="less.js"></script>
The other way is to specify the options on the script tag, e.g.
<script>
less = {
env: "development"
};
</script>
<script src="less.js" data-env="development"></script>
Or for brevity they can be set as attributes on the script and link tags
<script src="less.js" data-poll ="1000" data-relative-urls ="false"></script>
<link data-dump-line-numbers ="all" data-global-vars ='{ "myvar": "#ddffee", "mystr": ""quoted"" }' rel="stylesheet/less"
type="text/css" href="less/styles.less" >
Pros and Cons of Less
???
Cons:
- The learning curve
- Could inherit mistakes of framework
developer
- Could lead to a lack of understanding the
underlying css
- Bloated code
- Might restrict you as you conform to the
framework
- Lack of semantic code
Pros:
- Increased productivity
- Standardized codebase
- Makes it easier to work with a team
- Provides a more organized approach to
css
- Publicly released frameworks are
improved by the community
- Fewer mistakes
- Cross browser reliability
- Can be a learning tool
Would you like to learn more about Less?
- Overview: http://lesscss.org/
- Usages: https://lesscss.org/usage/#using-less-in-the-browser
- Functions: https://lesscss.org/functions/
- Features: https://lesscss.org/features/
- Tools: https://lesscss.org/tools/
- Editors and Editor Plugins: https://lesscss.org/tools/#editors-and-plugins
- Contributing?: https://lesscss.org/usage/#developing-less
- More: https://www.tutorialspoint.com/less/index.htm
Question?
Sanjoy K. Paul
Lead Application Developer | Software Architect | DevOps | Trainer
www.SanjoyPaul.com | skpaul@DevsStation.com | +880-1511-927992

Weitere ähnliche Inhalte

Was ist angesagt?

P H P Part I I, By Kian
P H P  Part  I I,  By  KianP H P  Part  I I,  By  Kian
P H P Part I I, By Kian
phelios
 

Was ist angesagt? (20)

Less & Sass
Less & SassLess & Sass
Less & Sass
 
Do more with {less}
Do more with {less}Do more with {less}
Do more with {less}
 
basics of css
basics of cssbasics of css
basics of css
 
Web Design Course: CSS lecture 2
Web Design Course: CSS  lecture 2Web Design Course: CSS  lecture 2
Web Design Course: CSS lecture 2
 
Compass, Sass, and the Enlightened CSS Developer
Compass, Sass, and the Enlightened CSS DeveloperCompass, Sass, and the Enlightened CSS Developer
Compass, Sass, and the Enlightened CSS Developer
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 
Css web side
Css web sideCss web side
Css web side
 
Making Sense of Twig
Making Sense of TwigMaking Sense of Twig
Making Sense of Twig
 
Web Engineering - Basic CSS Properties
Web Engineering - Basic CSS PropertiesWeb Engineering - Basic CSS Properties
Web Engineering - Basic CSS Properties
 
Web Design Course: CSS lecture 4
Web Design Course: CSS  lecture 4Web Design Course: CSS  lecture 4
Web Design Course: CSS lecture 4
 
Web Design Course: CSS lecture 3
Web Design Course: CSS lecture 3Web Design Course: CSS lecture 3
Web Design Course: CSS lecture 3
 
Css3 and gwt in perfect harmony
Css3 and gwt in perfect harmonyCss3 and gwt in perfect harmony
Css3 and gwt in perfect harmony
 
CSS Walktrough Internship Course
CSS Walktrough Internship CourseCSS Walktrough Internship Course
CSS Walktrough Internship Course
 
ID01 Week 3
ID01 Week 3ID01 Week 3
ID01 Week 3
 
P H P Part I I, By Kian
P H P  Part  I I,  By  KianP H P  Part  I I,  By  Kian
P H P Part I I, By Kian
 
Ppt ch05
Ppt ch05Ppt ch05
Ppt ch05
 
CSS3 For WebKit: iPadDevCamp Presentation
CSS3 For WebKit: iPadDevCamp PresentationCSS3 For WebKit: iPadDevCamp Presentation
CSS3 For WebKit: iPadDevCamp Presentation
 
CSS3 Implementable Features
CSS3 Implementable FeaturesCSS3 Implementable Features
CSS3 Implementable Features
 
Web Design Course: CSS lecture 1
Web Design Course: CSS lecture 1Web Design Course: CSS lecture 1
Web Design Course: CSS lecture 1
 
ID01 / W01
ID01 / W01ID01 / W01
ID01 / W01
 

Ähnlich wie CSS Less framework overview, Pros and Cons

SASS is more than LESS
SASS is more than LESSSASS is more than LESS
SASS is more than LESS
Itai Koren
 
Big Design Conference: CSS3
Big Design Conference: CSS3 Big Design Conference: CSS3
Big Design Conference: CSS3
Wynn Netherland
 
Software programming tools for creating/managing CSS files
Software programming tools for creating/managing CSS filesSoftware programming tools for creating/managing CSS files
Software programming tools for creating/managing CSS files
Dinu Suman
 

Ähnlich wie CSS Less framework overview, Pros and Cons (20)

Write LESS. DO more.
Write LESS. DO more.Write LESS. DO more.
Write LESS. DO more.
 
Accelerated Stylesheets
Accelerated StylesheetsAccelerated Stylesheets
Accelerated Stylesheets
 
@Agawish creating a stunning ui with oracle adf faces, using sass
@Agawish   creating a stunning ui with oracle adf faces, using sass@Agawish   creating a stunning ui with oracle adf faces, using sass
@Agawish creating a stunning ui with oracle adf faces, using sass
 
Preprocessor presentation
Preprocessor presentationPreprocessor presentation
Preprocessor presentation
 
SASS is more than LESS
SASS is more than LESSSASS is more than LESS
SASS is more than LESS
 
Less(CSS Pre Processor) Introduction
Less(CSS Pre Processor) IntroductionLess(CSS Pre Processor) Introduction
Less(CSS Pre Processor) Introduction
 
LESS(CSS Pre Processor) introduction
LESS(CSS Pre Processor) introductionLESS(CSS Pre Processor) introduction
LESS(CSS Pre Processor) introduction
 
CSS Extenders
CSS ExtendersCSS Extenders
CSS Extenders
 
Less css
Less cssLess css
Less css
 
SASS, Compass, Gulp, Greensock
SASS, Compass, Gulp, GreensockSASS, Compass, Gulp, Greensock
SASS, Compass, Gulp, Greensock
 
Why less?
Why less?Why less?
Why less?
 
Sass Essentials
Sass EssentialsSass Essentials
Sass Essentials
 
Big Design Conference: CSS3
Big Design Conference: CSS3 Big Design Conference: CSS3
Big Design Conference: CSS3
 
Fasten RWD Development with Sass
Fasten RWD Development with SassFasten RWD Development with Sass
Fasten RWD Development with Sass
 
LESS : The dynamic stylesheet language
LESS : The dynamic stylesheet languageLESS : The dynamic stylesheet language
LESS : The dynamic stylesheet language
 
Software programming tools for creating/managing CSS files
Software programming tools for creating/managing CSS filesSoftware programming tools for creating/managing CSS files
Software programming tools for creating/managing CSS files
 
An Introduction to CSS Preprocessors (SASS & LESS)
An Introduction to CSS Preprocessors (SASS & LESS)An Introduction to CSS Preprocessors (SASS & LESS)
An Introduction to CSS Preprocessors (SASS & LESS)
 
Sass & Compass (Barcamp Stuttgart 2012)
Sass & Compass (Barcamp Stuttgart 2012)Sass & Compass (Barcamp Stuttgart 2012)
Sass & Compass (Barcamp Stuttgart 2012)
 
Less css framework
Less css frameworkLess css framework
Less css framework
 
Css3 and gwt in perfect harmony
Css3 and gwt in perfect harmonyCss3 and gwt in perfect harmony
Css3 and gwt in perfect harmony
 

Kürzlich hochgeladen

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

CSS Less framework overview, Pros and Cons

  • 1. {less} It's CSS, with just a little more.
  • 2. LESS Framework Overview, pros and cons.. Sanjoy K. Paul Lead Application Developer, DevOps SanjoyPaul.com | hello@skpaul.me | +880-1511-927992
  • 3. What is Less Framework? Less (Leaner Style Sheets) is a backwards-compatible language extension for CSS. - Less.js, the JavaScript tool that converts your Less styles to CSS styles. - Less looks just like CSS, so it is easy to learn. - Less only makes a few convenient additions to the CSS language, which is one of the reasons it can be learned so quickly. - Cleaner and more readable code can be written in an organized way. - We can define styles and it can be reused throughout the code. - LESS is an agile tool that sorts out the problem of code redundancy. LESS was designed by Alexis Sellier in 2009 and published as an open-source. The first version of LESS was written in Ruby; in the later versions, the use of Ruby was replaced by JavaScript.
  • 4. What does Less add to CSS? We will go through a quick overview of features. - Comments - Importing - Variables - Mixins - Nesting - Nested At-Rules and Bubbling - Operations - Escaping - Namespaces and Accessors - Maps - Scope
  • 5. Comments in Less Both block-style and inline comments may be used /* One heck of a block * style comment! */ @var: red; // Get in line! @var: white;
  • 6. Importing in Less 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";
  • 7. Variables in Less Variables are pretty self-explanatory, these are same as in other programming languages (i.e.: JavaScript/C/C++/Java). @width: 10px; @height: @width + 10px; #header { width: @width; height: @height; } :root{ --width: 10px; --height: var(--width) + 10px; } #header { width: var(--width); height: var(--height); }
  • 8. Mixins in Less A way of including ("mixing in") a bunch of properties from one rule-set into another rule-set. .bordered { border-top: dotted 1px black; border-bottom: solid 2px black; } #menu a { color: #111; .bordered(); } .post a { color: red; .bordered(); } .bordered { border-top: dotted 1px black; border-bottom: solid 2px black; } Parametric Mixins: .border(@width; @style; @color) { border: @width @style @color; } .myheader { .border(2px; dashed; green); } Functions, Guards, Passing Rulesets
  • 9. Nesting in Less Less gives you the ability to use nesting instead of, or in combination with cascading. CSS Code #header { color: black; } #header .navigation { font-size: 12px; } #header .logo { width: 300px; } LESS Code #header { color: black; .navigation { font-size: 12px; } .logo { width: 300px; } } Code is more concise, and mimics the structure of the HTML.
  • 10. Nesting in Less We can also bundle pseudo-selectors with our 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; } } a { color: blue; &:hover { color: green; } } a { color: blue; } a:hover { color: green; }
  • 11. Nested At-Rules and Bubbling in Less 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. .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; } }
  • 12. Operations in Less 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. - 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 operates on numbers as they are and assign explicitly stated unit type to the result. - Less allows us to do arithmetical operation on colors (#224488 / 2 = #112244) Let’s see some example..
  • 13. Operations in Less Example: 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% Example: Multiplication and division do not convert numbers. @base: 2cm * 3mm; // result is 6cm Example: Arithmetic on colors @color: #224488 / 2; //results in #112244 background-color : #112244 + #111; // result is #223355 Example: Colors functions saturate(hsl(90, 80%, 50%), 20%) =>#80ff00 // hsl(90, 100%, 50%) lighten(hsl(90, 80%, 50%), 20%) => #b3f075 // hsl(90, 80%, 70%) darken(hsl(90, 80%, 50%), 20%) => #4d8a0f // hsl(90, 80%, 30%) fade(hsl(90, 90%, 50%), 10%) => rgba(128, 242, 13, 0.1) //hsla(90, 90%, 50%, 0.1)
  • 14. Operations (calc() exception) in Less calc() function does not evaluate math expressions for CSS compatibility, but it evaluates variables and math in nested functions. @var: 50vh/2; width: calc(50% + (@var - 20px)); // output is calc(50% + (25vh - 20px))
  • 15. Functions in Less Less provides a variety of functions which transform colors, manipulate strings and do maths @base: #f04615; @width: 0.5; .class { width: percentage(@width); // returns `50%` color: saturate(@base, 5%); background-color : spin(lighten(@base, 25%), 8); } Using them is pretty straightforward. This 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.
  • 16. Namespaces and Accessors in Less 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. Example: if we 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.
  • 17. Escaping in Less 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; } } In Less 3.5, you can simply write. In 3.5+, many cases previously requiring "quote-escaping" are not needed. @min768: (min-width: 768px); .element { @media @min768 { font-size: 1.2rem; } }
  • 18. Maps in Less We 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; }
  • 19. Scope in Less 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 } } @var: red; #page { #header { color: @var; // white } @var: white; }
  • 20. Installing, configuring and usages of Less - Command Line Usag - https://lesscss.org/usage/#command-line-usage - Browser Usage - https://lesscss.org/usage/#using-less-in-the-browser Supports: - Less.js supports all modern browsers (recent versions of Chrome, Firefox, Safari, IE11+, and Edge). - It is possible to use Less on the client side in production, please be aware that there are performance implications for doing so (although the latest releases of Less are quite a bit faster). - Sometimes cosmetic issues can occur if a JavaScript error occurs. This is a trade off of flexibility vs. speed. - For the fastest performance possible for a static web site, - We recommend compiling Less on the server side.
  • 21. Installing, configuring and usages of Less To start off, link your .less stylesheets with the rel attribute set to "stylesheet/less" <link rel="stylesheet/less" type="text/css" href="styles.less" /> Next, download less.js and include it in a <script></script> tag in the <head> element of your page <script src="less.js" type="text/javascript" ></script> Need to keep in mind: - Make sure you include your stylesheets before the script. - When you link more than one .less stylesheet each of them is compiled independently. So any variables, mixins or namespaces you define in a stylesheet are not accessible in any other. - Due to the same origin policy of browsers, loading external resources requires enabling CORS
  • 22. Installing, configuring and usages of Less You can set options either programmatically, by setting them on a less object before the script tag - this then affects all initial link tags and programmatic usage of less. <script> less = { env: "development", async: false, fileAsync: false, poll: 1000, functions: {}, dumpLineNumbers : "comments", relativeUrls: false, rootpath: ":/a.com/" }; </script> <script src="less.js"></script> The other way is to specify the options on the script tag, e.g. <script> less = { env: "development" }; </script> <script src="less.js" data-env="development"></script> Or for brevity they can be set as attributes on the script and link tags <script src="less.js" data-poll ="1000" data-relative-urls ="false"></script> <link data-dump-line-numbers ="all" data-global-vars ='{ "myvar": "#ddffee", "mystr": ""quoted"" }' rel="stylesheet/less" type="text/css" href="less/styles.less" >
  • 23. Pros and Cons of Less ??? Cons: - The learning curve - Could inherit mistakes of framework developer - Could lead to a lack of understanding the underlying css - Bloated code - Might restrict you as you conform to the framework - Lack of semantic code Pros: - Increased productivity - Standardized codebase - Makes it easier to work with a team - Provides a more organized approach to css - Publicly released frameworks are improved by the community - Fewer mistakes - Cross browser reliability - Can be a learning tool
  • 24. Would you like to learn more about Less? - Overview: http://lesscss.org/ - Usages: https://lesscss.org/usage/#using-less-in-the-browser - Functions: https://lesscss.org/functions/ - Features: https://lesscss.org/features/ - Tools: https://lesscss.org/tools/ - Editors and Editor Plugins: https://lesscss.org/tools/#editors-and-plugins - Contributing?: https://lesscss.org/usage/#developing-less - More: https://www.tutorialspoint.com/less/index.htm
  • 25. Question? Sanjoy K. Paul Lead Application Developer | Software Architect | DevOps | Trainer www.SanjoyPaul.com | skpaul@DevsStation.com | +880-1511-927992