Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pass variable into CSS style sheets

Posted on 2006-11-29
6
Medium Priority
?
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unordered lists (HTML ul tags) are very handy to create menus of all types, shapes and sizes.  All it takes is a little knowledge of CSS and the possibilities are endless. First off, you should know the general format for menus created using the …
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…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

719 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