• Giao hàng miễn phí toàn quốc - TP HCM giao tận nơi
  • Balo PGYTECH ONE GO AIR - ONE MO 2
  • Smallrig phụ kiện hỗ trợ quay điện thoại Iphone
  • Smallrig phụ kiện cage, l plate, rig, tools
  • Tilta Phụ kiện chính hãng
  • Phụ kiện Gopro 11 10 9 8 7 6 5

3 New LESS CSS Features - 3 tính nắng mới của LESS trong CSS

There have been a bunch of new additions in it, and in this post we are going to walk you through 3 of my new favorite features, Referencing External File, Extend, Merging Property, which can help us write better CSS. Let’s take a look.

File Import

First of all, let’s take a look at how LESS CSS handles external files with the @import rule.

Some people may split their stylesheet into multiple files, rather than putting it in one pot. Then they import them with the @import to another stylesheet, so that the Mixins (as well as the Variables) from those files can be reused in that other file.

view plaincopy to clipboardprint?
  1. @import "external.less";      

The problem is that LESS will compile all the Mixins from these external files, so that we would ended up with multiple style rules that define the same thing.

Take a look at the following example: We have two LESS files called style.less and external.less. We import the external.less into style.less. Then, we have .square mixin in external.less to define the style for square boxes. Within the style.less, we use the mixin like so.

view plaincopy to clipboardprint?
  1. @import "external";  
  2.   
  3. .box {  
  4.     .square;  
  5. }  

This will produce the following result in CSS. The style-rules from .square mixin is generated as well – which is no good.

view plaincopy to clipboardprint?
  1. .square {  
  2.     width100px;  
  3.     height100px;  
  4.     margin: 0 auto;  
  5.     background-colorred;  
  6. }  
  7. .box {  
  8.     width100px;  
  9.     height100px;  
  10.     margin: 0 auto;  
  11.     background-colorred;  
  12. }  

In version 1.5, LESS introduced (reference), which can be used to instruct LESS to use the import file only for reference, and not to output the content.

Put the (reference) instruction this way:

view plaincopy to clipboardprint?
  1. @import (reference) "external";  

Compile the LESS stylesheet, and now the .square is not output.

view plaincopy to clipboardprint?
  1. .box {  
  2.     width100px;  
  3.     height100px;  
  4.     margin: 0 auto;  
  5.     background-colorred;  
  6. }  

Extend

Extend method is pure awesomeness. Technically, it groups selectors that share the same style-rules, which result in cleaner and more organised CSS. When we build a website, at some poins, we could end up having some selectors with closely similar style-rules, like below:

view plaincopy to clipboardprint?
  1. .box {  
  2.     width100px;  
  3.     height100px;  
  4.     margin: 0 auto;  
  5.     border1px solid black;  
  6.     background-colortransparent;  
  7. }  
  8. .box2 {  
  9.     width100px;  
  10.     height100px;  
  11.     margin: 0 auto;  
  12.     border1px solid black;  
  13.     background-colorred;    
  14. }  

It is redundant, and does not follow best practices, which suggest putting the same styles together. Sass, in this case, is known with its @extend directive to do this job, in LESS we can do a similar thing with &:extend(), introduced in version 1.4.

Given the above example we can do:

@import (reference) "external";
.box {
	&:extend(.square);
	background-color: transparent;
}
.box2 {
	&:extend(.square);	
	background-color: red;
}

When compiled to regular CSS, the above code will produce:

view plaincopy to clipboardprint?
  1. .square, .box, .box2 {  
  2.     width100px;  
  3.     height100px;  
  4.     margin: 0 auto;  
  5.     border1px solid black;  
  6. }  
  7. .box {  
  8.     background-colortransparent;  
  9. }  
  10. .box2 {  
  11.     background-colorred;  
  12. }  

Merging Property

Another cool new feature is Merging Property. This feature applies to the CSS property that accept multiple values, like transform, transition, and box-shadow. And as the name implies, it will merge values that belong to the same property. Let’s check out this example below.

As you probably already know, CSS3 Box Shadow property accepts multiple shadow values. By using this Merging Property, you can build shadow effect easily and make them more manageable.

Here we create two mixins .inner-shadow and .outer-shadow – I guess the names are self-explanatory.

view plaincopy to clipboardprint?
  1. .inner-shadow {  
  2.     box-shadow+: inset 10px 10px 5px #ccc;  
  3. }  
  4. .outer-shadow {  
  5.     box-shadow+: inset 10px 10px 5px #ccc;  
  6. }  

Notice that we add a + sign at the end of the property’s name. This + sign is required for this feature to work. Then, we can use these mixins, as follow:

  1. .box {  
  2.     .inner-shadow;  
  3.     .outer-shadow;  
  4. }  
  5. .box2 {  
  6.     .inner-shadow;  
  7. }  
  8. .box3 {  
  9.     .outer-shadow;  
  10. }  

This code will give us the following result.

view plaincopy to clipboardprint?
  1. .box {  
  2.   box-shadow: inset 10px 10px 5px #ccc10px 10px 5px #ccc;  
  3. }  
  4. .box2 {  
  5.   box-shadow: inset 10px 10px 5px #ccc;  
  6. }  
  7. .box3 {  
  8.   box-shadow: 10px 10px 5px #ccc;  
  9. }  

Final Thought

These 3 new features – Referencing External File, Extend, Merging Property – motivated me to use LESS more. With them we can write better, and more manageable CSS. I’m looking forward to more cool new capabilities that LESS will offer in upcoming versions.

Share facebookShare facebook

Bạn đã đọc tin này chưa ?

Go Top
Chat hỗ trợ
Chat ngay