Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pass variable into CSS style sheets

Posted on 2006-11-29
6
Medium Priority
?
331 Views
Last Modified: 2008-02-01
I have a button styles
.btnA {
      background-image: url(../imgA/Bg/top-hover.jpg);
      background-repeat: no-repeat;}
.btnB {
      background-image: url(../imgB/Bg/top-hover.jpg);
      background-repeat: no-repeat;}

I want to combine them into:
.btn {
      background-image: url(<<%%==GV_Area_Parameter%%>>/Bg/top-hover.jpg);
      background-repeat: no-repeat;}

Where GV_Area_Parameter is a global variable in ASP.
However, I don't want to put this as <img src='<%=GV_Area_Parameter%>/Bg/top-hover.jpg'>
How? Is this possible? or some work arounds?
0
Comment
Question by:dvcgreen
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

by:Garve
ID: 18043786
A stylesheet doesn't have to have a .css suffix. I could be wrong, but I'm pretty sure that if you give your stylesheet an ASP suffix then it'll work fine.

eg

styles.asp

.btn {
     background-image: url(<%==GV_Area_Parameter%>/Bg/top-hover.jpg);
     background-repeat: no-repeat;}
0
 
LVL 30

Expert Comment

by:VirusMinus
ID: 18044040
a stylesheet must have a .css extension

you cannot run server side code inside a .css file.

the way to do what you're after is to use inline styles, eg:

<input type="button" style="background: url(<%=GV_Area_Parameter%>) no-repeat" value="myButton" >

OR

to have your css embedded in the page, eg:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
      <title>Untitled</title>
      <style type="text/css">
          .btnA, .btnB {background: url(<<%%==GV_Area_Parameter%%>>/Bg/top-hover.jpg) no-repeat;}
      </style>
</head>

<body>

<input type="button" class="btnA" value="myButton" >

<input type="button" class="btnB" value="myButton" >

</body>
</html>
0
 
LVL 7

Accepted Solution

by:
Garve earned 2000 total points
ID: 18044417
Hi VirusMinus

I've tried the following code

index.asp
-----------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Test CSS</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<link href="styles.asp?color=<%=request("color")%>" rel="styleSheet" type="text/css">
</head>

<body>
Text
</body>
</html>

styles.asp
-----------

body {
<%
if request("color") = "" then
%>
color: red;
<%
else
%>
color: <%=request("color")%>;
<%
end if
%>
}


It passes a variable to the stylesheet which is an ASP. It seems to work fine in IE7, and the HTML and CSS both validate. However, Firefox doesn't seem to like it.

Anyway, I was just wondering why you say 'a stylesheet must have a .css extension' - have you seen it in a specification somewhere?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:VirusMinus
ID: 18049919
Ok, i've done some further research and found the following. I may have jumped the gun to say a css file must have a .css extension. It is recommended by w3c, which points to an RFC which says the same: http://www.ietf.org/rfc/rfc2318.txt

The key point here is the MIME type. if your linking to an external file for your css and it has a different extension, other than .css then you must make sure the content type, or MIME header is of the type "text/css"

which explains that IE7 is probably detecting the content type of your styles.asp file as "text/css" and applying it. most other browsers won't.

however, server side languages allow you set the MIME type.

so if you modified you styles.asp as follows it would work! :)

'styles.asp
'-----------
<% Response.ContentType = "text/css" %>
body {
<%
if request("color") = "" then
%>
color: red;
<%
else
%>
color: <%=request("color")%>;
<%
end if
%>
}
'------------------

That would open a whole heap of possibilities, varying styles based on dates, pages, server variables, dynamic loading of CSS from a database and so on. I'm off to do more research on performance and any drawbacks of this approach.

Google seems to use serverside languages, cgi/perl possibly to generate its css as well which means there must be a lot more to gain from this approach.

Thanks Garve, goes to prove, we all learn here at EE!
0
 
LVL 7

Expert Comment

by:Garve
ID: 18052233
I didn't know about

Response.ContentType = "text/css"

I've been having problems getting XML RSS newsfeeds with .asp extensions recognised as XML - I wonder if this might solve it?

dvcgreen - sorry for getting a bit off track on your question, but hopefully you have a combined solution now.

cheers

Garve
0
 
LVL 4

Expert Comment

by:madutis
ID: 18129909
Garve is right. You can have any extension you want (or nor extension at all). But you must pass correct content type in HTML header (text/css) in your case.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a result of several questions about how to use Bootstrap I thought it would be a good idea to write down the development aspect of creating a Bootstrapped website in as little time as possible. Part 1 of this article will only concentrate on g…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question