When I first started web development I used to dislike CSS. Looking back I see I only felt that way because I never bothered to learn it properly. It wasn't because I was lazy, I was just new to development and would focus most of my learning on the back end or be too busy learning front end technologies for thick clients such as Swing, WinForms and WPF (and some proprietary solutions too if they count). Now I really enjoy CSS and a quick look at codepen.io shows part of the reason why and that I'm not alone.
In saying this, I will be the first to concede that CSS has a lot of short comings. Enter SASS, Syntactically Awesome Stylesheets. The Sass website describes it best:
Sass is an extension of CSS3, adding nested rules, variables, mixins, selector inheritance, and more.
With Sass you can specify your colors in a variables and use them throughout your selectors:And when its translated to CSS:
Our markup now only needs 1 class and we still only need to modify one place to change our background image.
This is great but we can still make it a little easier to read with nested rules:
This one may not be everyone's cup of tea but I think nesting makes for better readability. It translates to the same CSS as before but removes some of the duplication.
But wait, wouldn't it be nice if the border colour of our footer was a little bit lighter than the border colour of our header? No problem:
This and so much more make Sass a wonderful extension to CSS. Head over to the official guide to see all the other features Sass provides. Oh, and the best part, because it's a superset of CSS, any valid CSSis valid SASS.
Getting up and running with Sass in MVC4
To get up and running with Sass in an MVC4 project, install BundleTransformer.SassAndScss via nuget. I prefer to use nuget as it ensure the appropriate handlers are added to the web.config.
If you don't have a BundleConfig.cs in your App_Start you will need to add one:
If you had to add the BundleConfig.cs don't forget the relevant entry in you Global.asax.cs:
Now just add the bundle to your Razor code:
Be warned, SASS is addictive, once you've used it there is no going back.