Dynadot

[Resolved] PHP inside CSS?

Spaceship Spaceship
Watch
Impact
11
PHP inside CSS?

Hello,
Another PHP related question that is pretty much essential to my new projects development. Here it is:

Say i had a hex code (example #000000) stored in a PHP database, would it be possible to get the information from the database and use it in a CSS stylesheet to determine a part of the CSS example:

Code:
.header  {
  font-size: 11px;
  font-family: Verdana;
  font-weight: bold;
  color: <? $variable->database_recovery_for_hex_color; ?>;
 }

If this is possible, It would be so great.. I for some reason doubt that it is. If infact it is not possible, I would be very thankful to whoever can provide an alternative way to doing the same thing!

Thanks for reading!
Declan.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
It's possible and easy to do. :o

Save the file as css.php or something, and just add this line before everything else:
PHP:
<?php header("Content-type: text/css"); ?>




edit: Reading the post below, this is for external files (obviously?). As long as it's in the <style> tag's in a .php page, it will run fine.
 
Last edited:
0
•••
This code should be parsed by PHP as long as the CSS is embedded within your script page. If it's an external css files it won't be parsed.
 
0
•••
sdsinc said:
This code should be parsed by PHP as long as the CSS is embedded within your script page. If it's an external css files it won't be parsed.

I saved this as style.css
Code:
<style type="text/css">
<!--
.header  {
  font-size: 10px;
  font-family: Verdana;
  font-weight: bold;
  color: <?php echo "$info->f"; ?>;
  background-color: <?php echo "$info->f"; ?>;
 }
body {
	background-color: #787878;
}
-->
</style>

Then attached it to a php file, In dreamweaver it clearly shows that the background has been changed to 787878 on the PHP file i attached it too. But the .header class doesnt show up when i try to class a table with it.
 
0
•••
I use php in css all the time. Just set the variable beforehand, like so:

Code:
<?php 
$color = 'red';
?>

Then in your css:
Code:
<style type=text/csss>
.whatever {color: <?php echo $color;?>;}
</style>
 
0
•••
LogiK, if you are saving the CSS as it's own file, you don't put <style> .. </style> in it.
Also, I don't think you can give a <table> those properties.
 
0
•••
thats insane.. never knew you could put php in an external css :)
 
0
•••
Or you can always use htaccess to mod_rewrite css as php.

The example Dan showed is good enough for most though.
 
0
•••
I think dan's example would be the best to implement.
 
0
•••
wow we learn something new everyday, thanks for this never knew we could pop php code in our css files.. umm need to edit a few things :)
 
0
•••
Bare in mind that the new CSS PHP file will have to be rendered and sent to the browser everytime the main page is requested... so it won't get cached in the client's browser as it would if it were just a normal CSS include. And if remember correctly, Firefox could have some issues with a CSS file having a PHP extension.

It may be a bit more work, but I think it would run better if you made your main CSS template, and then create a script that caches different versions depending on how you want your styles altered (dynamically) - then just save these dynamic versions of your style sheet on the server with the .css extension, and call the relevant css when needed...

Hope that helps!

A. :)
 
0
•••
If you think the PHP file extension would hurt, what would you think about serving .css files as PHP through the .htaccess, then using the line I posted?
 
0
•••
ZCCool4718 said:
wow we learn something new everyday, thanks for this never knew we could pop php code in our css files.. umm need to edit a few things :)


php can be put into almost any type of document as long as you set it up to run through the php parser.

to get a css file to run through the php parser just create a .htaccess file and place it into the same folder where the css is going to be and put the following line into the file:-

AddType application/x-httpd-php .css
 
0
•••
The server executes PHP before CSS/HTML, so something like this does work

PHP:
.class {
 color: "<?php echo $color; ?>";
}

Jason
 
0
•••
Jason that is provided that the script that is contained in runs through the php parser if it does not then it will be output exactly as it reads in your example.
 
0
•••
Right, I was assuming that.

Jason
 
0
•••
peter@flexiwebhost said:
php can be put into almost any type of document as long as you set it up to run through the php parser.

to get a css file to run through the php parser just create a .htaccess file and place it into the same folder where the css is going to be and put the following line into the file:-

AddType application/x-httpd-php .css


Will this CSS file get cached?
 
0
•••
this will spit out norm CSS code to the browser, if your settings on your browser is set to cached then it will. user will not know you have php in your file like norm.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back