how to generate a dynamic css file using php or codeigniter

给你一囗甜甜゛ 提交于 2021-01-29 07:45:58

问题


I want to develop a project with dynamic css file ,i mean i want to apply styles dynamically with out using static css file. I want to apply styles like colors, borders, width, heights etc..from one form fields and those are stored in database and i want to get values from database and apply changes dynamically in css file(styles), project css applied as dynamically.How can i write code in this scenario

.css file

#border
{
  border: 30px;
}
#color
{
  color: red;
}

like this... php form view

     <h1 id='color'>hello world</h1>
     <input type='text' id='border' value='hello'/>

i want my .css file like this

  #border
{
  border: <?= $settings[0]->border ?>;
}
#color
{
  color: <?= $settings[0]->color ?>;
}

回答1:


You can do it using create a .php file.

Add logic to it, make it a CSS file (with .php extension).

Add header parameters in (start of file) PHP file to tell Server that this is a CSS file.

<?php
header('Content-Type: text/css');
?>

Now, include the the file as a CSS file.

e.g.

<link rel="stylesheet" href="CUSTOM_STYLES.PHP"/>

This inclusion of PHP file with CSS body in it will work as a dynamic CSS file.




回答2:


You can inline this part of css into your HTML template:

<style>
#border
{
  border: <?= $settings[0]->border ?>;
}
#color
{
  color: <?= $settings[0]->color ?>;
}
</style>

Or you could generate a CSS file, when the $settings value changes (to take advantage of browser caching)



来源:https://stackoverflow.com/questions/32991789/how-to-generate-a-dynamic-css-file-using-php-or-codeigniter

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