5.x common.inc drupal_get_css($css = NULL)
6.x common.inc drupal_get_css($css = NULL)
7.x common.inc drupal_get_css($css = NULL, $skip_alter = FALSE)

Returns a themed representation of all stylesheets that should be attached to the page.

It loads the CSS in order, with 'module' first, then 'theme' afterwards. This ensures proper cascading of styles so themes can easily override module styles through CSS selectors.

Themes may replace module-defined CSS files by adding a stylesheet with the same filename. For example, themes/garland/system-menus.css would replace modules/system/system-menus.css. This allows themes to override complete CSS files, rather than specific selectors, when necessary.

If the original CSS file is being overridden by a theme, the theme is responsible for supplying an accompanying RTL CSS file to replace the module's.


$css: (optional) An array of CSS files. If no array is provided, the default stylesheets array is used instead.

Return value

A string of XHTML CSS tags.

See also


4 calls to drupal_get_css()
chameleon_page in themes/chameleon/chameleon.theme
template_preprocess_maintenance_page in includes/theme.maintenance.inc
The variables generated here is a mirror of template_preprocess_page(). This preprocessor will run it's course when theme_maintenance_page() is invoked. It is also used in theme_install_page() and theme_update_page() to keep all the variables…
template_preprocess_page in includes/theme.inc
Process variables for page.tpl.php
_color_page_alter in modules/color/color.module
Callback for the theme to alter the resources used.


includes/common.inc, line 1920
Common functions that many Drupal modules will need to reference.


function drupal_get_css($css = NULL) {
  $output = '';
  if (!isset($css)) {
    $css = drupal_add_css();
  $no_module_preprocess = '';
  $no_theme_preprocess = '';
  $preprocess_css = variable_get('preprocess_css', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update');
  $directory = file_directory_path();
  $is_writable = is_dir($directory) && is_writable($directory) && variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC;

  // A dummy query-string is added to filenames, to gain control over
  // browser-caching. The string changes on every update or full cache
  // flush, forcing browsers to load a new copy of the files, as the
  // URL changed.
  $query_string = '?' . substr(variable_get('css_js_query_string', '0'), 0, 1);
  foreach ($css as $media => $types) {

    // If CSS preprocessing is off, we still need to output the styles.
    // Additionally, go through any remaining styles if CSS preprocessing is on and output the non-cached ones.
    foreach ($types as $type => $files) {
      if ($type == 'module') {

        // Setup theme overrides for module styles.
        $theme_styles = array();
        foreach (array_keys($css[$media]['theme']) as $theme_style) {
          $theme_styles[] = basename($theme_style);
      foreach ($types[$type] as $file => $preprocess) {

        // If the theme supplies its own style using the name of the module style, skip its inclusion.
        // This includes any RTL styles associated with its main LTR counterpart.
        if ($type == 'module' && in_array(str_replace('-rtl.css', '.css', basename($file)), $theme_styles)) {

          // Unset the file to prevent its inclusion when CSS aggregation is enabled.

        // Only include the stylesheet if it exists.
        if (file_exists($file)) {
          if (!$preprocess || !($is_writable && $preprocess_css)) {

            // If a CSS file is not to be preprocessed and it's a module CSS file, it needs to *always* appear at the *top*,
            // regardless of whether preprocessing is on or off.
            if (!$preprocess && $type == 'module') {
              $no_module_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $file . $query_string . '" />' . "\n";
            else {
              if (!$preprocess && $type == 'theme') {
                $no_theme_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $file . $query_string . '" />' . "\n";
              else {
                $output .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $file . $query_string . '" />' . "\n";
    if ($is_writable && $preprocess_css) {

      // Prefix filename to prevent blocking by firewalls which reject files
      // starting with "ad*".
      $filename = 'css_' . md5(serialize($types) . $query_string) . '.css';
      $preprocess_file = drupal_build_css_cache($types, $filename);
      $output .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $preprocess_file . '" />' . "\n";
  return $no_module_preprocess . $output . $no_theme_preprocess;


anon’s picture

All style tags after the first 30 style tags on an HTML page are not applied. Occasionally, you may also receive the following error message:
The page you are looking for might have been moved or had its name changed.

More info: http://support.microsoft.com/kb/262161

Temp fix: Enable "Optimize CSS files" under admin/settings/performance

mikeytown2’s picture

jghyde’s picture

For adding css files dynamically in your template.php, this is the zen method. Change 'YOURTEMPLATENAME' to the name of your template.

In template.php:

// Hypothetical condition about, say, making this only apply to the home page:

 * Override or insert variables into the page templates.
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("page" in this case.)
function YOURTEMPLATENAME_preprocess_page(&$vars, $hook) {
  // Add a style sheet if something is true, in this case, it's the home page:
  if ($vars['is_front']) {
    $css = path_to_theme() . '/css/styles2.css';
    drupal_add_css($css, 'theme', 'all', 1);
  // Rebuild Drupal's css array:
  $css = drupal_add_css();
  // Apply that array to the $styles string to be printed in the  section of page.tpl.php
  $vars['styles'] = drupal_get_css($css);
// */
iamsuriyan’s picture

in page.tpl.php :

i use print $styles;

in include default.css, system.css, etc. but i dont want this.

how to avoid this, please help me!!!!

chrisjlee’s picture

You need to unset the array of styles

$css = drupal_add_css();

lassaad’s picture

* Override or insert PHPTemplate variables into the templates.
function phptemplate_preprocess_page(&$vars) {

// Unset stylesheets.
$css = $vars['css'];
$vars['styles'] = drupal_get_css($css);