Add a class to the <body> in Drupal 8 with a module

Step 1

Create a MODULNAME.libraries.yml file with: dependencies: - core/drupalSettings"!


MODULNAME:
version: VERSION
dependencies:
- core/drupalSettings
- core/drupal

Step 2

Add this hook_preprocess_html() function in your module (MODULNAME.module):


/**
* Implements hook_preprocess_html().
*/
function MODULNAME_preprocess_html(&$variables) {
$variables['attributes']['class'][] = "BODYCLASS";
}

Zwahlendesign: