Skip to content

Sharing common CSS between themes

This approach is ideal for managing multiple child themes with slight variations for different websites. It promotes code reusability and simplifies maintenance.

Using CSS Variables

CSS Variables (custom properties) allow centralizing styling and enabling customization across themes. You can define a base theme and customize it using variables, including features like dark mode.

Update tailwind.config.js

First, replace desired properties in your tailwind.config.js with CSS variables. Let's use primary colors as an example:

module.exports = hyvaModules.mergeTailwindConfig({
    theme: {
        extend: {
            ...
            colors: {
                primary: {
                    lighter: 'hsl(var(--color-primary-lighter) / <alpha-value>)',
                    "DEFAULT": 'hsl(var(--color-primary) / <alpha-value>)',
                    darker: 'hsl(var(--color-primary-darker) / <alpha-value>)'
                },
            },
            ...
        }
    }
});

Important

Include the color syntax (e.g., hsl) to maintain Tailwind's opacity modifier functionality.

Define CSS Variables

After configuring tailwind.config.js, define the CSS variables with the chosen syntax. Here are a few options:

  • Global CSS: Add them to the root selector (body:root or :root) in a separate CSS file:
    :root {
      --color-primary: 160 100% 54%;
    
      /* Example rgb syntax */
      --color-primary-lighter: 20 255 175;
    }
    
  • Magento Theme Configuration: Add them to the Design Configuration → <STORE> → HTML Head → Scripts and Style Sheets section in your Magento admin.
  • PHTML files: Define variables directly within PHTML files using PHP for dynamic values.
    <style>
    :root {
      --color-primary: 160 100% 54%;
    
      /* Dynamic values from Magento (if applicable) */
      --color-primary-darker: <?= $brandColor ?>;
      }
    </style>
    
  • Third-party solutions: Some libraries allow setting CSS variables directly in your Tailwind CSS configuration.

Important Notes:

  • Hex color values are not compatible with Tailwind's opacity modifier when using CSS variables. Use HSL or RGB syntax for opacity support.
  • Disabling opacity modifiers allows using any color syntax (hex, hsl, rgb) but sacrifices Tailwind's built-in opacity functionality.

Dynamic configuration

Since the tailwind.config.js file allows for any JavaScript, it is possible to build the configuration dynamically.
An example of such can be seen in the Advanced Purge Configuration documentation.