Is there a way to override just some CSS rules used by the admin theme, without touching the admin theme itself (via some theme functions or module hooks)? For example, I'd like to add a text-align: right to every integer field in admin forms (and in edit nodes too).

  • 5
    Perfectly valid question that should not be closed. There is often a need to tweak the admin theme (typically Seven) where creating a new sub theme would be overkill. – Kari Kääriäinen Dec 3 '15 at 9:19

I did it just using:

function mymodule_preprocess_html(&$variables) {

  global $theme;

  if ($theme === variable_get('admin_theme', 'seven')) {

    // Reference your custom stylesheet.
    drupal_add_css(drupal_get_path('module', 'mymodule') . '/css/mymodule.css', array('weight' => CSS_THEME));

That's a bit more readable for me.

| improve this answer | |
  • 1
    Worked perfectly for me, too. Clean solution. – Sasha Grievus Jun 18 '15 at 12:36
  • Is there a drupal 8 version of this handy code? – petergus Jan 21 '17 at 11:49

There are a number of ways to do this but not all approaches are equal in terms of affecting the performance of your website. For example:

  1. If you copy the admin theme or create a subtheme based off of the admin theme (note, this won't work with Drupal Commerce, as it already has a subtheme based off of Shiny) - and then add a css file via a .info call, you will load the CSS on every page.
  2. If you copy the admin theme, or create a subtheme based off of the admin theme, you can use the hook drupal_add_css() in your template.php file. This will allow you to call the stylesheet conditionally, on certain pages only for example, or for certain browsers. This works alright but you're already calling more files than necessary.
  3. Create your own module, and call drupal_add_css() from it. As far as I know, this is the 'lightest' way to alter the admin theme without touching the original and will work if your admin theme is already a subtheme (for ex. Drupal Commerce Kickstart Admin theme is a subtheme of Shiny). I use this method because it will work in all circumstances. See below for an example:

Create a directory called 'mymodule' (use whatever name you want), create these files within it, and then place it within your sites/all/modules/custom directory. I've added comments to the code below so you can see what's happening.

  • mymodule.info
  • mymodule.module
  • css/mymodule.css

mymodule.info contains:

name = mymodule
description = Custom alterations for admin pages on my website
core = 7.x

mymodule.module contains:

function mymodule_preprocess_html(&$variables) {
  // Add conditional stylesheets for admin pages on admin theme.
  if (arg(0) === "admin") {
    // Reference your current admin theme.
    $theme_path = drupal_get_path('theme', 'commerce_kickstart_admin');
    // Reference your own stylesheet.
    drupal_add_css(drupal_get_path('module', 'mymodule') . '/css/mymodule.css', array('weight' => CSS_THEME));

css/mymodule.css will then have your styles which are added to the admin theme. Clear your caches, enable this module and you're off to the races! Learn more about drupal_add_css here.

| improve this answer | |
  • 2
    No need for the $theme_path variable if you use the current module path... and CSS_THEME is not a weight but a group: array('group' => CSS_THEME) – MrUpsidown Sep 23 '15 at 13:47
  • Is there a drupal 8 version of this handy code? – petergus Jan 21 '17 at 11:49

What I do is create a sub-theme for whatever admin theme I'm using with just the .info, a unique CSS file (usually named overrides.css), and a template.php file if necessary.

| improve this answer | |

What about CSS Injector?

I don't know if it allows a theme-dependant configuration rule, but I assume it does allow one based on the path (so admin/, node/add/, node/*/edit should do the trick).

| improve this answer | |
  • Good idea. Tried it, but it seems not to work (the CSS rule is not actually saved!). I also have doubts that it would work with the overlay system (seems the problem being in file_unmanaged_save_data() saving nothing. But my setup is kinda weird - a ubuntu box inside an OSX vbox). – Claudio May 28 '11 at 10:14
  • Hm. Maybe a bug or something like that in the current version. I think it should work in theory inside the overlay. It is also able to show the overlay with a different theme.. You could also do this in a simple custom module I guess, save your css into a file, implement hook_init() and use drupal_add_css() if some conditions apply. – Berdir May 28 '11 at 10:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.