Using Content type: News, I have created pages NewsItem1, NewsItem2

I would like to have a background and color blue to NewsItem1 page and set the pink background to NewsItem2 page through Drupal admin. Is that possible?

I am using Drupal 8.5 version. I think themekey module allowed to do this but it is not available for D8. I couldn't find any other easy way to do this. Any suggestions? Thanks in advance.

1 Answer 1


You will probably have to add a "Background Color" field to the content type with some selections that map to CSS class names.

Then, depending on where you want to add the background (e.g. on the body or on just the main content area), to add a preprocessor function in your theme to add the selected class as a variable that can be used by the twig template.

E.g. for just the main content area, you would use something like:

function bartik_preprocess_page(&$variables) { $node = \Drupal::routeMatch()->getParameter('node'); if ($node) { if (!$node->color->isEmpty()) { $variables['bg-class'] = $node->color->getValue(); } } }

Copy the page.html.twig file to your theme's templates directory and add the bg-class to the section you want that color. E.g. class="{{ bg-color }}".


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.