开发者

Dynamic CSS Background URLs

开发者 https://www.devze.com 2023-01-04 06:00 出处:网络
Consider this CSS Property: background: url(http://images.something.com/b开发者_JS百科ackground.png) left top repeat-x;

Consider this CSS Property:

background: url(http://images.something.com/b开发者_JS百科ackground.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.


Dynamic CSS Background URLs

There is another cool trick.

You could add a

.php

to the file's name so it will be Hypertext Preprocessed next time, someone calls it.

Now you can easily do (in your CSS file):

<?php $num = rand(1, 3); ?>

background: url(http://images.something.com/background<?php echo $num; ?>.png) left top repeat-x;

This will switch randomly the background-image between

background1.png, background2.png and background3.png

P.S. Dont forget to update your <link> to your css.php file.


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消