tags:

views:

28

answers:

3

Consider this CSS Property:

background: url(http://images.something.com/background.png) left top repeat-x;

Is there a way to dynamically specify the URL being used in the external file (Meaning the URL is sort of automatically generated rather than hard-coded in the CSS file) ?

Thanks.

A: 

Yes.

You can call a server-side page, and based on variables, it can put in different CSS there.

If you just mean with html/css -- there is very little you can do dynamically.

Kerry
A: 

You can also set background images using Javascript (there are a large quantity of possibilities), but HTML and CSS are, by nature, static languages.

Mike Tierney
+2  A: 

background: url(http://images.something.com/getimage.html?image=random) left top repeat-x;

And in the getimage.html, check if request[image] == "random". Using whatever server-side language you desire, respond with an arbitrarily or randomly selected image.

Duracell
It'd probably get `getimage.php` but, whatever :P Your logic is right.
Mark
This works if you're setting a random (in this example), but if you want an image that's produced based on the context of the page?Your options there are pretty much limited to some manner of scripting language (either client- or server-side) to load a more specific image.
Mike Tierney
@Mark: Yeah, I know. I figured I'd make it language agnostic though. It could also be getimage.aspx or something else.
Duracell
Thanks for the answer but it's not what I'm looking for. What I want is to dynamically specify the URL itself so I won't be bothered changing URLs in the css file every time I work locally or when I will transfer to another domain. Am I making sense here? Thanks.
500ml