What is right way to add CSS to some pages?
mattez - April 7, 2009 - 15:54
| Project: | Conditional Stylesheets |
| Version: | 6.x-1.x-dev |
| Component: | Documentation |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Jump to:
Description
Here http://drupal.org/project/conditional_styles is written:
For example, if you are re-generating the $styles variable in your theme's preprocess_page function by calling $vars['styles'] = drupal_get_css() (old versions of the Zen theme did that), you will either need to stop doing that (adding stylesheets with drupal_add_css in your template.php is bad for performance anyway) or, after you are done screwing with $var['styles'], you can $var['styles'] .= $var['conditional_styles']; to add them back in.
What is right way to add own (color) CSS to some custom pages?
Otherwise then edit template.php and add CSS via drupal_add_css / drupal_get_css?

#1
When you have CSS aggregation turned on (and you should), Drupal has to combine all the stylesheets together and save it in a new file.
But when you conditionally include an additional stylesheet via drupal_add_css(), you force Drupal to rebuild the aggregated stylesheet for that page. Which is 1.) a significant performance hit for Drupal and, worse, 2.) a big performance hit for your website’s users because they have to download a new really large aggregated stylesheet. :-p
So, the way to avoid that is to:
<?phpfunction MYTHEMENAME_preprocess_page(&$vars, $hook) {
if ($condition) {
$vars['body_classes'] .= ' conditional-class-1';
}
}
?>
So now the aggregated stylesheet remains the same on each page request, but the styles that get applied depend on the body classes.
#2
JohnAlbin THANX A LOT!
That's what I call right USEFUL clever answer post!
great :)
(more stuff like this here :)
#3
Automatically closed -- issue fixed for 2 weeks with no activity.