Implementing Dynamic Colour Palettes in Drupal Themes Using CSS Variables
In a blog post shared by Morpht, Naveen Valecha introduces a modern approach to managing theme colours in Drupal using CSS Variables. The post explains how the convivial_govcms_bootstrap
theme moves away from static SASS-based theming—which requires recompilation for each change—and instead enables real-time colour adjustments. This shift benefits branding consistency, accessibility, and ease of use. Site builders can define colour palettes, preview changes instantly, and ensure WCAG-compliant contrast levels without writing code.
The implementation uses Drupal hooks, JavaScript, and CSS to create a no-code interface for selecting and applying theme colours. Key steps include integrating colour settings into the theme configuration form using hook_form_system_theme_settings_alter()
injecting CSS variables via hook_page_attachments_alter()
, and dynamically processing colour relationships with JavaScript. Support for light/dark themes and UI variations ensures flexibility across elements like buttons, backgrounds, and text.