stylesheet trick RRS feed

  • Question

  • User1828997928 posted

    This is very simple and obvious and may already be used by a lot of people but it helped me out a lot.  I have a central stylesheet "stylesheet.css" and I kept wanting to put variables in it and get values from the web.config file (Example:  The image path is different on my dev and production boxes and is stored in the web.config). 

    I changed the name of my css from stylesheet.css to stylesheet.aspx and used code in script tags to call function and create variables.  Here is an example: 

    <script runat="server">
    Public oColor as String = "red"
    Sub Page_Load(Sender as object, e as eventargs)
    End Sub

    .ClassName{color:<%# oColor %>;}

    Then in the header of the page I changed the link href attribute from stylesheet.css to stylesheet.aspx

    <link href="stylesheet.aspx" rel="stylesheet" type="text/css">

    It works great and now I have a lot more control over my central css file

    Wednesday, April 5, 2006 4:23 PM

All replies

  • User-1046474645 posted
    I'd like to add something:
    It is important to set ContentType="text/css" in the @Page directive (or setting Response.ContentType="text/css"; in for instance Page_Load) to make sure stylesheet.aspx is served as CSS and interpreted correctly by all browsers. I don't think Internet Explorer cares much about content types, but I know Firefox and Opera does.

    Tuesday, April 11, 2006 5:19 AM
  • User1828997928 posted
    I tested it with the latest versions of FireFox and Opera and it seemed to work fine but you are right that it should be included.
    Tuesday, April 11, 2006 9:10 AM