Click here to Skip to main content
15,881,757 members
Articles / Web Development / CSS

CSS Variables

Rate me:
Please Sign up or sign in to vote.
2.83/5 (20 votes)
17 Dec 2006CDDL2 min read 139.1K   449   32   47
Using HTTPHandlers to support dynamic CSS.

Introduction

Users want flexibility in the websites they visit; they want to define what content they see, and how it is shown. Website developers want to give them that ability, but need to balance it against maintainability. Using Cascading Style Sheets (CSS), the developer can specify certain base layouts, or themes, and allow the user to change these at runtime. However, creating separate files for all possibilities is just not reasonable. A better way would be to change certain CSS values at runtime based on settings specified by the user, yet CSS does not have variables that can be evaluated at runtime.

Solution

To get around the problem of not having variables in CSS, one must read the CSS file and replace the given values at runtime. The good news is that in ASP.NET, this is a relatively easy task.

CSS
body
{
   background-color:#BG_COLOR#
}

Generic HTTPHandler

Using Visual Studio 2005, you can easily add a Generic HTTPHandler.

Add New Item

This will create an ashx file and add it to your project. This file implements the IHTTPHandler interface, with its one and only method, ProcessRequest, and includes the WebHandler page directive.

ASP.NET
<%@ WebHandler Language="C#" Class="Handler" %>

The .NET Framework treats these files as HTPPHandlers without the need to register them in the <httpHandlers> section of the web.config file.

ASP.NET
<%@ WebHandler Language="C#" Class="CSSHandler" %>

The code-behind:

C#
using System;
using System.Web;
using System.Configuration;

public class CSSHandler : IHttpHandler 
{
    public void ProcessRequest (HttpContext context) 
    {
        context.Response.ContentType = "text/css";
        
        // Get the file from the query stirng
        string File = context.Request.QueryString["file"];
        
        // Find the actual path
        string Path = context.Server.MapPath(File);
        
        //Limit to only css files
        if(System.IO.Path.GetExtension(Path) != ".css")
           context.Response.End();

        //Make sure file exists
        if(!System.IO.File.Exists(Path))
           context.Response.End();

        // Open the file, read the contents and replace the variables
        using( System.IO.StreamReader css = new System.IO.StreamReader(Path) )
        {
            string CSS = css.ReadToEnd();
            CSS = CSS.Replace("#BG_COLOR#", 
                      ConfigurationManager.AppSettings["BGColor"]);
            context.Response.Write(CSS);
        }
    }
    
    public bool IsReusable 
    {
        get { return false; }
    }
}

As we can see, the ProcessRequest method simply opens the file specified on the query string, reads it, and uses string replace to add the value for the variable that has been specified in the web.config file. Not much to it.

XML
<link rel="Stylesheet" href="CSSHandler.ashx?file=default.css" />

<appSettings>
   <add key="BGColor" value="Red"/>
</appSettings>

Limitations

Using a generic webhandler has a disadvantage in that you must specify the style sheet to parse. This breaks down when using ASP.NET 2.0 Themes, however, because any stylesheet placed in the theme folder will automatically be linked, no need to manually add it to your web pages. Although you can manually add each one, it isn't a very maintainable model.

Better solution

A better solution is to create a custom HTTPHandler and add it to the httpHandlers section of the web.config file.

XML
<httpHandlers>
    <add verb="*" path="*.css" 
       type="CustomHandler.CSSHandler, CustomHandler"/>
</httpHandlers>

The code:

C#
public class CSSHandler : IHttpHandler 
{
#region IHttpHandler Members

    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(System.Web.HttpContext context)
    {
        // Get the physical path of the file being processed
        string File = context.Request.PhysicalPath;

        // Open the file, read the contents and replace the variables
        using(System.IO.StreamReader reader = new System.IO.StreamReader(File))
        {
            string CSS = reader.ReadToEnd();
            CSS = CSS.Replace("#BG_COLOR#", 
                      ConfigurationManager.AppSettings["BGColor"]);
            context.Response.Write(CSS);
        }
    }

    #endregion
}

The only difference from the previous example is that the CSS file to parse is obtained from the context.Request.PhysicalPath property. Since the handler is registered for CSS files, it will process any stylesheet file regardless of its location in the web project.

Conclusion

This article has hopefully shown a method that can be used to provide dynamic settings to an otherwise static file and give website users a more positive experience.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)



Comments and Discussions

 
GeneralRe: Not working for me Pin
Saumin29-May-07 5:23
Saumin29-May-07 5:23 
GeneralGreat Work Pin
dbeard10-May-07 8:10
dbeard10-May-07 8:10 
GeneralVariable Source Pin
JoeReynolds1-Apr-07 6:56
JoeReynolds1-Apr-07 6:56 
GeneralRe: Variable Source Pin
Not Active1-Apr-07 13:53
mentorNot Active1-Apr-07 13:53 
GeneralLots of praise [modified] Pin
jokva14-Feb-07 6:58
jokva14-Feb-07 6:58 
GeneralRe: Lots of praise Pin
Not Active14-Feb-07 8:03
mentorNot Active14-Feb-07 8:03 
GeneralSecurity & Performance Pin
Evyatar Ben-Shitrit18-Dec-06 19:54
Evyatar Ben-Shitrit18-Dec-06 19:54 
GeneralRe: Security & Performance Pin
Not Active19-Dec-06 2:10
mentorNot Active19-Dec-06 2:10 
Agreed. This was a project to demonstrate the concept, not a complete solution for everyone.

Yes, not checking the file extention on the first example was my laziness (and was corrected). As I have pointed out below, however, using the second method, which I point out is a better solution, this is not an issue.


only two letters away from being an asset

GeneralDangerous Pin
The .NET Junkie18-Dec-06 7:40
The .NET Junkie18-Dec-06 7:40 
GeneralRe: Dangerous Pin
Not Active18-Dec-06 7:51
mentorNot Active18-Dec-06 7:51 
GeneralRe: Dangerous Pin
The .NET Junkie18-Dec-06 8:31
The .NET Junkie18-Dec-06 8:31 
GeneralRe: Dangerous Pin
Not Active18-Dec-06 8:56
mentorNot Active18-Dec-06 8:56 
GeneralRe: Dangerous Pin
Brian Lowe18-Dec-06 9:43
Brian Lowe18-Dec-06 9:43 
GeneralRe: Dangerous Pin
Not Active18-Dec-06 10:59
mentorNot Active18-Dec-06 10:59 
GeneralRe: Dangerous Pin
evolved19-Dec-06 5:29
evolved19-Dec-06 5:29 
GeneralRe: Dangerous Pin
trooper08149-May-07 16:55
trooper08149-May-07 16:55 
Generalbetter solution Pin
selap18-Dec-06 6:27
selap18-Dec-06 6:27 
GeneralRe: better solution Pin
Not Active18-Dec-06 6:50
mentorNot Active18-Dec-06 6:50 
GeneralRe: better solution Pin
selap20-Dec-06 5:47
selap20-Dec-06 5:47 
QuestionGood concept, but what about performance? Pin
Steven Berkovitz17-Dec-06 8:28
Steven Berkovitz17-Dec-06 8:28 
AnswerRe: Good concept, but what about performance? Pin
Not Active17-Dec-06 10:17
mentorNot Active17-Dec-06 10:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.