Using drupal_add_css to dynamically add inline CSS based on term reference I'm trying to create a CSS class called .bg-unit-color which will change depending on the taxonomy reference chosen  

I've set up a course category taxonomy and then set up a color field within it using jQuery Colorpicker module.  I've got to the point where I can render the color in my node--example.tpl.php file with the following:

&lt;?php
$term=taxonomy_term_load($node-&gt;field_example_category['und'][0]['tid']);
$category_color=field_view_field('taxonomy_term',$term,'field_example_category_color');
echo render($category_color);
?&gt;

I've also used the drupal_add_css function in my template.php file, so this works:

$new_color='black';
drupal_add_css(".bg-unit-color {background-color: $new_color;}", 'inline');

However I can't marry the two together to create what I'd like to do which is this:

drupal_add_css(".bg-unit-color {background-color: $category_color;}", 'inline');

Any help would be massively appreciated. Thanks in advance
If using render you should attach the css to that render array.

$term = taxonomy_term_load($node-&gt;field_example_category['und'][0]['tid']);
$category_color = field_view_field('taxonomy_term',$term,'field_example_category_color');
$name = $term-&gt;name;
$category_color['#attached']['css'][] = array(
  'data' =&gt; ".bg-unit-color {background-color: $name;}", 
  'type' =&gt; 'inline',
);