Quantcast

Add variables to standard CSS stylesheets in ASP.NET

Get the WebProNews Newsletter:


[ Business]

After reading a post on The Cafes about the lack of dynamics in the CSS language, I found myself inspired to do something about it.

It became clear that something had to be done and that we couldn’t wait for the W3C to come up with a new standard and browser vendors to incorporate them.

There were two things I wanted to enrich the CSS language with:

  • Variables
  • Scripting
  • To make it work, I created an HttpHandler for ASP.NET that is able to parse these two new features and apply them to the stylesheet. It does not interfere with any CSS standards because everything is handled by the server.

    Here is an example of a .css file that uses these new capabilities.

    define BACKCOLOR = #00FFFF;
    define FORECOLOR = pink;
    define TOTALWIDTH = 550;
    define RIGHTWIDTH = browser == "IE" & version >= 6 ? "150px" : "170px";
    define LEFTWIDTH = TOTALWIDTH / 2 + "px";
    body{
         background-color: BACKCOLOR;
          color: FORECOLOR;
          width: TOTALWIDTHpx;
    }
    #RightColumn{
          width: RIGHTWIDTH;
    }
    #LeftColumn{
          width: LEFTWIDTH;
    }

    This will then be outputted as this standard compliant stylesheet:

    body{
          background-color: #00FFFF;
          color: pink;
          width: 550;
    }
    #RightColumn{
          width: 150px;
    }
    #LeftColumn{
          width: 275px;
    }

    All the lines beginning with “define” will be treated as a variable declaration, parsed and then deleted from the parsed stylesheet. Notice the two variable names “browser” and “version”. Those two variables are built in properties that return the browser name and major version number respectively.

    The scripting syntax is 100% C# because it is compiled by the CodeDom into C# code in-memory. No new syntax of any kind is invented. The reason to use C# and not VB.NET is that developers or designers writing stylesheets probably also know JavaScript which also uses the C style syntax like C#. So, if you are comfortable writing JavaScript, it should be no problem to add scripting to the new CSS variables.

    Remember, like JavaScript, these features are case-sensitive. However, that is not why the variable names are upper cased, I just thought it makes sense to let them stick out from the rest of the styles.

    Implementation

    There are two versions of the HttpHandler – one to use if you can control the IIS and one if you can’t.

    If you are able to let the IIS send .css files to the ASP.NET engine you should download the CssHandler.cs file below and add it to the App_Code folder and then add the following to the web.config:

    <httpHandlers>
      <add type="CssHandler" verb="*" path="*.css" />
    </httpHandlers>

    If you can’t access the IIS, you need to download the CssHandler.ashx below and then call that file while passing the .css file as a query string in the URL like this:

    <link rel="stylesheet" type="text/css" href="csshandler.ashx?path=style.css" />

    Total feature list

    This is the total feature list of the handler:

    • Add variables to CSS (feature)
    • Use scripting capabilities (feature)
    • Server and client side caching (performance)
    • Cache dependant of the source .css file (performance)
    • Whitespace and comment removal (performance)
    • Works only on .css files (security)
    • Blocks the System namespace (security)

    Word of caution

    Because the scripting language is pure C#, it also means that you have to take injection attacks into account. I’ve already made sure that any scripting using anything in the System namespace will be rejected. That means that you can’t use any System.File.IO commands or anything else that has the word “System.” in it. That eliminate probably 99% of the obvious attack surface, but it is not totally secure.

    Download

    CssHandler (.cs – for IIS access).zip (1,96 KB)

    CssHandler (.ashx – for non IIS access).zip (1,91 KB)

    Comments

    Tag:

    Add to Del.icio.us | Digg | Yahoo! My Web | Furl

    Bookmark WebProNews:

    Mads Kristensen currently works as a Senior Developer at Traceworks located
    in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
    2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
    web services in his daily work as well. A true .NET developer with great passion for the simple solution.

    http://www.madskristensen.dk/

    Add variables to standard CSS stylesheets in ASP.NET
    About Mads Kristensen
    Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

    http://www.madskristensen.dk/ WebProNews Writer


    Top Rated White Papers and Resources
    • Minhthu

      how to separate the different file
      variable and css
      help me
      thank

    • Join for Access to Our Exclusive Web Tools
    • Sidebar Top
    • Sidebar Middle
    • Sign Up For The Free Newsletter
    • Sidebar Bottom