add CSS programmatically for a specific page in .theme file I have a question about adding a CSS file programmatically depending on which site I visit. In Drupal 7 I could achieve this by calling the following line in the preprocess_html method in the template.php file:

function template_preprocess_html(&amp;$vars) {

    $css_file_name = 'specific-page';
    drupal_add_css(path_to_theme() . '/css/pages/'.drupal_clean_css_identifier($css_file_name).'.css', array('group' =&gt; CSS_THEME));

}


But I can't achieve the same with Drupal 8. I tried the same like in 7 just with the new Drupal 8 way. So here are my tries in the template.theme file

use Drupal\Component\Utility\Html;


1.)

function template_page_alter(&amp;$page) {

  $css_file_name = 'specific-page';
  $clean_alias = Html::cleanCssIdentifier($css_file_name);
  $path_to_css = drupal_get_path('theme', 'template') . '/css/pages/' . $clean_alias  . '.css';

  $page['#attached']['css'][] = $path_to_css;

}


2.)

    function template_preprocess_page(&amp;$vars, $hook) {

      $css_file_name = 'specific-page';
      $clean_alias = Html::cleanCssIdentifier($css_file_name);
      $path_to_css = drupal_get_path('theme', 'template') . '/css/pages/' . $clean_alias  . '.css';


      $local_css = array(
        '#attached' =&gt; array(
          'css' =&gt; array(
            $path_to_css =&gt; array(
              'group' =&gt; CSS_THEME,
              'weight' =&gt; 9999),
          ),
        ),
      );

      drupal_render($local_css);

    }


Here I get the error: You are not allowed to use css in #attached

3.)

function template_page_alter(&amp;$page) {

          $css_file_name = 'specific-page';
          $clean_alias = Html::cleanCssIdentifier($css_file_name);
          $path_to_css = drupal_get_path('theme', 'template') . '/css/pages/' . $clean_alias  . '.css';

          $vars['attached_css'] = array(
            '#attached' =&gt; array(
            'data' =&gt; $path_to_css,
            'options' =&gt; array(
             'group' =&gt; CSS_THEME
            ),
           ),
          );

}


In my template file:

{{  attached_css }}


None of my tries worked so far. I collect the information for the code from serveral tutorials and stackoverflow questions, but it looks like that many of them are very old comparing to the stable release date of Drupal 8. So maybe there are some changes which I forget.
I could solve the problem (thanks to @Eyal).

template.theme :

function template_preprocess_html(&amp;$variables) {

    $css_file_name = 'specific-page';
    $clean_alias = Html::cleanCssIdentifier($css_file_name);

    $activeThemeName = \Drupal::theme()-&gt;getActiveTheme()-&gt;getName();

    //OR

    $activeThemeName = 'template';

    $variables['#attached']['library'][] = $activeThemeName . '/' . $clean_alias;

}


template.libraries.yml:

specific-page:
  css:
    base:
      css/pages/specific-page.css: {}