How to add CSS classes to body tag based on Drupal path

Add custom CSS classes to <body> tag of your theme.

 

/**
* Implements template_preprocess_html().
*/
function YOUR_THEME_preprocess_html(&$vars) {
  $path = 'your-drupal-page';
  if (drupal_match_path($_GET['q'], $path) || drupal_match_path(drupal_get_path_alias($_GET['q']), $path)) {
    $vars['classes_array'][] = 'page-my-css-class';
  }
}

The $path can be more than one path, separated with newline “\n”.

Leave a Reply

Your email address will not be published.