Adding CSS to admin pages in Drupal 7

Tags: Drupal, CSS.

Sometimes it is necessary to only add css to the administrative areas of a site. In Drupal 7 this can be accomplished with a simple precocess_html. For portability it can easily be made into a module. The code below will look in "/sites/all/themes/your-theme/css/" for admin.css.

function admin_css_preprocess_html(&$variables) {
  if(arg(0) == 'node' && arg(2) == 'edit' || arg(0) == 'admin') {
    // reference your current theme
    $theme_path = drupal_get_path('theme', variable_get('theme_default', NULL));
    // reference your own stylesheet
    $admin_css = $theme_path . '/css/admin.css';
    drupal_add_css($admin_css, array('weight' => CSS_THEME));
  }
}