CssHandler: CSS + Variables

November 16, 2007 12:54 PM

It's been more than three and a half years since Rory Blyth said "screw standards — let's add variable support to CSS right this minute." In retrospect, adding variables to CSS seems obvious; everyone is asking about it. There have even been one or two other .Net solutions developed, though Rory's solution has remained my favorite. With CSS2 barely working and CSS3 not addressing the issue, those of us who are tired of repeating ourselves throughout our CSS files have no choice but to take matters into our own hands.

With that in mind I set out to update Rory's code. Not that there was anything wrong with it in the first place, of course (this is where I try to tactfully avoid being drawn as a jerkasaurus in one of Rory's comics), but Rory did whip the code up for a PADNUG meeting and that was about 3.5 million years ago (give or take many orders of magnitude). It was time to bring the CssHandler into 2007! Conveniently right before 2008.

Lo and behold, someone else had beat me to it. Gabe Moothart had already uploaded the CssHandler to CodePlex, and even improved upon it. While Rory's code originally only allowed variable declarations & references and stripped comments, Gabe's version also resolved application-relative paths (e.g.: "~/blah/blah.gif") into browser friendly paths. I had further goals in mind though, and Gabe was kind enough to add me onto the project.

With my updates complete, I am now proud to present...

CssHandler 1.0

Features:

  • Define variables for later reference.
  • Resolve application relative paths.
  • Only link to one CSS file from your HTML page. Let the CssHandler combine additional CSS files at runtime to limit HTTP connections and share variable definitions across files.
  • All comments are stripped before render.
  • Most white-space is stripped before render.
  • HttpHandler can be mapped to *.css or can be referenced as CssHandler.axd and passed a CSS file in the query string.

Bugs fixed:

  • The @define{...} block is no longer sent down to the client.
  • Similarly named variables no longer present a problem.

Example:

The HTML references "styles.css", which looks like:

CssHandler_stylecss

As seen above, "styles.css" references "styles2.css" via the @import statement, which looks like:

CssHandler_style2css

The CssHandler then:

  1. Intercepts the browser's request for "styles.css".
  2. Resolves the two URLs that are using application relative paths.
  3. Replaces the @import directive with the text from "styles2.css".
  4. Strips all comments from the CSS.
  5. Replaces all referenced variables with their defined values.
  6. Compresses most of the CSS's white-space.
  7. Renders the following to the browser:

CssHandler_rendered

I hope you find it as useful as I have. Many thanks to Rory Blyth and Gabe Moothart. Hopefully CSS4 will add variables and we'll never have to use this again!

Visit the project's CodePlex site to download.

Update (Dec. 2, 2007): Rory has posted and given his blessing, so to speak. Thanks Rory!

Tags: ,
Categories: Tools
Actions: E-mail | Permalink | Comments (11) RSS Feed for this post's comments.

Comments

11/16/2007 1:03 PM #

trackback


Trackback from: DotNetKicks.com

11/16/2007 1:47 PM #

Gabe


Troy,
Thanks for your work cleaning things up and adding some features.

My original motivation was jealousy at all those condescending Rails programmers using Sass(http://haml.hamptoncatlin.com/docs/sass). Now I can be less jealous Smile

Gabe us

11/16/2007 1:52 PM #

Danny Douglass

Awesome! I've used this before and it rocks. Well done Troy!

Danny Douglass us

11/16/2007 1:59 PM #

Troy Goode

Gabe, thanks for pointing out Sass... I've actually never seen it before. I'll take a look at it and see if there are any other features that might be quick or highly useful to implement.

Troy Goode us

12/4/2007 12:17 AM #

trackback


Trackback from: Danny Douglass

12/9/2007 7:46 AM #

liviu


Hi,

Do you support formulas ;) ?

let's say that i want something like this ( very likely to happenSmile

.smallBorder { border : solid @borderPx Blue}
.biggerBorder { border : solid @borderPx+1 Blue}

liviu ro

12/9/2007 12:31 PM #

Troy Goode


Hi Liviu,

Currently we do not support formulas, though that has been proposed as the likely next step. I've given some thought recently on how to implement this and believe I have a gameplan, so now I just need some time to sit down and make it happen. Thanks for the question!

Troy

Troy Goode us

1/22/2008 7:44 AM #

Balaji

i want to assign the javascript variable in to css variable. how can i write the code?

Balaji in

1/22/2008 10:31 AM #

Troy Goode

Hi Balaji, I'm not exactly certain what it is you want to do here. This CssHandler allows you to set server-side variables for your CSS files, while JavaScript is used primarily for client-side stuff.

Are you just asking how to change a CSS property of an element via JavaScript? If you wanted to change the background color of this element:

<div id="myId">hello</div>

You would use the following JavaScript statement:

document.getElementById('myId').style.backgroundColor = 'red';

Does this help you?

Troy Goode us

4/14/2008 1:42 PM #

Paul

Troy, great work! Any thought of adding in conditional comments to get around various browser hacks?

e.g.: /*if[ie]
@import('~/style/ie.css')
[endif]*/

to avoid even parsing, etc the strings on files that aren't needed in other browsers?

Paul us

4/14/2008 4:41 PM #

Troy Goode

Very interesting idea, Paul. I'm not currently working on this project actively, but that does seem like a useful suggestion.

Troy Goode us

Add Comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading




Troy Goode

Troy Goode
Microsoft Certified Professional Developer
AddThis Feed Button

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008

Colophon

Powered by:
BlogEngine.NET 1.4
Template:
Designs by Darren
Header Font:
Stamper
Syntax Highlighting:
WLW Code Snippet Plugin