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 ”;
Category: less
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 – Passing Rulesets to Mixins ”; Previous Next Description Detached ruleset contains rulesets such as properties, nested rulesets, variables declaration, mixins, etc. It is stored in a variable and included in another structure; all the properties of the ruleset get copied to that structure. Example The following example shows how to pass a ruleset to mixin in the LESS file − passing_ruleset.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 @detached-ruleset: { .mixin() { font-family: “Comic Sans MS”; background-color: #AA86EE; } }; .cont { @detached-ruleset(); .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 .cont { font-family: “Comic Sans MS”; background-color: #AA86EE; } Output Follow these steps to see how the above code works − Save the above html code in the passing_ruleset.htm file. Open this HTML file in a browser, the following output will get displayed. Scoping All variables and mixins in detached ruleset are available wherever the ruleset called or defined. Otherwise, both the caller and the definition scopes are available by default. The declaration scope takes the priority when both scopes contain same mixin or variable. Detached ruleset body is defined in the declaration scope. It does not change its scope after the detached ruleset is copied from one variable to another. The following table lists all the types of scope − Sr.No. Types & Description 1 Definition and Caller Scope Visibility Variables and mixins are defined inside the detached ruleset. 2 Referencing Won”t Modify Detached Ruleset Scope Just giving the references, the ruleset does not access to any new scopes. 3 Unlocking Will Modify Detached Ruleset Scope The detached ruleset can access to scope by being imported into it. Print Page Previous Next Advertisements ”;
Less – Loops
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 ”;
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 – Nested Directives and Bubbling ”; Previous Next Description You can nest the directives such as media and keyframe in the same manner, the way you nest the selectors. You can place the directive on top and its relative elements will not be changed inside its rule set. This is known as the bubbling process. Example The following example demonstrates the use of the nested directives and bubbling in the LESS file − <html> <head> <title>Nested Directives</title> <link rel = “stylesheet” type = “text/css” href = “style.css” /> </head> <body> <h1>Example using Nested Directives</h1> <p class = “myclass”>LESS enables customizable, manageable and reusable style sheet for web site.</p> </body> </html> Next, create the file style.less. style.less .myclass { @media screen { color: blue; @media (min-width: 1024px) { color: green; } } @media mytext { color: black; } } 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 @media screen { .myclass { color: blue; } } @media screen and (min-width: 1024px) { .myclass { color: green; } } @media mytext { .myclass { color: black; } } Output Follow these steps to see how the above code works − Save the above html code in the nested_directives_bubbling.html file. Open this HTML file in a browser, the following output will get displayed. Print Page Previous Next Advertisements ”;
Less – Comments
LESS – Comments ”; Previous Next Description Comments make the code clear and understandable for the users. You can use both the block style and the inline comments in the code, but when you compile the LESS code, the single line comments will not appear in the CSS file. Example The following example demonstrates the use of comments in the LESS file − <html> <head> <title>Less Comments</title> <link rel = “stylesheet” type = “text/css” href = “style.css” /> </head> <body> <h1>Example using Comments</h1> <p class = “myclass”>LESS enables customizable, manageable and reusable style sheet for web site.</p> <p class = “myclass1″>It allows reusing CSS code and writing LESS code with same semantics.</p> </body> </html> Now create the style.less file. style.less /* It displays the green color! */ .myclass { color: green; } // It displays the blue color .myclass1 { color: red; } You can compile the style.less file to style.css by using the following command − lessc style.less style.css Now execute the above command; it will create the style.css file automatically with the following code − style.css /* It displays the green color! */ .myclass { color: green; } .myclass1 { color: red; } Output Follow these steps to see how the above code works − Save the above html code in the comments.html file. Open this HTML file in a browser, the following output will get displayed. Print Page Previous Next Advertisements ”;