Solved

Pass variable into CSS style sheets

Posted on 2006-11-29
6
315 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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:VirusMinus
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now