Setup dotLess CSS In ASP.NET MVC Project

This article shows you how to set up the dotLess CSS framework with an ASP.NET MVC project in order to enable Inheritance, Arithmetic and Variables etc. in stylesheets.

Advantages

  1. LESS broadens CSS with dynamic behaviors like Nesting, Variables, Math Functions, Operational Function, Reusable Mixins and namespaces etc.
  2. It makes easy to work with CSS.
  3. It makes reusable components.
  4. It minifies the original CSS file which results the performance boost.

Running LESS on the Client Side

LESS can run on the client or from the server side. It involves the following three steps to run it from the client side.

  1. Add your .less file to your project and reference that in your LINK tag.
  2. Update the rel value in the LINK tag to rel="stylesheet/less".
  3. Add a reference to the LESS JavaScript file from the CDN.

It is very important to update the rel value to "stylesheet/less" because the LESS library looks for rel value. It process the file once it gets rel value as a "stylesheet/less". Your page header should look like below.

  1. <linkhref="Content/sample.less"rel="stylesheet/less"/>  
  2. <scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/less.js/2.6.1/less.min.js"></script>  
I have defined some LESS code in sample.less file to ensure it works.

Less code:
  1. @color: #4D926F;  
  2. h3 {  
  3. color: @color;  
  4. }  
HTML code:
  1. <!DOCTYPEhtml>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title></title>  
  6.     <meta charset="utf-8" />  
  7.     <link href="Content/sample.less" rel="stylesheet/less" />  
  8.     <script src="https://cdnjs.cloudflare.com/ajax/libs/less.js/2.6.1/less.min.js">  
  9.         </script>  
  10. </head>  
  11.   
  12. <body>  
  13.     <h3>This is Santosh!</h3>  
  14. </body>  
  15.   
  16. </html>  
Output:

output

In the same way you can use other dynamic behaviors as well:

Use of Nesting

dotLess CSS code

  1. body {  
  2.     .header   
  3.     {  
  4.         h1  
  5.         {  
  6.             font - size: 28 px;  
  7.         }  
  8.   
  9.         h2   
  10.         {  
  11.             font - size: 21 px;  
  12.         }  
  13.     }  
  14. }  
Will translate to:
  1. body.headerh1  
  2. {  
  3.     font - size: 28 px;  
  4. }  
  5. body.headerh2  
  6. {  
  7.     font - size: 21 px;  
  8. }  
Variables

Less code:
  1. @color: #4D926F;  
  2. h3 {  
  3. color: @color;  
  4. }  
Will translate to: 
  1. h3  
  2.  {  
  3. color: #4D926F;  
  4. }  
Math Functions

Less code:
  1. @base - size: 10 px;  
  2.   
  3. .small  
  4. {  
  5.     font - size: @base - size;  
  6. }  
  7.   
  8. .medium  
  9. {  
  10.     font - size: @base - size * 1.2;  
  11. }  
  12.   
  13. .large  
  14. {  
  15.     @_large: @base - size * 1.5;  
  16.     font - size: @_large;  
  17.     line - height: @_large + 4;  
  18. }  
Will translate to:
  1. .small   
  2. {  
  3.     font - size: 10 px;  
  4. }  
  5. .medium   
  6. {  
  7.     font - size: 12 px;  
  8. }  
  9. .large  
  10. {  
  11.     font - size: 15 px;  
  12.     line - height: 19 px;  
  13. }  
Operational Functions

