Drupal8: Add class to body tag

Here is the solution for adding our custom class to body tag using preprocess_html() function.

function hook_preprocess_html(&$variables) {
$variables[‘attributes’][‘class’][] = ‘class-name’;
}

If you want to add class for all node of specify content type, you can do like below

function hook_preprocess_html(&$variables) {
if ($node = \Drupal::request()->attributes->get(‘node’)) {
if($node->getType() == ‘content_type_name’) {
$variables[‘attributes’][‘class’][] = ‘class-name’;
}
}
}

Hope this will helpful to you…

 

Advertisements