Home

Lullabot

Lullabot Ideas

We know stuff. We empower you to know stuff too.

On Site Drupal Training

We'll come to you! Graduate from your own on-site courses and become Drupalistas!

How to properly add CSS files

Article by Ted SerbinskiMay 17, 2006 - 4:37pm

As I was working on cleaning up a few isses for the Javascript Tools module, I stumbled upon one issue dealing with overriding CSS styles. In this case, JSCalendar was adding it's styles after a theme's styles were being loaded. Obviously, from a CSS standpoint, it would be impossible for the theme to override and change CSS styles in JSCalendar. This needed to be fixed.

After some digging, I found this comment that explains this problem in more detail and outlines the correct procedure for modules and themes to add CSS files.

To summarize Tom:

<?php
// Modules should do:
drupal_set_html_head(theme('stylesheet_import', base_path() . drupal_get_path('module', 'mymodule') .'/mymodule.css'));
?>

<?php
// user-created PHP pages/blocks/comments/etc. should do:
theme_add_style('misc/local/my_custom_style.css');
// (theme_add_style is also used internally for the style.css files)
?>

<?php
// themes should do:
$output .= theme('stylesheet_import', base_path() . path_to_theme() . '/extra_stylesheet.css');
/* note that this should be BEFORE theme_get_styles, which will reference
  the style.css file and anything added by the user... themers will probably also
  want it located after drupal_get_html_head, which will bring in any module-specific CSS
*/
?>

Comments

merlinofchaos (not verified) on May 17, 2006 - 5:27pm

Can deal with modules that use the wrong method...

I 'fixed' this in my theme by overriding theme_get_styles and reversing the order.

<?php
function phptemplate_get_styles() {
 
$output = '';
  foreach (
array_reverse(theme_add_style()) as $style) {
   
$output .= theme('stylesheet_import', $style->path, $style->media);
  }
  return
$output;
}
?>

May 17, 2006 - 11:12pm Ted Serbinski

Ingenious!

Ingenious!

bjorn (not verified) on May 17, 2006 - 6:21pm

Adding stylesheets for alternate media

I added a Drupal.org handbook page re: the phptemplate theme snippet above.

http://drupal.org/node/64144

-b

Heine (not verified) on May 17, 2006 - 7:59pm

But see also

See also the issue: http://drupal.org/node/60096 => theme('stylesheet_import'... in modules stops block/theme preview

To quote Dries:

Using theme_add_style('misc/local/my_custom_style.css'); appears to make sense.

Erik Mallinson (not verified) on May 18, 2006 - 8:39am

Simply adding a new style

For more flexibility one can include other styles directly in CSS, skipping the PHP code. This can be done by using @import url("my-style.css"); within a stylesheet that is linked in your page.tpl.php. These can even be recursive but browser compatibility gets dodgy.

That method isn't the best way to overwrite any CSS, but it's a quick and easy method to bring in other stylesheets without hardcoding links in phptemplate.

Jeremy Epstein (not verified) on May 21, 2006 - 6:42pm

Very confusing

I too was very confused about how to properly add CSS files for a module, until I found the drupal.org thread that you referenced here. I hadn't realised before what the correct procedure was, or what the effects were of doing it wrong. Now, I've updated my modules to use the correct method.

Thanks for taking the time to document this - hopefully, it will make developers less confused in the future!

Anonymous (not verified) on May 26, 2006 - 3:22am

Is your codeblock functioning correctly?

This line is a little screwie: "drupal_set_html_head(theme('stylesheet_import', base_path() . drupal_get_path('module', 'mymodule') .'/mymodule.css'));"

I guess you may want it "forced" down, simular to how your comment blocks behave but I could be wrong... :)

petition (not verified) on August 13, 2008 - 8:26pm

For more flexibility one can

For more flexibility one can include other styles directly in CSS, skipping the PHP code. This can be done by using @import url("my-style.css"); within a stylesheet that is linked in your page.tpl.php. These can even be recursive but browser compatibility gets dodgy.

That method isn't the best way to overwrite any CSS, but it's a quick and easy method to bring in other stylesheets without hardcoding links in phptemplate.

Anonymous (not verified) on October 6, 2008 - 10:03am

how to add stylesheet for module theme

i have created a filename _.tpl.php and added it to theme's folder so as to override theme hook for that content . I have some abc.css for the above template . Now I want to know where to put this css and how to make drupal know it is the css corresponding to above template

duncanmoo (not verified) on October 7, 2009 - 7:09am

Really old info for Drupal 4

lullabot.com is really a great resource, but I find articles like this one misleading as it is out of date, but Google search regards it highly.

Could you archive the article possibly and others that are out of date? Or how about tagging Drupal4, Drupal5 ...

Thanks

guysaban (not verified) on November 4, 2009 - 10:08am

Easier in Drupal 6

In the .info file of your theme add a stylesheet declaration, as follows:

stylesheets[all][] = new_stylesheet.css

About this 'bot

Ted Serbinski

Ted graduated from Cornell University with a degree in Information Science, Systems, and Technology and a minor in Computer & Electrical Engineering. While working on a 5 month coop at Intel in Massachusetts, Ted quickly realized that building web based tools was far more his forte than debugging...

more

Recent

Drupal Voices 160: Moshe Weitzman on Page Rendering in Drupal 7

Podcast 9.02.2010

Drupal Voices 159: John Albin Wilkins on Drupal 7 Theming

Podcast 9.01.2010

Drupal Voices 158: Emma Jane Hogbin on PHP for Designers

Podcast 8.31.2010

Command Line Basics: More Editing with Vi/Vim

Video 8.31.2010

Lullabot's Back to School Sale

Blog 8.30.2010

Popular

Drupal Voices 160: Moshe Weitzman on Page Rendering in Drupal 7

Podcast 9.02.2010

Drupal Voices 159: John Albin Wilkins on Drupal 7 Theming

Podcast 9.01.2010

Photo galleries with Views Attach

Article 6.01.2009

Drupal Voices 158: Emma Jane Hogbin on PHP for Designers

Podcast 8.31.2010

Announcing BeautyTips, a jQuery Tooltip Plugin

Article 10.20.2008
 
  • Home
  • Services
  • Events
  • Ideas
  • Store

Connect the Bots:

Twitter Facebook YouTube blip.tv All Posts Newsletter
  • Ideas
  • Blog
  • Podcasts
  • Videos
  • About
  • Contact
  • Jobs
  • Services
    • Training
  • Events
    • Training Workshops
    • Other Events
    • Conferences
    • Calendar
  • Products
    • Videos
    • Books
    • Swag
  • Ideas
    • Blog
    • Podcast
    • Videos
  • About
    • Philosophy
    • Team
    • Presskit
  • Contact
    • General
    • Work Inquiries
    • Mailing List