node_id_class
The Node ID Class module for Drupal enables site builders to assign custom CSS IDs and classes to the <body> element and individual node wrappers. These can include dynamic tokens such as {node_id}, {node_title}, {bundle}, and {node_author_uid}, allowing flexible theming, unique layouts, or custom frontend behavior.
Requirements
- Drupal Version: This module requires Drupal 10 or higher.
- Additional Modules: No additional contributed modules are required.
Installation
- Download the Node ID Class module.
- Install using Composer:
composer require drupal/node_id_class - Or manually place the module in your
modules/customdirectory. See:
Drupal module installation guide
- Enable the module via the Extend page in the Drupal admin interface or using Drush:
drush en node_id_class
Configuration
After enabling the module, a new Node ID Class fieldset will appear on each content type’s edit page (e.g., admin/structure/types/manage/article). Within this fieldset, you can configure the following:
- CSS Body ID: Applied to the
<body>element on full node pages. Supports tokens:{node_id},{bundle},{node_title},{node_author_uid}. Example:body-id-{node_id}-{bundle}-{node_title} - CSS Body Class(es): Applied to the
<body>element. Supports multiple classes separated by spaces. Example:body-class-{node_id}-{bundle} - CSS Node Class(es): Applied to the node wrapper in all view modes and listings (e.g., Views). Supports multiple classes separated by spaces. Example:
node-class-{node_id}-{bundle}-{node_title}
Dynamic Tokens
{node_id}– Node ID{node_title}– Node title (converted to a URL-friendly slug){bundle}– Content type machine name{node_author_uid}– Node author’s user ID
Tokens are sanitized and escaped automatically to prevent invalid markup or XSS vulnerabilities.
Similar Projects
Troubleshooting & FAQ
- Custom classes not appearing? Ensure the node is displayed in a full-page view mode and your theme prints
$attributesand$content_attributes. - Body ID or classes not showing? Verify that the settings are saved for the correct content type.
- Tokens are not replaced? Tokens only work when viewing a node page.
- Why is HTML not allowed in the fields? HTML is stripped for security, preventing XSS attacks and invalid CSS.
Support
Please post bug reports, feature requests and support requests to the Node ID Class module issue queue. If you met any problems with this module - feel free to create an issue.