Solved

Pass variable into CSS style sheets

Posted on 2006-11-29
6
318 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Changing way home page shows 2 36
Create CSS Animation for Page Transitions 4 38
Increase the size of the modal in Bootstrap 5 43
Change box shadow 1 29
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 …
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 style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

856 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