[development] Overriding CSS files in themes

Steven Wittens steven at acko.net
Wed Nov 1 10:06:34 UTC 2006


> theme_add_style(drupal_get_path('module', 'mymodule')).'/ 
> mymodule.css');
>
> This is often called in hook_menu() so as to get in as early as  
> possible.
>
> Themes almost always need to override those css files. The way it  
> is done
> is to go to the mymodule directory and edit mymodule.css.
>
> The problem here is when one upgrades, they have to be very careful
> not to overwrite the changes. What is worse, if one is upgrading from
> cvs directly, then there could be conflicts resulting in invalid  
> data in
> the file.

As far as I know, theme_add_style() is not and never has been a  
themable function. In 4.7, it is only once invoked in its themable  
form, which is obviously as a mistake. And, ironically that's inside  
theme_maintenance_page(), which is unthemable by definition. The  
proper function to use in 4.7 is theme('stylesheet_import').

In spite of this, I don't see why you'd need to edit module .css  
files. Due to the CSS cascade, you can just provide more specific CSS  
rules in your theme to override them. Depending on the order the  
theme's stylesheet is added in, you either need to use the same  
specificity, or one level more, for example prefixing the CSS  
selector with "html".
http://www.w3.org/TR/CSS1#the-cascade

In 5.0, this problem has been solved, as the theme_add_style()/theme 
('stylesheet_import') functions have been replaced by drupal_add_css 
(), and themes get a nice structured array of all stylesheets back in  
$css, which they can hack up as much as they want. You can then do  
drupal_get_css($css) to render this structured array into the right  
set of HTML headers.

Steven Wittens



More information about the development mailing list