Less code:
  1. h1   
  2. {  
  3.     color: rgb(230, 140, 230);  
  4.     color: darken(#123456,10%);   
  5.     color: fade(# 123456, 50 % );  
  6. }  
Will translate to:
  1. h1  
  2. {  
  3.     color: #e68ce6;  
  4.     color: #091a2c;  
  5.     color:rgba(18, 52, 86, 0.5);  
  6. }  
Reusable Mixins

Less code:
  1. .radius(@size: 5 px)   
  2.     {  
  3.         -webkit - border - radius: @size; - moz - border - radius: @size;  
  4.         border - radius: @size;  
  5.     }  
  6.     .box  
  7.     {  
  8.         .radius;  
  9.     }  
  10.     .circle  
  11.     {  
  12.         .radius(15 px);  
  13.     }  
Will translate to:
  1. .box  
  2. {  
  3.     -webkit - border - radius: 5 px; - moz - border - radius: 5 px;  
  4.     border - radius: 5 px;  
  5. }  
  6. .circle   
  7. {  
  8.     -webkit - border - radius: 15 px; - moz - border - radius: 15 px;  
  9.     border - radius: 15 px;  
  10. }  
Import CSS files into Less file

If you have a big CSS file then you can split your code into multiple CSS files and you can import into one less file. The main file where you have imported, that needs to be included in the webpage, from this file all other style modules are imported.

Less code:
  1. @import (less) "Content/Site.css";  
  2. /*@import url('Site.css');*/  
Will translate to: 
  1. @import "Content/Site.css";  
Running LESS on the ServerSide

serverside

From the Tools menu, select NuGet Package Manager, then Manage NuGet Package for Solution. Run the following command in the Package Manager Console.

PM> Install-Package dotLess

This command installs the latest dotLessNuGet packages.

Note: You can install dotLess either from Package Manager Console or Manage NuGet Package for Solution as shown in the above screen.

After successful installation, you can remove the JavaScript CDN reference from your page. And make sure that you have updated the LINK tag by removing the /less from the rel attribute. The code should look like below:
  1. <link href="Content/sample.less"rel="stylesheet"/>  
After installation “Install-Package dotLess” plugin from Package Manager Console or Manage NuGet Package for Solution, the package adds a few entries to the Web.Config file available in ConfigSection and HTTP handler section defined into your Web.config file to cater for .less requests (refer below code snippet).
  1. <configSections>  
  2.     <section name="dotless" type="dotless.Core.configuration.DotlessConfigurationSectionHandler, dotless.Core" />  
  3. </configSections>  
  4. <dotlessminifyCss="false" cache="true" web="false" strictMath="false" />  
  5.   
  6. <system.webServer>  
  7.     <handlers>  
  8.         <addname="dotless" path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler,dotless.Core" resourceType="File" preCondition="" />  
  9.     </handlers>  
  10. </system.webServer>  
Sample code snippet with output:

Add the following code to your Razor layout that is _Layout.cshtml

  1. <!DOCTYPEhtml>  
  2. <html>  
  3.   
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  7.         <title>@ViewBag.Title - My ASP.NET Application</title>  
  8.         @Styles.Render("~/Content/css") @Styles.Render("~/Content/less") @Scripts.Render("~/bundles/modernizr")  
  9. </head>  
  10.   
  11. <body>  
  12.     --- --- ---  
  13. </body>  
  14.   
  15. </html>  
  16.   
  17. Index.cshtml @{ ViewBag.Title = "Home Page"; Layout = "~/Views/Shared/_Layout.cshtml"; }  
  18. <div class="row">  
  19.     <h3>Hello Santosh</h3>  
  20.     </div>  
Output:

output

The best feature about dotLess is that it can automatically minify the CSS for you via the minifyCss attribute. If you update that to "true" in your Web.config file and run the web application/website now, you'll see the minified CSS.
  1. <dotlessminifyCss="false" cache="true"web="false"strictMath="false" />  
If the minifyCss attribute is "false" then the output will be as below:

code

If the minifyCss attribute is "true" then the output will be as below:

code

Improve performance for dotLess files in MVC project

Bundles are an easy way to merge and minify resources in your application (such as JavaScript files and CSS stylesheets). Using “System.Web.Optimization.Less” plugin, you can improve site performance in a better way.

So go back to Package Manager Console and install the below plugin:

PM> Install-Package System.Web.Optimization.Less

And add your bundle to the appropriate location within BundleConfig.cs,
  1. public class BundleConfig  
  2. {  
  3.     public static void RegisterBundles(BundleCollection bundles)  
  4.     {  
  5.         // NOTE: existing bundles are here  
  6.   
  7.         bundles.Add(newLessBundle("~/Content/less").Include("~/Content/*.less"));  
  8.     }  
  9. }  
So, this LessBundle gives you the facility to combine and minify files while running the application in <compilation debug="false" /> mode, and it takes care of transforming LESS code into CSS. It does not require updating the layout every time you add a new file to the project.

Development Vs production workflow

In development, we use a handler for parsing dotLess stylesheets on the fly. It allows us to make modifications to the dotLess CSS and simply reload a page in the web browser to see the results.

In production, we exclude the handler and use a pre-parsed minified version of the stylesheet. This pre-parsed minified version is updated every time the website project is built.

Read more articles on ASP.NET: