Friday, January 25, 2008 11:36 AM mehfuzh

Keep your CSS files clean with a tiny HttpHandler

CSS, is the heart of any web application today. Writing cross browser CSS is really a hell of an art and including CSS files selectively and writing CSS hacks that will work out in one browser but wont in other is also a tedious job.

Anyway, I have come up with a tiny CSS handler , which might be handy in terms of loading multiple CSS for different browsers, removing extraneous characters that can help reduce file size and do some caching on the browser end.

At the end of post , I have given the files to download, but before that lets see how can we put it in our project.

First of all , add it to your .aspx or .master file.

<link rel="Stylesheet" href="StyleHandler.axd" type="text/css" />

Then , add the handler to the httpHanlders section in  web.config

<add verb="*" path="StyleHandler.axd" type="CssHandler" validate="false"/>

Of course, the CssHandler.cs should be under App_code. Also, validate=false means the underlying handler file wont be verified for its existence(Ex. there is no physical existence of StyleHandler.axd). Finally, you need to include a tiny xml file to the web project, that will have the browser to CSS mappings.

Inside CssMappings.xml

<cssMappings>
  <add browser="all" file="default1.css|default2.css"/>
  <add browser="ie*" file="allie.css" />
  <add browser="firefox*" file="allfirefox.css" />
</cssMappings>

Terminology

"all" means the CSS will be included for all browser types. ie*, firefox* means the CSS file will be used for all versions of that particular browser. Similarly, when specific browser number is used (Ex.ie7), the corresponding CSS file will only be included for that specific type of browser.In some scenarios, it is necessary that we have customized CSS for different version of the same browser, in that case ie* and ie7 distinction is useful. "|" is used for multiple CSS files that will be combined for a given browser type. You can also add "safari" and other known browsers that you wish to target CSS for, but you need to be sure of the name which is passed in HttpRequest for a particular browser to put it in mapping file, as the handler compares mapping entry with Request.Browser property values.

--

Additionally, the handler does a basic compression , by removing comments, newlines and tabs that makes browser feel happy.

To see , how the code works or to give a try, download it here

Updated Jan 27, 2008 : Added Gzip compression

kick it on DotNetKicks.com

Filed under: ,

Comments

# Keep your CSS files clean with a tiny HttpHandler

Friday, January 25, 2008 6:08 AM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: Keep your CSS files clean with a tiny HttpHandler

Friday, January 25, 2008 7:23 AM by joeaudette

I use a different strategy myself. I always design first using Firefox because it is the most standards compliant browser and this way I end up with the correct css markup in my main css file. IE 6 and IE7 both provide a way for including an extra style sheet so its easy to include an extra one with css that overrides the main one and fixes things that don't look right in IE.

I use one for IE 6 and a different one for IE 7, as IE7 is more standards compliant than IE 6 so they often need different treatments.

I'm pretty sure I read about the include statements below from Scott Guthrie's blog a long time ago. They are inside comments but IE detects them correctly and includes the extra css.

<link href='www.mojoportal.com/.../style.css' type='text/css' rel='stylesheet' />

<!--[if lt IE 7]>

<link runat="server" rel="stylesheet" href="www.mojoportal.com/.../IESpecific.css" type="text/css" id="IEMenuCSS" />

<![endif]-->

<!--[if gt IE 6]>

<link runat="server" rel="stylesheet" href="www.mojoportal.com/.../IE7Specific.css" type="text/css" id="IE7MenuCSS" />

<![endif]-->

This can go in the head of your master page.

Anyway its an alternative to the special handler and xml file approach you describe and thought it worth mentioning.

Cheers,

Joe

# re: Keep your CSS files clean with a tiny HttpHandler

Friday, January 25, 2008 8:18 AM by Dave Markle

Or you could make a master page with a <link runat="server" > tag and just swap out the CSS file that's sent over on Page_Load...

# re: Keep your CSS files clean with a tiny HttpHandler

Friday, January 25, 2008 11:35 AM by mehfuzh

Nice, suggestions thanks!! Especially Joe :-)

# re: Keep your CSS files clean with a tiny HttpHandler

Saturday, January 26, 2008 12:58 PM by Simon Philp

When creating a design template I usually do what joeaudette does, design with FF then introduce other style sheets that fix IE etc using style conditions.

This will go on my code snippets list though as I think it's a good different approach.  Have you thought about extending it to cache and compress the css on output aswell?

# re: Keep your CSS files clean with a tiny HttpHandler

Saturday, January 26, 2008 9:29 PM by mehfuzh

it already does a cache using Cache.HttpSetCacheability and will add a gzip for sending compressed response as well.In the meantime, it also removes unnecessary characters to make the file smaller as a first round of compression.

# re: Keep your CSS files clean with a tiny HttpHandler

Sunday, January 27, 2008 8:06 AM by Vijay Santhanam

hi, this is quite useful.

there's a similar project called jscssconsolidate

www.codeplex.com/jscssconsolidate

jcconsol doesn't encourage inmemory caching/gzip'd responses and instead relies on an MSBuild action to consolidate/clean/tidy the files.

I prefer the inmemory idea so i can change JS/css files without redeployment.

i'd like to see a few things in your code

1. js support - probably another handler with obfuscation/compaction

2. file system watching for changing css/js files. if the target static files change, it'd be cool if the handler would reload them automatically rather than restarting the appdomain.

i like reading your code,

thx

V

# re: Keep your CSS files clean with a tiny HttpHandler

Sunday, January 27, 2008 10:58 AM by mehfuzh

Thanks Vijay for your comment :-)

# re: Keep your CSS files clean with a tiny HttpHandler

Monday, January 28, 2008 3:50 AM by Shannon

I wasn't aware this was so easy to do.  I'll definitely use this in future/

# things to look at (January 29th - January 30th) | stimulant - changing things around. . .

Pingback from  things to look at (January 29th - January 30th) | stimulant - changing things around. . .

# re: Keep your CSS files clean with a tiny HttpHandler

Wednesday, August 12, 2009 3:08 AM by Puck

Oh wow, thanks for sending me this link! It works like a charm! Keep up the great work!

Leave a Comment

(required) 
(required) 
(optional)
(required)