Drupalwoo ☉

How to add custom CSS variables to a Drupal theme

Drupal themes become harder to maintain when every colour, spacing value and focus state is hard-coded across templates, component styles and contributed modules. CSS custom properties give those values stable names, so a brand or spacing decision can be changed once and reused throughout the interface. This is the practical route for creating a reusable token layer without editing Drupal core or duplicating values in many selectors.

Custom properties are native to CSS and work through inheritance. A declaration such as --woo-colour-accent: #005ea8 can be consumed with var(--woo-colour-accent), while child elements inherit the value unless they declare another. That behaviour suits Drupal, where a node, form or view may appear in several regions. It also creates a shared vocabulary for design tokens that developers and site builders can maintain together.

The implementation is small, but the surrounding choices matter: where variables are declared, how Drupal markup is used for scoping, and whether a module expects a particular class. The following method covers a custom theme from stylesheet setup through component usage, cascade behaviour, testing and packaging. It suits Drupal 10 and Drupal 11 projects and also fits agencies maintaining a mixture of legacy and current Drupal estates.

Australian teams should also account for local procurement and accessibility requirements. Government and council briefs in Canberra, Sydney, Melbourne and Brisbane commonly require WCAG 2.2 AA, and agency projects often serve both public-sector and e-commerce audiences. Testing on the devices and mobile connections Australians use is as important as matching a design file. The examples use Australian spelling, including colour and behaviour, and remain portable across the local Drupal services market.

Choose where the variables should live

Start by deciding whether a value is a global design token or a one-off component rule. Brand colours, focus colours, spacing and readable line lengths suit a global layer; an exceptional border used by one component belongs beside that component. Clear scope prevents a theme from accumulating unused variables and makes later maintenance easier.

Decision Theme-level custom properties Utility classes
Best fit Shared brand, spacing, typography and state values One-off layout or presentation adjustments
Drupal use A reusable theme layer inherited by regions and components Classes added through Twig, views or paragraphs
Main advantage Central control with minimal markup Precise changes without redefining a token
Maintenance Requires consistent naming and documentation Repeated combinations can clutter templates

Theme-level properties are usually the stronger foundation because components can consume them without extra markup. Utility classes remain useful for isolated presentation decisions, but applying one to every node can produce repetitive Twig templates. Drupal views and paragraphs can add classes efficiently, yet a token-based value is often simpler to restyle from the theme.

Give variables a short, theme-specific prefix such as --woo-, and use names that describe purpose rather than appearance. --woo-colour-accent can change later; a name such as --woo-blue encourages developers to preserve the original meaning and makes overrides easier to reason about.

Build variables into the theme cascade

Declare shared values on the root element so every part of the page can inherit them. A small foundation might look like this:

:root {
  --woo-colour-ink: #17212b;
  --woo-colour-surface: #ffffff;
  --woo-colour-accent: #005ea8;
  --woo-colour-accent-strong: #004b80;
  --woo-space: 1rem;
  --woo-measure: 72ch;
}

Place this CSS in the custom theme rather than in a page-specific file. If the theme inherits from a base theme, load the custom stylesheet after the base assets so its declarations can override inherited defaults. Inspect the rendered source after CSS aggregation, because cached bundles can otherwise hide the actual cascade order.

Variables may then be inherited by the body, regions and components. Use a fallback when a token is optional: var(--woo-space, 1rem). A fallback covers an undefined property, but it cannot repair an invalid value, so validation and naming discipline still matter.

Use Drupal markup for reliable scope

Drupal supplies useful classes and attributes, but their exact output depends on the active theme, core version and preprocess functions. Inspect the rendered markup before writing selectors. Core commonly exposes data-drupal-selector attributes, while themes add body classes for routes, content types and view modes.

For a custom scope, add a stable attribute in the relevant Twig template. For example, merging data-woo-region="content" into the template attributes produces a clear hook without depending on generated class names. The attribute can target one region, a node bundle or a layout variant without altering field markup.

Then define a token on that scope: [data-woo-region="content"] { --woo-measure: 72ch; }. Its descendants inherit the value, while the rest of the page keeps the global default. This approach is more resilient than styling every child and gives a custom theme control without changing contributed module templates.

Apply variables to reusable components

Apply tokens where visual rules are consumed, rather than replacing values inside component styles. A card might use the surface and ink colours, a brand-coloured border and a shared spacing unit. Keeping the property names in the component makes its intent obvious to the next developer who opens the stylesheet.

.my-theme .node--view-mode-card {
  background: var(--woo-colour-surface);
  color: var(--woo-colour-ink);
  padding: var(--woo-space);
  border-inline-start: 4px solid var(--woo-colour-accent);
}

.my-theme .button {
  background: var(--woo-colour-accent);
  color: #fff;
}

.my-theme .button:hover {
  background: var(--woo-colour-accent-strong);
}

View-mode and bundle classes vary between sites, so check the actual classes Drupal assigns to a node before relying on .node--view-mode-card. A custom view display or theme hook may be a better hook when the same component must behave consistently. Pseudo-classes such as :hover and :focus-visible should also consume tokens.

Broader Drupal implementation examples appear in the Nix Mash tutorials, but the token pattern remains the same for a bespoke agency theme. The important part is that the markup, stylesheet and component API all refer to the same named values.

Override values without specificity fights

Custom property declarations still use the normal CSS cascade. A declaration on body overrides one inherited from :root for that subtree, and a nearer region can override the body value. This makes targeted theme variants straightforward without resorting to broad !important rules.

body {
  --woo-colour-accent: #006a8e;
}

.my-theme .site-header {
  --woo-colour-accent: #6b3f00;
}

A declaration on the component itself has greater relevance than an inherited body value. If a base theme sets --button-colour directly on .button, changing a body-level token will not affect it. Override the component token at the same element or deliberately align both naming schemes.

For settings controlled by editors, expose a small set of data attributes or classes and map them to predefined tokens. This avoids injecting unchecked values into inline styles and gives the site team a bounded set of choices. Theme configuration should validate those choices before they reach the rendered page.

Test accessibility and responsive behaviour

Test the variable system at mobile, tablet and desktop widths, including at least one narrow viewport. A spacing token can work in a wide Melbourne layout but create cramped controls on a small Brisbane phone. Verify wrapping, zoom, touch targets and horizontal overflow with real content rather than placeholder text.

Colour contrast needs checking in every token combination. A global ink colour may be accessible on white but fail against a tinted region, and hover colours can erase the contrast of primary buttons. Australian public-sector accessibility requirements make this a release condition, not a final visual tweak. Keyboard focus must remain visible against both the component and its surroundings.

Also test forced-colours mode, dark-mode preferences and print styles. A token set that relies on a background image to communicate state can fail in forced-colours mode. Use a visible outline for focus, avoid motion that ignores prefers-reduced-motion, and ensure links remain distinguishable without colour alone.

Package, document and verify changes

Package the stylesheet through the theme's library definition, then clear Drupal's caches so the asset list and aggregation are rebuilt. A minimal libraries.yml entry looks like this:

my_theme:
  version: VERSION
  css:
    theme:
      css/custom-properties.css: {}

Do not edit generated aggregate files, and do not place the custom file in Drupal's core directory. Keep it under the custom theme, commit the source CSS and library changes, and record the token names in the theme documentation. For background on the site's practical Drupal perspective, see the Drupalwoo author profile.

Two short release checks keep the change accountable. The design review confirms that:

The browser and content pass confirms that: