开发者

How do I add css to be loaded last in drupal?

drupal_add_css('override/files/style.css');

This kind of statement can't ensure the css is loaded last,

how can I开发者_StackOverflow do the trick?


Use the group attribute :

drupal_add_css(drupal_get_path('theme', 'mytheme') . '/css/styles.css', array('group' => CSS_THEME);


The CSS gets added in the order drupal_add_css() is called, which depends largely on the weight of the module or theme doing the calling, stored in the system table. Drupal.org's instructions on changing module weight also work for themes.

The other factor determining the order of drupal_add_css() is simply the order in which the containing function is called in the code of Drupal. template_preprocess_page(), for example, is always called after template_preprocess_node(), simply because the node goes in the page.


For Drupal 6 use:

drupal_add_css('site.css', 'theme', '', array('weight' => 999))


if you look in the template.php theme file of the ninesixty theme you will find a solution.

The following function are called from the theme_preprocess_page hook:

function ninesixty_css_reorder($css) {
  global $theme_info, $base_theme_info;

  // Dig into the framework .info data.
  $framework = !empty($base_theme_info) ? $base_theme_info[0]->info : $theme_info->info;

  // Pull framework styles from the themes .info file and place them above all stylesheets.
  if (isset($framework['stylesheets'])) {
    foreach ($framework['stylesheets'] as $media => $styles_from_960) {
      // Setup framework group.
      if (isset($css[$media])) {
        $css[$media] = array_merge(array('framework' => array()), $css[$media]);
      }
      else {
        $css[$media]['framework'] = array();
      }
      foreach ($styles_from_960 as $style_from_960) {
        // Force framework styles to come first.
        if (strpos($style_from_960, 'framework') !== FALSE) {
          $framework_shift = $style_from_960;
          $remove_styles = array($style_from_960);
          // Handle styles that may be overridden from sub-themes.
          foreach ($css[$media]['theme'] as $style_from_var => $preprocess) {
            if ($style_from_960 != $style_from_var && basename($style_from_960) == basename($style_from_var)) {
              $framework_shift = $style_from_var;
              $remove_styles[] = $style_from_var;
              break;
            }
          }
          $css[$media]['framework'][$framework_shift] = TRUE;
          foreach ($remove_styles as $remove_style) {
            unset($css[$media]['theme'][$remove_style]);
          }
        }
      }
    }
  }

  return $css;
}


Use the weight attribute :

drupal_add_css(drupal_get_path('theme', 'mytheme') . '/css/styles.css', array('weight' => 999));

Note : CSS added in your theme's info will always be last, so use the same function as before to change the weight of your stylesheets declared in theme's info file.


This worked for me. Adds the css after the last css on the page

$module_path = drupal_get_path('module', 'my_module');
  drupal_add_css($module_path . '/css/my_module.css', array('weight' => 99999, 'group'=>CSS_THEME));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