Solved

Pass variable into CSS style sheets

Posted on 2006-11-29
6
317 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 500 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
alert before form submission 6 40
Easy responsive table out of existing table 28 55
Target h5 of a div 3 16
help squeezing some space out in this site 9 31
CSS3 Custom checkboxes This article shows how to style the checkbox form element using only CSS. Works in: Chrome, FF, Safari, Opera, IE9+ Uses modernizr.js to check for :checked pseudo class, falling back to plain old checkboxes (IE8 and bel…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
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…

813 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

17 Experts available now in Live!

Get 1:1 Help Now