Dynamic CSS using php

折月煮酒 提交于 2020-07-09 17:07:19

问题


Currently, I am working on PHP codeignitor framework. I have some properties suppose like color,fonts etc. saved in my database for one of fields. All I want to do is, I have some classes in the css file with default values. Like ex. I have a css class as below:

     .text_box{
           color: pink;
           text-decoration: none;
           background-color: transparent !important;
           transition: all 0.2s ease-in-out 0s;
       }
       .text_box:hover{
             color: blue;
             text-decoration: none;
             background-color: transparent !important;
             transition: all 0.2s ease-in-out 0s;
        }

In my database, I have set color property as yellow for text box. I want to change it dynamically. means when I change that color from input field, it should automatically get converts into the color I want in css property.

I dont know exactly whether my question is correct or not. I have goggled many links but didn't get the relevant solution to my scenario.

Using PHP how can create such dynamic changes.

Thank you.


回答1:


You can do it using jQuery.

var color = "value from DB";
$('.text_box').css({ 'color' : color, });

or in PHP:

<?php
$color = 'value from DB'; //you have to get the value from db
?>
<style>
.text_box { 
color: <?php echo $color; ?>;
}
</style>

Hope this works.




回答2:


You can check Smarty

Basically, you read the values from your database and then you assign them to the template and then you will get dynamically loaded classes.




回答3:


Php or javascript won't change your css file directly, but what you want to do is use Php or javascript to dynamically assign an id (#) to your HTML element that has the associated css you want.

#pink.text_box{
       color: pink;  //...
}
#blue.text_box{
       color: blue;  //...
}

And then use your javascript or php code to assign the id to the element you need




回答4:


Check if you can use color picker tool. This is the best solution I think

http://www.dynamicdrive.com/dynamicindex11/yuicolorpicker/




回答5:


Try using multiple css files like

css_red.css css_blue.css css_green.css with different default colors.

Then in php echo your stylesheet <link href="<?php echo "css_yourcolor";?>.css" >



来源:https://stackoverflow.com/questions/42576453/dynamic-css-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!