Dynamic CSS

J

JWL

Hi

I need to create a bunch of sites with slightly dynamic CSS. Basically,
all the image paths in the CSS need to be dynamic, depending on the
values of certain ASP variables.

I can think of 3 ways to do this:

1. Write a script to create a semi-dynamic CSS file, which will be run
whenever we need to make changes to the ASP variables controlling the
image paths.

2. Have an ASP script generate the CSS on the fly:

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

3. Put all static CSS in a normal CSS file, and have a special dynamic
style sheet for only those rules that require variability. We could
either use method 2 above to generate a second style sheet, or place
dynamic rules directly in the actual asp pages.

I'm not sure what to do. (1) has the disadvantage that the CSS is not
truly dynamic. (2)... well, I'm not sure if (a) this is legal by W3C
standards and whether there's a performance hit involved. (3) has the
disadvantage that it places rules for the same elements in different
locations, which could lead to confusion.

Is there a generally accepted way to solve this problem?
 
M

Mike Brind

JWL said:
Hi

I need to create a bunch of sites with slightly dynamic CSS. Basically,
all the image paths in the CSS need to be dynamic, depending on the
values of certain ASP variables.

I can think of 3 ways to do this:

1. Write a script to create a semi-dynamic CSS file, which will be run
whenever we need to make changes to the ASP variables controlling the
image paths.

2. Have an ASP script generate the CSS on the fly:

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

3. Put all static CSS in a normal CSS file, and have a special dynamic
style sheet for only those rules that require variability. We could
either use method 2 above to generate a second style sheet, or place
dynamic rules directly in the actual asp pages.

I'm not sure what to do. (1) has the disadvantage that the CSS is not
truly dynamic. (2)... well, I'm not sure if (a) this is legal by W3C
standards and whether there's a performance hit involved. (3) has the
disadvantage that it places rules for the same elements in different
locations, which could lead to confusion.

Is there a generally accepted way to solve this problem?

The generally accepted way is to do whatever works best for you with
the minimum amount of work. Another alternative is to dynamically
generate the <link href="style.css" rel="stylesheet" type="text/css">
tag using a SELECT CASE, and have a number of hardcoded style sheets to
link to.

With regard to your points and comments on them:

1. So what if it's not truly dynamic (whatever that means)? If it
works, it works.
2. The browser has no idea what generated the CSS file. It either
validates, or it doesn't. However, generating a style sheet every time
a page is requested seems unnecessary.
3. You're right - you are overcomplicating things.
 
D

dp

JWL said:
Hi

I need to create a bunch of sites with slightly dynamic CSS.
Basically, all the image paths in the CSS need to be dynamic,
depending on the values of certain ASP variables.

I can think of 3 ways to do this:

1. Write a script to create a semi-dynamic CSS file, which will be run
whenever we need to make changes to the ASP variables controlling the
image paths.

2. Have an ASP script generate the CSS on the fly:

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

3. Put all static CSS in a normal CSS file, and have a special dynamic
style sheet for only those rules that require variability. We could
either use method 2 above to generate a second style sheet, or place
dynamic rules directly in the actual asp pages.

I'm not sure what to do. (1) has the disadvantage that the CSS is not
truly dynamic. (2)... well, I'm not sure if (a) this is legal by W3C
standards and whether there's a performance hit involved. (3) has the
disadvantage that it places rules for the same elements in different
locations, which could lead to confusion.

Is there a generally accepted way to solve this problem?

You don't have to use the .css extension for an included style sheet. You
can acutally use a .asp extension and intersperse asp and css much the same
way you would if you had html interspersed within an asp page. You just have
to set the content type of the .asp css file to text/css.

An example of what's in an .asp file linked to instead of a .css file is...
THE HTML
------------------
<link rel="stylesheet" type="text/css" href="/stylesheet.asp?margin=10">


THE stylesheet.asp FILE
---------------------------------
<%
Response.ContentType="text/css"
Dim strImageMargin
strImageMargin = Request.QueryString("margin")
%>

img {margin:<%=Trim(strImageMargin)%>px;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top