Skip to main content

How to add a class to a Drupal 7 region

Today I needed to add a class to a Drupal 7 region and found the lovely template_preprocess_region() function to do the dirty work for me. With this function, you can easily add any class to your regions with the code below.

In a nutshell, you find the region you want to add the class to and then append your class onto the end of the classes_array variable.

  1. function MYTHEME_preprocess_region(&$variables) {
  2.     // Run a switch statement to find the correct region we want to add a new class to.
  3.     switch($variables['region']) {
  4.       case 'left_sidebar':
  5.         // Once we've discovered the correct region, add our new class to the classes_array.
  6.         $variables['classes_array'][] = 'blue';
  7.       break;
  8.     }
  9. }