0

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 ?>;
}
naresh
  • 174
  • 2
  • 12

2 Answers2

3

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.

Pupil
  • 23,834
  • 6
  • 44
  • 66
  • i want generate dynamic css file in separate folder not with php extention – naresh Oct 07 '15 at 12:13
  • 1
    Apply proper path there for separate folder. You can do it any language, but, the contents should be CSS. – Pupil Oct 07 '15 at 12:15
0

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)

oliverpool
  • 1,624
  • 13
  • 30
  • thank you but i want generate css file dynamically in separate folder help me how it is.. – naresh Oct 07 '15 at 12:17
  • So you want to create and save a CSS file, based on a template? – oliverpool Oct 07 '15 at 12:18
  • Two steps: [get your view into a variable](http://stackoverflow.com/questions/11772651/load-view-into-a-variable) and [save this variable to a file](http://stackoverflow.com/questions/14956300/how-write-a-new-file-in-codeigniter) – oliverpool Oct 07 '15 at 12:27
  • You have two examples in my previous comment: create a `css.php` file in your template folder (with your CSS content and variables), render this template (using my first link) and save it as a `style.css` file using the second link. – oliverpool Oct 07 '15 at 12:32
  • can you provide any previous example link – naresh Oct 07 '15 at 12:34
  • Did you give it a try? – oliverpool Oct 07 '15 at 12:36