# Dynamic CSS

**URL:** <https://www.ruby-forum.com/t/dynamic-css/66328>\
**Category:** Rails\
**Created:** [August 24, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/dynamic-css/66328 "2006-08-24T19:44:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [August 24, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/dynamic-css/66328/1 "2006-08-24T19:44:41Z")

</div>

Hey all,

I have a scenario where I am loading objects to display on the  
screen, and they each have colors in the Model. I’d like to use these  
hex values to generate CSS code so I can display them cleanly, any  
suggestions?

I saw the rcss gem and it looks like that might work, but in order to  
reduce the DB hit, I’d have to have a seperate CSS file that gets  
called _only_ when a jack is being displayed so the RCSS isn’t calling  
the DB each time to create the CSS for the jack colors, am I right?

–

- Nic

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [August 25, 2006, 9:50am UTC](https://www.ruby-forum.com/t/dynamic-css/66328/2 "2006-08-25T09:50:15Z")

</div>

> I have a scenario where I am loading objects to display on the  
> screen, and they each have colors in the Model. I’d like to use these  
> hex values to generate CSS code so I can display them cleanly, any  
> suggestions?
> 
> I saw the rcss gem and it looks like that might work, but in order to  
> reduce the DB hit, I’d have to have a seperate CSS file that gets  
> called _only_ when a jack is being displayed so the RCSS isn’t calling  
> the DB each time to create the CSS for the jack colors, am I right?

Why do it by generating a new css file? You can just add  
‘style=“color: #mycolor”’ to the elements that need dynamic coloring.  
You can then use the rails caching to lighten the load.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [August 25, 2006, 11:38am UTC](https://www.ruby-forum.com/t/dynamic-css/66328/3 "2006-08-25T11:38:29Z")

</div>

On 25 Aug 2006, at 08:40, Peter De Berdt wrote:

> Why do it by generating a new css file? You can just add  
> ‘style=“color: #mycolor”’ to the elements that need dynamic  
> coloring. You can then use the rails caching to lighten the load.

It’s not very good practice to put styles within HTML, assuming this  
is what you were getting at.

* * *

Alastair M.  
Standards compliant web development with Ruby On Rails, PHP and ASP  
[www.kozmo.co.uk](http://www.kozmo.co.uk)  
07738 399038

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [August 25, 2006, 12:10pm UTC](https://www.ruby-forum.com/t/dynamic-css/66328/4 "2006-08-25T12:10:31Z")

</div>

On 25 Aug 2006, at 11:27, Alastair M. wrote:

> > > order to  
> > > this is what you were getting at.  
> > > Sure, I totally agree, but as I said before, if it’s for the better,  
> > > you sometimes need to bend the rules a bit. Generating dynamic css  
> > > will have the browser reload the whole css every time around instead  
> > > of using the locally cached version. Not really very efficient, as  
> > > 95% of the css file will be identical.

Now you have two options: split up your css file in a static part  
(that can be cached) and a dynamic one (which will generate another  
request to the server each time around) or just use styling in your  
XHTML file. I tend to favor the last option, because it doesn’t need  
yet another plugin. Also remember that if you’re planning to use  
scriptaculous visual effects, you’ll have to use the style attribute  
anyway (display: none;).

Like always, extremism leads nowhere 🙂

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [August 25, 2006, 12:19pm UTC](https://www.ruby-forum.com/t/dynamic-css/66328/5 "2006-08-25T12:19:59Z")

</div>

Peter De Berdt wrote:

> Now you have two options: split up your css file in a static part  
> (that can be cached) and a dynamic one (which will generate another  
> request to the server each time around) or just use styling in your  
> XHTML file. I tend to favor the last option, because it doesn’t need  
> yet another plugin. Also remember that if you’re planning to use  
> scriptaculous visual effects, you’ll have to use the style attribute  
> anyway (display: none;).

I agree, that is the best approach. The only minor change I would do is  
to instead using inline styles, use the tag in header. You can  
put all variable CSS selectors there and not clutter the html itself.

Dynamically generated CSS files were in fact doubling the load on the  
server so I removed them entirely from development version of RCSS[1].

Cheers,

Bragi

[1] [http://rcss.rubyforge.org](http://rcss.rubyforge.org)
