0

I have a little question. Can I dynamically change (with php) content of the css style in this way?

<?php
               header("Content-type: text/css; charset: UTF-8");

               $color = "red;";

               ?>

               header      {

                           color:<?php print $color; ?>

                           }    

?>
7
  • 1
    Have you considered trying it? Commented Oct 6, 2013 at 14:07
  • Yes, but its not found. Problem is, that when use the file without <style> and <style> tags, i cant use the contextual help for css. Commented Oct 6, 2013 at 14:08
  • You need to rename your style file to end in .php or use .htdocs to parse .css files as PHP. As for cache, you just need to set that within headers. Commented Oct 6, 2013 at 14:08
  • I'm afraid I do not know how Commented Oct 6, 2013 at 14:11
  • 2
    Anyway this approach is sooo 2006 - check Less / SCSS / stylus. Commented Oct 6, 2013 at 14:20

3 Answers 3

4

Sure it's possible, why not give a try?

Linking the php css in HTML:

<link rel="stylesheet" type="text/css" href="css/name-of-file.css.php">

and in your css.php file you put your code, but without the

<style type="text/css">

so it should look like this

 <?php
           header("Content-type: text/css; charset: UTF-8");
           $color = "red;";
           ?>

           header      {

                       color:<?php print $color; ?>

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

4 Comments

Yes, I know, but if you do not use the <style> tags, contextual help not found :/
problem is, that I need change css dynamically with my php and db. And I need change css properites with php, get from db and save into the db.
I can't see a problem there, type your PHP code in it and have fun. Did you even try it?.. I still don't get your problem.
@user2579395 Did it run?
2

It is better you use your javascript to change the style between php tags.

for example:

if($response){

echo "<script>
    $('#matter_table').css('display', 'block');
</script>";
} else{

echo "<script>
    $('#matter_table').css('display', 'none');
</script>";
}

Comments

0

You should try this

<?php
echo<<_CSS
<style>
body{
color:red;
}
</style>
_CSS; 
?>

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.