-2

i want to put variable inside css but i don't know how to do it. I have created a php file named style.css.php containing this simple example:

   <?php $background = 'blue'; ?>
    <style type="text/css"> 
       body {background: <?php echo $background; ?>;}

      </style>

But is this a good method? I need to create a customizable theme. The other universal stylesheets are in a normal css file.

Please help.

leofabri
  • 141
  • 12
  • 1
    You should read about LESS and/or SASS. – STT LCU Jul 22 '15 at 09:02
  • @STTLCU, SASS is good idea! But, there is one benefit of using php, i.e. it helps to retrieve images from database to be used in css, say – rohankvashisht Jul 22 '15 at 09:05
  • Storing images in databases isn't generally a good idea, though. The key point of my comment is: while you do know PHP, don't try to always solve problems using PHP but be always on the lookout for newer, better tools. :) – STT LCU Jul 22 '15 at 09:11

2 Answers2

3

This question already has an answer.
By the way these link will help you to implement php inside css:

https://css-tricks.com/css-variables-with-php/
How to use PHP inside css file
How do i run PHP inside CSS

Community
  • 1
  • 1
rohankvashisht
  • 563
  • 1
  • 6
  • 12
  • this should not be the accepted answer, not only is it a non-answer, it includes links that might or might not exist. terrible. – eballeste Mar 31 '20 at 08:15
0

I think your approach is already good enough, I'm guessing you are including your style.css.php in the head, potentially putting a lot of CSS there, which isn't necessarily a bad thing.

You don't really have to include your CSS files if you need them on every page anyway, putting them directly into the file saves a HTTP Request but makes your file bigger - but bigger file size doesn't matter if you would load the css file anyway. This way you have even finer control over what gets loaded and what does not, which usually shouldn't be necessary.

daga
  • 63
  • 6