Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamically assign colors in CSS

Status
Not open for further replies.

gtbikerider

Technical User
May 22, 2001
81
GB
Hi

I'd like to specify colors - something like
................................
<cfswitch expression="#product#">

<cfcase value="01">
<cfset backgroundcolor = "#99CC00">
</cfcase>

<cfdefaultcase>
<cfset backgroundcolor = "#33FF33">
</cfdefaultcase>

</cfswitch>
................................

then use those colors in the CSS file. However I can't put variables in the CSS file because (presumably) ColdFusion doesn't parse them. I realise I could load different CSS files red.css/blue.css etc but this seems a kludge - is there a better way?

--


--
John
 
escape the #-sign by doubling them in CSS style sheets (don't forget the <cfoutput> tags also)

EX:

##990000

CF will ignore the # then.

____________________________________
Just Imagine.
 
<cfif IsDefined('form.submit')>

<cfswitch expression="#FORM.selectColor#">
<cfcase value="red">
<cfset myClass = "##990000">
<cfset myColor = "red">
</cfcase>
<cfcase value="green">
<cfset myClass = "##009900">
<cfset myColor = "green">
</cfcase>
<cfdefaultcase>
<cfset myClass = "##3300CC">
<cfset myColor = "blue">
</cfdefaultcase>
</cfswitch>

<cfelse>
<cfset myClass = "##FFFF00">
<cfset myColor = "black">

</cfif>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<cfoutput>
<form name="form1" method="post" action="">
<div style="<Cfif IsDefined('myClass')>color:#myClass#;</Cfif>">
<p>I am #myColor#</p>
<p>
<select name="selectColor">
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
</select>
</p>
</div>

<input type="submit" name="Submit" value="Submit">
</form>
</cfoutput>

</body>
</html>




 
Thanks for that.

To confirm my thinking though...can I use the #myclass# in a separate CSS file?

--
John
 
Hmm...Well it seems not to work for me! If I put that in a separate CSS file it renders as

.content {
BORDER-RIGHT: #999999 1px solid; MARGIN: auto; BORDER-LEFT: #999999 1px solid; WIDTH: 955px; background-color:<cfoutput>#color_1#</cfoutput>;
}

In other words the CF code in the style.css file appears not to be parsed by CF. Is there some trick I'm not aware of?

--
John
 
Oh wait, you know what, cf server only parse code if its a cfm template, and since the css is a .css template cf server doesn't parse it.....D'uh!! Must be friday.

____________________________________
Just Imagine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top