How do I include admin.css in my theme so that it's loaded in the admin section (and preferably only the admin section)?

Right now when I add admin.css via the x.libraries.yml file, it's only loaded on the front-end.


So... based on your comments/answers I've created a module admin-css. But nothing happens. The file isn't loaded. What am I doing wrong?

Inside admin_css.info.yml :

name: Admin CSS
description: Alters the Admin CSS for better usability.
package: Custom

type: module
core: 8.x

Inside admin_css.libraries.yml :

  version: 1.x
      css/admin.css: {}

Inside admin_css.module :


function admin_css_attachments(array &$attachments) {
    $is_admin = \Drupal::service('router.admin_context')->isAdminRoute();
    if ($is_admin) {
      $attachments['#attached']['library'][] = 'admin_css/admin-css';

  • are you using the same themes in front-end and backend ?
    – rémy
    Feb 22, 2017 at 11:03
  • No I'm not, I'm using the standard theme for back-end. But I'd like to include an admin.css in my theme, just for a few minor changes to the admin section.
    – Pim
    Feb 22, 2017 at 11:05

2 Answers 2


Unless you use the same theme for front and backend, that's not the right approach.

If something transcends the theme layer, e.g. you have functionality that you need to apply to two separate themes, it belongs in a module.

See Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 module for instructions to actually accomplish that, specifically the section on Attaching a library to all (or a subset of) pages which contains a contrived code example:

function contextual_page_attachments(array &$attachments) {
  if (!\Drupal::currentUser()->hasPermission('access contextual links')) {

  $attachments['#attached']['library'][] = 'contextual/drupal.contextual-links';
  • I'm using the standard theme for backend, but I'd like to include an admin.css in my theme, just for a few minor changes to the admin section. Literally 6 lines of CSS. Is it at all possible?
    – Pim
    Feb 22, 2017 at 11:07
  • 1
    Of course :) See the code example. It will need to be in a module though, for obvious reasons a theme's functionality is only loaded/invoked when it's actively being used. Executing functionality from one theme when a different theme is being used to render the page could be disastrous
    – Clive
    Feb 22, 2017 at 11:08
  • I've updated the question, could you take a look at it? Thanks!
    – Pim
    Feb 22, 2017 at 11:54
  • 1
    You'll need to change the logic in the attachments hook function to match your requirements, that's just a contrived example that uses a different condition. Searching "programatically determine admin page in drupal 8" should give you the answer
    – Clive
    Feb 22, 2017 at 12:12
  • I've done just that (based on stackoverflow.com/questions/4483200/…), but still no luck.
    – Pim
    Feb 22, 2017 at 12:19

The hook is called "hook_page_attachments" so you have to rename your function from

function admin_css_attachments(array &$attachments) {


function admin_css_page_attachments(array &$attachments) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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