Is it possible to add css class to the page's body tag with rules when certain conditions are met?


Assuming you are using the Pathauto module to automatically create semantic paths your content pages based on the menu path you can use the page's path to create the classes you are looking for:

function THEMENAME_preprocess_html(&$vars) {
  $path = drupal_get_path_alias();
  $aliases = explode('/', $path);

  foreach($aliases as $alias) {
    $vars['classes_array'][] = drupal_clean_css_identifier($alias);

Yes - the Context module allows you to add CSS classes to the body (Theme HTML condition) based on whatever events you specify. You would then combine this with the Context Rules module which extends Context to allow for context events from Rules.

Tags: Rules

Similar questions

Add a class to the <body> tag on a node view
Is there any module that adds a field to all nodes and then add the value of that field as a class on the tag of the page when the node is displayed?
How to add custom class in body tag on form submit?
I have created custom module with custom form, in which I have added input type select. Form submission is working fine, but I wanted set select option value as custom class for BODY tag.
How do I add a node ID as class to the <body> tag?
In Drupal 7, there is a class like path-node-NID (with NID replaced by the node ID of the node being viewed) added to the <body> tag so we can easily add CSS styles to specific nodes. This is missing in Drupal 8. How can I add the node ID as CSS class to the <body> tag?
Add Taxonomy ID class to the body tag (D8)
I have a taxonomy vocabulary whose various parents and children need to have different styles. I would need the tid of the every taxonomy page to appear as a class in the body tag in order to make CSS selectors. I tried using this to work out a solution, but it appears to work only for nodes. I get a class called path-taxonomy for every taxonomy te...
Editing the class attribute for a<body> tag in a drupal page
I'm working on a drupal website that has a different class for the tag of each page. I'm trying to figure out how to edit this class attribute. Does anyone know?
How do I add a css class to body from a page callback function?
I have a module that produces a lot of pages. A set of these are handled by a particular part of my application, and I would like all these to have a class added to either html or body. I'd settle for #block-system-main. The existing answers point to a template preprocess hooks e.g. hook_preprocess_html() but this feels wrong. I'd have to create th...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.