Attach dynamic inline css to Is it possible to add inline CSS to into the &lt;head&gt; section of a Drupal 8 page? My expected result should look like this:

&lt;head&gt;
&lt;style&gt;
.my-custom-class {
  background-color: red;
}
&lt;/style&gt;
&lt;/head&gt;


I've tried to add it to hook_preprocess_HOOK() but it will not show up.

function my_module_preprocess_html(&amp;$variables) {
  $variables['html']['#attached']['css'][] = array(
    'type' =&gt; 'inline',
    'every_page' =&gt; TRUE,
    'data' =&gt; '.my-custom-class { background: ' . $dynamic_color . '; }',
  );
}


I've tried a few other things but it did not work either. Whats the right way to do this?
After doing some more research i've found an answer to my problem. The main reason for not finding a solution was, they don't want you to do this in Drupal 8. 

From their documentation:

https://www.drupal.org/developing/api/8/assets#dynamic-css-js


  If the dynamic CSS/JS is built for each request, then you enter the truly advanced territory. This is hard, and for a good reason: per-request dynamic assets have to be built on every single request and therefore slow Drupal down. We want to make it hard to make Drupal slow down, so this is why we don't offer a nice API for this — since we don't want you to do it.


This makes sense and there is a short description how to get it to work anyway but its lacking of an example. This is what i've done to get it to work:

function my_module_page_attachments_alter(array &amp;$page) {
  $background_color = '#000';
  $page['#attached']['html_head'][] = [
    [
      '#tag' =&gt; 'style',
      '#value' =&gt; '.my-custom-class { background: ' . $background_color . '; }',
    ],
    'my-custom-module'
  ];
}