Drupal is a registered trademark of Dries Buytaert
Release: Leaflet 10.4.11 Minor update available for module leaflet (10.4.11). Release: Session Inspector 1.0.8 Minor update available for module session_inspector (1.0.8). Release: Migrate QA 2.0.4 Minor update available for module migrate_qa (2.0.4). Release: CKEditor Description List 3.0.0 Major update available for module ckeditor_descriptionlist (3.0.0). Release: FlowDrop 2.4.0 Minor update available for module flowdrop (2.4.0). Release: JWT Token Refresh 1.0.4 Minor update available for module jwt_token_refresh (1.0.4). Release: ConReg 1.0.0-beta1 First beta version released for module conreg (1.0.0-beta1). Release: AI Image Studio 1.0.0-beta8 New beta version released for module ai_image_studio (1.0.0-beta8). Usage Milestone: Role Theme Switcher Module role_theme_switcher crossed 1,000 active installs. Module Revived: Decoupled Router 2.0.7 Module decoupled_router updated after 11 months of inactivity (2.0.7).

Roles Classes

329 sites No security coverage
View on drupal.org

This module adds CSS classes to the HTML body tag based on the roles assigned to the currently logged-in user. It allows for easy styling and custom behavior targeting of users by role, with options to configure class prefixes and exclude specific roles. The module also adds classes for authenticated and anonymous users.

The Body Roles Classes module adds CSS classes to the HTML element based on the roles of the currently logged-in user.
This helps developers and themers easily target users by role when applying styles or custom behaviors.

Features

  • Adds role-based CSS classes to the element
  • Configurable class prefix (e.g. role-editor, role-administrator)
  • Option to exclude specific roles from being added as classes
  • Adds user-authenticated and user-anonymous classes
  • Cache-safe implementation using Drupal cache context (user.roles)

Example

If a user has the following roles:

  • authenticated
  • editor

The output will be:

  <body class="role-authenticated role-editor">

If the "administrator" role is excluded, it will not be added as a class.

Configuration

After enabling the module, go to:
/admin/config/user-interface/body-roles-classes

You can:

  • Enable or disable the module
  • Set a class prefix
  • Select roles to exclude from output

Installation

  1. Download and place the module in your DRUPAL_ROOT/modules/contrib directory.
  2. Go to Admin > Extend and enable the module.
  3. Configure the module at Admin > Configuration > User Interface > Body Roles Classes.

Use Cases

  • Role-based theming
  • Custom styling for administrators, editors, or other roles
  • Frontend personalization based on user roles

Notes

  • Use the exclude roles option to prevent exposing sensitive roles (e.g. administrator) in the frontend.
  • The module uses proper cache contexts to ensure correct output for each user.

Activity

Tracked releases
4
Tracked since
May 2026
Latest release
2 months ago
Releases (12 mo)
4 ▲ from 0
Maintenance
Active

Release Timeline

Releases

Version Type Release date
2.0.1 Stable Jun 15, 2026
8.x-1.5 Stable May 25, 2026
2.0.x-dev Dev May 9, 2026
2.0.0 Stable May 9, 2026