LESS provides us a way to organize CSS styles and make it manageable. We could define variables that can be reusable and make the rules consistent.
There are lots of repeating values in CSS, most especially when we are working with multiple browser’s compatibility. To name some:
- border-radius
- box-shadow
- gradient
- colors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.box_radius_normal { | |
-webkit-border-radius:4px; | |
-khtml-border-radius:4px; | |
-moz-border-radius:4px; | |
-ie-border-radius:4px; | |
-o-border-radius:4px; | |
border-radius:4px; | |
} |
The Solution:
Let us refactor the CSS class using LESS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@radius_value:4px; | |
.box_radius_LESS { | |
-webkit-border-radius:@radius_value; | |
-khtml-border-radius:@radius_value; | |
-moz-border-radius:@radius_value; | |
-ie-border-radius:@radius_value; | |
-o-border-radius:@radius_value; | |
border-radius:@radius_value; | |
} |
How Do We Use LESS CSS
less.js
You need to download less.js @ lesscss.org and include it in a
<script></script>
tag in the <head>
element of your page. This is the JavaScript that converts LESS commands into normal CSS file on the client site.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="js/less-1.7.3.min.js"></script> |
Normal CSS File
We can code LESS in normal CSS file. In Visual Studio 2013, you don’t need to name the CSS file as .less.
But if you need full LESS editing features, install Web Essentials. You can separate LESS file and CSS file.
In the HMTL Page
You need to specify the rel attribute to stylesheet/less.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="css/site.css" rel="stylesheet/less" /> |
Your thoughts please.