Community Documentation

Adding style sheets

Last updated January 16, 2012. Created by LeeHunter on August 28, 2007.
Edited by JohnNoc, earthday47, ingaro, mariolina. Log in to edit this page.

This page explains how to add a style sheet using the .info file of a theme. To add a style sheet programmatically, see the API functions page. Styling themes purely through CSS is possible with the information provided here.

Notes:

  • When working with style sheets, make sure that CSS Optimization is disabled. It is located in "Administer > Site configuration > Performance". CSS Optimization aggregates all of the style sheets for a site in order to improve performance. When CSS Optimization is enabled, no changes to your style sheets will be reflected on your site until the aggregated styles are cleared. You can enable CSS Optimization again when you're done modifying your style sheets.
  • The .info file is cached. Adding or removing any styles will not be detected until the cache is cleared and the revised .info is read. (Do not confuse this with the theme registry.) You must clear the cache to see the changes.
Adding style sheets:

In Drupal 6, by default, a "style.css" file will be used from your theme when no other styles are defined inside the .info file. In Drupal 7, the style.css file will be used only if it is specified in the .info file. Adding other styles is as simple as defining a new 'stylesheets' key with its media property and the name of the style sheet. Keep in mind that defining custom styles will prevent the default "style.css" from loading. Remember to explicitly define the default style sheet if your theme uses it.

; Add a style sheet for all media
stylesheets[all][] = theStyle.css

; Add a style sheet for screen and projection media
stylesheets[screen, projection][] = theScreenProjectionStyle.css

; Add a style sheet for print media
stylesheets[print][] = thePrintStyle.css

; Add a style sheet with media query
stylesheets[screen and (max-width: 600px)][] = theStyle600.css
   

A few notes:

  • Note the empty square brackets between the [media] and = styleName.css. These are always empty and denote that each stylesheet is appended to an array, as in php.
  • The order in which the styles are listed in the head of the page will reflect the order it is defined here.
  • The style sheets can be placed in sub-directories, i.e., stylesheets[all][] = stylesheets/styleName.css. Useful for organizing style sheets.
  • However, it is recommended that sub-directories be kept to one level, i.e.,stylesheets[all][] = css/foo/styleName.css may cause a problem with some templates. Safer is stylesheets[all][] = css/styleName.css or stylesheets[all][] = foo/styleName.css.

Adding external stylesheets
To use a stylesheet external to your theme, such as one hosted on a CDN, you cannot use the themes .info file. Instead you can add this in template.php. In Drupal 7 do this as follows:
<?php
function mytheme_preprocess_html(&$variables) {
 
drupal_add_css('http://fonts.googleapis.com/css?family=News+Cycle', array('type' => 'external'));
}
?>

In Drupal 6 you cannot specify a stylesheet as being external. Instead you will need to use the theme_preprocess_page to add a &lt;link&gt; element as follows:
<?php
function mytheme_preprocess_page(&$vars, $hook) {
 
$vars['head'] .= '<link '. drupal_attributes(array(
   
'rel' => 'stylesheet',
   
'type' => 'text/css',
   
'href' => 'http://fonts.googleapis.com/css?family=News+Cycle')
  ) .
" />\n";
}
?>

Comments

Drupal 6 has no theme_preprocess_html

The Drupal 6 snippet is wrong. There's no theme_preprocess_html() function, it has to be put in theme_preprocess_page().

EDIT: I edited the page itself, so it's right now.

-Wes

D7 code issue

I am having an issue with the D7 code. I am attempting to point to my sites folder to access my custom css file. here is what I have

<?php
     
function mytheme_preprocess_html(&$variables) {
       
drupal_add_css('http://www.website.com/sites/default/themes/css/css?family=News+Cycle', array('type' => 'external'));
      }
?>

For some reason the css file cannot be access. When I move the style sheet to the theme/css and edit the .info file the stylesheet can be accessed.

I am not really sure how this code works. The actual file path to the file is http://www.website.com/sites/default/themes/css/my_css.css

The folders have a permission set to 755 for all folders and the css file.

Any ideas?

Spineless

check the source code

It sounds like you are trying to link to that css externally because you know the folder path but not about the characters Drupal added.

Even with the performance settings off, and the settings are right in drupal 7, you will notice that by looking at the rendered code in your browser that Drupal has added some extra characters to the css file (example: http://www.website.com/sites/default/themes/css/my_css.css?7thusik). Right click in your website and click view source. Look in the head section above the body and you will see what i am talking about. I don't exactly know the reason why. It may be for security, rights protection to css design, performance or just PHP reasons. It might even be in our best interest for it to be that way.

add external css

This:

<?php
     
function mytheme_preprocess_html(&$variables) {
       
drupal_add_css('http://fonts.googleapis.com/css?family=News+Cycle', array('type' => 'external'));
      }
    
?>

didn't work for my drupal 7.

but that:

function mytheme_preprocess_html(&$variables) {
drupal_add_css('http://www.somewhere.com/style.css', array('group' => CSS_THEME, 'preprocess' => FALSE));
}

did.

Thank you for this page!

Being an HTML/CSS-coding non-programmer with (at the time) no Drupal experience who inherited a very complex Drupal site (on which the money ran out, requiring the developers to wrap it up quickly), I have not found a more useful bit of documentation on drupal.org than this:

  • When working with style sheets, make sure that CSS Optimization is disabled. It is located in "Administer > Site configuration > Performance". CSS Optimization aggregates all of the style sheets for a site in order to improve performance. When CSS Optimization is enabled, no changes to your style sheets will be reflected on your site until the aggregated styles are cleared. You can enable CSS Optimization again when you're done modifying your style sheets.

Trying to reverse engineer a site when you can't figure out where the styles are coming from is incredibly frustrating. Turning off CSS Optimization made it instantly possible to inspect elements, figure out which stylesheet(s) are creating any given style, and then edit the CSS files.

Thank you to mariolina for your edit of the above text which, while simple, was profoundly helpful.

nobody click here