Body Roles Classes

This project is not covered by Drupal’s security advisory policy.

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.
Supporting organizations: 
Development and bug fixing

Project information

Releases