CodeIgniter Forums
PHP in CSS file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: PHP in CSS file (/showthread.php?tid=9226)



PHP in CSS file - El Forum - 06-17-2008

[eluser]Kemik[/eluser]
Hey all,

This is a bit of a noob question but if I put php in a css file and load the css file as normal will the php run?

Code:
#middle_banner{
width:651px;
height:165px;
margin:auto;
padding:20px 0 0 0;
background:url(images/middle_banner_2.jpg) no-repeat center;
}

PHP's rand() function would be great to select a random middle_banner. I was going to do
middle_banner_<?php rand(2, 6) ?>.jpg

Thanks.


PHP in CSS file - El Forum - 06-17-2008

[eluser]daulex[/eluser]
found this on the net
Quote:I guess php tags were not being processed because the webserver did not associate a .css file with the php script engine.
The simplest way is to save the css file as
stylesheet.php and change the link to

link href="stylesheet.php" rel="stylesheet" type="text/css"

Alternatively you could solve that by telling your webserver that .css files are infact php scripts, but how you do that depends on what webserver you are using. I'd stick to the first method, as it's simple.



PHP in CSS file - El Forum - 06-17-2008

[eluser]Tom Glover[/eluser]
[quote author="daulex" date="1213747249"]found this on the net
Quote:I guess php tags were not being processed because the webserver did not associate a .css file with the php script engine.
The simplest way is to save the css file as
stylesheet.php and change the link to

link href="stylesheet.php" rel="stylesheet" type="text/css"

Alternatively you could solve that by telling your webserver that .css files are infact php scripts, but how you do that depends on what webserver you are using. I'd stick to the first method, as it's simple.
[/quote]

Remember php ignores any other code in a file that is not wrapped in php tags.


PHP in CSS file - El Forum - 06-17-2008

[eluser]Kemik[/eluser]
I tried changing the extension to php before I opened this topic as it seemed common sence. Apparently you need to add some header and content type info to the top of the css file, which I originally missed.


PHP in CSS file - El Forum - 06-17-2008

[eluser]Wilbur Himself[/eluser]
you can do it, you must have the file with a .php extension, and you must set a header like:

header('Content-type: text/css');


PHP in CSS file - El Forum - 06-17-2008

[eluser]John_Betong[/eluser]
Hi Kemik,
 
>>> PHP’s rand() function would be great to select a random middle_banner.
>>> I was going to do middle_banner_<?php rand(2, 6) ?>.jpg
 
I recently introduced this code so that the 1676x76px smiley banner starts at a different position each time the page is loaded.
Code:
<body>
   ...
   ...
    <?php $X = 20 * rand(0,80) ?>    
    <div style='clear:both; background:transparent url(/afiles/logo/la_1676x72.jpg) -&lt;?php echo $X ?&gt;px 0 repeat-x'>
    </div>  
   ...
   ...
&lt;/body&gt;
&nbsp;
You can see it here
&nbsp;
&nbsp;