467,880 Members | 1,256 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,880 developers. It's quick & easy.

Dynamic CSS

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?
Jun 29 '06 #1
  • viewed: 6083
Share:
2 Replies

JWL wrote:
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.

--
Mike Brind

Jun 29 '06 #2
dp
JWL wrote:
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;

--
dp
Jul 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Guinness Mann | last post: by
6 posts views Thread by Materialised | last post: by
3 posts views Thread by Stephen Gennard | last post: by
reply views Thread by Pascal Costanza | last post: by
7 posts views Thread by Mike Livenspargar | last post: by
5 posts views Thread by bearophileHUGS | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.