LESS – Loops ”; Previous Next In this chapter, we will understand how Loops work in LESS. Loops statement allows us to execute a statement or group of statements multiple times. Various iterative/loop structures can be created when recursive mixin combine with Guard Expressions and Pattern Matching. Example The following example demonstrates the use of loops in the LESS file − loop_example.htm <!doctype html> <head> <link rel = “stylesheet” href = “style.css” type = “text/css” /> </head> <body> <div class = “cont”> <h2>Welcome to TutorialsPoint</h2> <p>The largest Tutorials Library on the web. </p> </div> </body> </html> Next, create the style.less file. style.less .cont(@count) when (@count > 0) { .cont((@count – 1)); width: (25px * @count); } div { .cont(7); } You can compile the style.less file to style.css by using the following command − lessc style.less style.css Execute the above command; it will create the style.css file automatically with the following code − style.css div { width: 25px; width: 50px; width: 75px; width: 100px; width: 125px; width: 150px; width: 175px; } Output Follow these steps to see how the above code works − Save the above html code in the loop_example.htm file. Open this HTML file in a browser, the following output will get displayed. Print Page Previous Next Advertisements ”;
Category: less
Less – Parametric Mixins
LESS – Parametric Mixins ”; Previous Next Description Parametric mixins use one or more parameters that extend functionality of LESS by taking arguments and its properties to customize the mixin output when mixed into another block. For instance, consider a simple LESS code snippet − .border(@width; @style; @color) { border: @width @style @color; } .myheader { .border(2px; dashed; green); } Here we are using the parametric mixin as .border with three parameters – width, style and color. Using these parameters, you can customize the mixin output with the passed parameters value. The following table describes the different types of parametric mixins along with description. Sr.No. Types & Description 1 Mixins with Multiple Parameters Parameters can be separated using commas or semicolon. 2 Named Parameters Mixins provide parameter values instead of positions by using their names. 3 @arguments Variable When a mixin is called, the @arguments include all the passed arguments. 4 Advanced Arguments and the @rest Variable Mixin takes variable number of arguments by using ….. 5 Pattern-matching Change the behavior of mixin by passing parameters to it. Print Page Previous Next Advertisements ”;
Less – CSS Guards
LESS – CSS Guards ”; Previous Next Description Guards are used to match simple values or a number of arguments on expressions. It is applied to the CSS selectors. It is syntax for declaring mixin and calling it immediately. To successfully bring out the if type statement; join this with feature &, which allows you to group multiple guards. Example The following example demonstrates the use of css guard in the LESS file − css_guard.htm <!doctype html> <head> <link rel = “stylesheet” href = “style.css” type = “text/css” /> </head> <body> <div class = “cont”> <h2>Welcome to TutorialsPoint</h2> </div> <div class = “style”> <h3>The largest Tutorials Library on the web.</h3> </div> </body> </html> Next, create the style.less file. style.less @usedScope: global; .mixin() { @usedScope: mixin; .cont when (@usedScope = global) { background-color: red; color: black; } .style when (@usedScope = mixin) { background-color: blue; color: white; } @usedScope: mixin; } .mixin(); You can compile the style.less file to style.css by using the following command − lessc style.less style.css Execute the above command; it will create the style.css file automatically with the following code − style.css .style { background-color: blue; color: white; } Output Follow these steps to see how the above code works − Save the above html code in the css_guard.htm file. Open this HTML file in a browser, the following output will get displayed. Print Page Previous Next Advertisements ”;
Less – Merge
LESS – Merge ”; Previous Next Description This feature in LESS allows the addition of values to comma or space separated list from multiple properties using a single property. It can be used for background and transform properties. The following table describes the two types of functions supported by the Merge feature. Sr.No. Types & Description 1 Comma It adds property value at the end. 2 Space It adds property value with space. Print Page Previous Next Advertisements ”;
Less – Overview
LESS – Overview ”; Previous Next LESS is a CSS pre-processor that enables customizable, manageable and reusable style sheet for website. LESS is a dynamic style sheet language that extends the capability of CSS. LESS is also cross browser friendly. CSS Preprocessor is a scripting language that extends CSS and gets compiled into regular CSS syntax, so that it can be read by your web browser. It provides functionalities like variables, functions, mixins and operations that allow you to build dynamic CSS. Why LESS? Let us now understand why do we use LESS. LESS supports creating cleaner, cross-browser friendly CSS faster and easier. LESS is designed in JavaScript and also created to be used in live, which compiles faster than other CSS pre-processors. LESS keeps your code in modular way which is really important by making it readable and easily changeable. Faster maintenance can be achieved by the use of LESS variables. History LESS was designed by Alexis Sellier in 2009. LESS is an open-source. The first version of LESS was written in Ruby; in the later versions, the use of Ruby was replaced by JavaScript. Features 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 based on JavaScript and is a super set of CSS. LESS is an agile tool that sorts out the problem of code redundancy. Advantages LESS easily generates CSS that works across the browsers. LESS enables you to write better and well-organized code by using nesting. Maintenance can be achieved faster by the use of variables. LESS enables you to reuse the whole classes easily by referencing them in your rule sets. LESS provides the use of operations that makes coding faster and saves time. Disadvantages It takes time to learn if you are new to CSS preprocessing. Due to the tight coupling between the modules, more efforts should be taken to reuse and/or test dependent modules. LESS has less framework compared to older preprocessor like SASS, which consists of frameworks Compass, Gravity and Susy. Print Page Previous Next Advertisements ”;
Less – Parent Selectors
LESS – Parent Selectors ”; Previous Next In this chapter, let us understand how Parent Selectors work. It is possible to reference the parent selector by using the &(ampersand) operator. Parent selectors of a nested rule is represented by the & operator and is used when applying a modifying class or pseudo class to an existing selector. The following table shows the types of parent selector − Sr.No. Types & Description 1 Multiple & The & will represent the nearest selector and also all the parent selectors. 2 Changing Selector Order Prepending a selector to the inherited (parent) selectors is useful when selector ordering is changed. 3 Combinatorial Explosion The & can also produce all the possible permutation of selectors in a list separated by commas. Example The following example demonstrates the use of parent selector in the LESS file − <!doctype html> <head> <link rel = “stylesheet” href = “style.css” type = “text/css” /> <title>Parent Selector</title> </head> <body> <h2>Welcome to TutorialsPoint</h2> <ul> <li><a>SASS</a></li> <li><a>LESS</a></li> </ul> </body> </html> Next, create the style.less file. style.less a { color: #5882FA; &:hover { background-color: #A9F5F2; } } You can compile the style.less file to style.css by using the following command − lessc style.less style.css Execute the above command; it will create the style.css file automatically with the following code − style.css a { color: #5882FA; } a:hover { background-color: red; } In the above example, & refers to selector a. Output Follow these steps to see how the above code works − Save the above html code in the parent_selector1.htm file. Open this HTML file in a browser, the following output will get displayed. The Parent selectors operator has many uses like, when you need to combine the nested rule”s selectors in other way than the default. Another typical use of & is to generate class names repeatedly. For more information click here. Print Page Previous Next Advertisements ”;
Less – Scope
LESS – Scope ”; Previous Next Description Variable scope specifies the place of the available variable. The variables will be searched from the local scope and if they are not available, then compiler will search from the parent scope. Example The following example demonstrates the use of namespaces and accessors in the LESS file − <html> <head> <title>Less Scope</title> <link rel = “stylesheet” type = “text/css” href = “style.css” /> </head> <body> <h1>Example using Scope</h1> <p class = “myclass”>LESS enables customizable, manageable and reusable style sheet for web site.</p> </body> </html> Now create the style.less file. style.less @var: @a; @a: 15px; .myclass { font-size: @var; @a:20px; color: green; } You can compile the style.less file to style.css by using the following command − lessc style.less style.css Execute the above command; it will create the style.css file automatically with the following code − style.css .myclass { font-size: 20px; color: green; } Output Follow these steps to see how the above code works − Save the above html code in the scope.html file. Open this HTML file in a browser, the following output will get displayed. Print Page Previous Next Advertisements ”;
Less – Extend
LESS – Extend ”; Previous Next Extend is a LESS pseudo class which extends other selector styles in one selector by using :extend selector. Example The following example demonstrates the use of extend in the LESS file − extend_syntax.htm <!doctype html> <head> <link rel = “stylesheet” href = “style.css” type = “text/css” /> </head> <body> <div class = “style”> <h2>Welcome to TutorialsPoint</h2> <p>Hello!!!!!</p> </div> </body> </html> Next, create the style.less file. style.less h2 { &:extend(.style); font-style: italic; } .style { background: green; } You can compile the extend.less file to extend.css by using the following command − lessc style.less style.css Execute the above command; it will create the style.css file automatically with the following code − style.css h2 { font-style: italic; } .style, h2 { background: blue; } Output Follow these steps to see how the above code works − Save the above html code in the extend_syntax.htm file. Open this HTML file in a browser, the following output will get displayed. Extend Syntax Extend is placed into ruleset or attached to a selector. It is similar to a pseudo class containing one or more classes, which are separated by comma. Using the optional keyword all, each selector can be followed. Example The following example demonstrates the use of extend syntax in the LESS file − extend_syntax.htm <!doctype html> <head> <link rel = “stylesheet” href = “style.css” type = “text/css” /> </head> <body> <div class = “style”> <h2>Welcome to TutorialsPoint</h2> <div class = “container”> <p>Hello!!!!!</p> </div> </div> </body> </html> Now create the style.less file. style.less .style:extend(.container, .img) { background: #BF70A5; } .container { font-style: italic; } .img { font-size: 30px; } You can compile the style.less file to style.css by using the following command − lessc style.less style.css Execute the above command; it will create the style.css file automatically with the following code − style.css .style { background: #BF70A5; } .container, .style { font-style: italic; } .img, .style { font-size: 30px; } Output Follow these steps to see how the above code works − Save the above html code in the extend_syntax.htm file. Open this HTML file in a browser, the following output will get displayed. The following table lists all the types of extend syntax which you can use in LESS − Sr.No. Types & Description 1 Extend Attached to Selector Extend is connected to a selector which looks similar to a pseudo class with selector as parameter. 2 Extend Inside Ruleset The &:extend(selector) syntax can be put inside the body of ruleset. 3 Extending Nested Selectors Nested selectors are matched using the extend selector. 4 Exact Matching with Extend By default, extend looks for the exact match between the selectors. 5 nth Expression The form of nth expression is important in extend otherwise, it treats selector as different. 6 Extend “all” When the keyword all is identified at last in the extend argument then LESS matches that selector as part of another selector. 7 Selector Interpolation with Extend The extend can be connected to interpolated selector. 8 Scoping/Extend inside @media Extend matches the selector only that is present inside the same media declaration. 9 Duplication Detection It cannot detect the duplication of selectors. Following are the types of Use Cases for Extend Sr.No. Types & Description 1 Classic Use Case Classic use case is used to avoid adding the base class in LESS. 2 Reducing CSS Size Extend is used to move the selector as far as the properties you want to use; this helps in reducing the css generated code. 3 Combining Styles/ A More Advanced Mixin Using extend we can combine the same styles of a particular selectors into other selector. Print Page Previous Next Advertisements ”;
Less – Variables
LESS – Variables ”; Previous Next In this chapter, we will discuss the Variables in LESS. LESS allows variables to be defined with an @ symbol. The Variable assignment is done with a colon(:). The following table demonstrates the use of LESS variables in detail. Sr.No. Variables usage & Description 1 Overview Repetition of same value many times can be avoided by the use of variables. 2 Variable Interpolation The variables can also be used in other places like selector names, property names, URLs and @import statements. 3 Variable Names We can define variables with a variable name consisting of a value. 4 Lazy Loading In lazy loading, variables can be used even when they are not. 5 Default Variables Default variable has an ability to set a variable only when it is not already set. This feature is not required because variables can be easily overridden by defining them afterwards. Print Page Previous Next Advertisements ”;
LESS – Namespaces and Accessors ”; Previous Next Description Namespaces are used to group the mixins under a common name. Using namespaces, you can avoid conflict in name and encapsulate a group of mixins from outside. Example The following example demonstrates the use of namespaces and accessors in the LESS file − <html> <head> <title>Less Namespaces and Accessors</title> <link rel = “stylesheet” type = “text/css” href = “style.css” /> </head> <body> <h1>Example using Namespaces and Accessors</h1> <p class = “myclass”>LESS enables customizable, manageable and reusable style sheet for web site.</p> </body> </html> Now create the style.less file. style.less .class1 { .class2 { .val(@param) { font-size: @param; color:green; } } } .myclass { .class1 > .class2 > .val(20px); } You can compile the style.less file to style.css by using the following command − lessc style.less style.css Execute the above command; it will create the style.css file automatically with the following code − style.css .myclass { font-size: 20px; color: green; } Output Follow these steps to see how the above code works − Save the above html code in the namespaces_accessors.html file. Open this HTML file in a browser, the following output will get displayed. Print Page Previous Next Advertisements ”;