-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.

3
  • 1
    You should read about LESS and/or SASS. Commented Jul 22, 2015 at 9: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 Commented Jul 22, 2015 at 9: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. :) Commented Jul 22, 2015 at 9:11

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

this should not be the accepted answer, not only is it a non-answer, it includes links that might or might not exist. terrible.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.