5

I am using a contributed theme and I would like to add a bit of CSS.

The theme uses a lot of stuff that isn't inherited by sub-themes (settings, colors, etc), and I don't want to create a sub-theme just for the small amount of CSS I need.

What would you suggest in this case?

5 Answers 5

4

Of course you could just change the theme's CSS but the cleanest/most elegant thing to do here is create a simple custom module that implements hook_init() and uses drupal_add_css() to add your custom stylesheet:

function mymodule_init() {
  $options = array(
    'group' => CSS_THEME,
    'every_page' => TRUE,
    'weight' => 9999
  );
  drupal_add_css(drupal_get_path('module', 'mymodule') . '/mymodule.css', $options);
}

Create mymodule.css in your custom module's folder and put all your css tweaks there.

4
  • Thanks. I think it should be drupal_get_path('module', 'mymodule') . '/mymodule.css'. Otherwise that's what I need.
    – daphshez
    Apr 17, 2011 at 10:22
  • sorry, I think that's still wrong there, you need the concatenate the path and the css name, these are not two separate parameters.
    – daphshez
    Apr 19, 2011 at 7:48
  • argh!! right again, edited :)
    – Alex Weber
    Apr 19, 2011 at 10:29
  • I see this has been getting attention, my answer was from 2011 please don't use hook_init() to add js in drupal 7! :)
    – Alex Weber
    Aug 4, 2014 at 13:39
6

My understanding is to never use hook_init if possible. It's a hook that's call in the early stages of the bootstrap process and can produce some funky results. The API docs clearly state:

To add CSS or JS that should be present on all pages, modules should not implement this hook, but declare these files in their .info file.

I'd suggest adding a stylesheets[all][] entry in the .info file -> Adding style sheets

2
  • Can I specify stylesheets in a module's info file? Nothing about that in the doc (drupal.org/node/542202)...
    – daphshez
    Apr 19, 2011 at 7:51
  • To quote "Webchick" -> drupal.org/node/328357#comment-1122233 I've always been taught that you should only include CSS/JS files on the pages you actually need them, so for modules, the inclusion of scripts/styles should always be done in the theme function of whatever's being displayed. Is that true? If so, is this a good idea? For example, jQuery UI module loading all of its scripts on every single page, regardless of whether it was actually used, would cause a tremendous amount of code to be downloaded each page request...
    – DeeZone
    Aug 11, 2011 at 18:15
1

If you want to theme a Form API form consider using #attached instead of hook_init()

see http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html#attached

0

The easiest thing to do is create an additional stylesheet with your changes and add it to the bottom of the load order in the theme's .info file. All your changes live in a separate file from the base theme, keeping it clean, and the stylesheet is loaded by the theme, where you would expect to see it. This seems far simpler and clearer than writing a module to add a stylesheet. If you put the stylesheet load in a module, enable the module, then change the theme to a different theme, that module is still loading that extra stylesheet on top your new (different) theme.

My .02, keep it in the theme.

1
  • Thanks, this is was I did at first, but this is still a change in the contrib theme, and I would really want to avoid that. First, for the normal software engineering considerations. And also, because I am working in a multi-site environment, and I would prefer to keep the theme intact for other sites.
    – daphshez
    Apr 19, 2011 at 8:01
0

What I would do, if it were only for minor css changes, is add a 'custom.css' file to the theme folder and the .info file and put my changes in there.

To avoid the theme updating (breaking my custom css), I'd also delete the version information and drupal.org-generated content (if it has any) from the theme's .info file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.