Skip to content

[theme] Material UI CSS Variables not respected for derived component colors when using external stylesheetΒ #47740

@sodehl

Description

@sodehl

Steps to reproduce

Open the provided StackBlitz link and inspect the Alert component's background color: https://stackblitz.com/edit/nze14hel?file=src%2FDemo.tsx

Current behavior

Many MUI components generate derived colors (like Alert backgrounds, Slider tracks, Switch disabled states) using JavaScript color manipulation at theme initialization time, which bakes in the default palette values instead of referencing the CSS variables.

Example with Alert component:

When using a standard Alert with color="info", the background color uses the default MUI blue (lightened by 90%) instead of our --mui-palette-info-* CSS variable defined in our stylesheet.

The generated CSS variable looks like:

--mui-palette-Alert-infoStandardBg: rgb(229, 246, 253); // πŸ‘ˆ Hardcoded color value based on default palette

This means CSS variables are completely ignored.

Expected behavior

Derived component colors should reference the CSS variables so they update dynamically when the base palette variables are overridden in external stylesheets.

Possible Solution

Generate the CSS variable by using the color-mix function and referencing the CSS variables:

--mui-palette-Alert-infoStandardBg: color-mix(in srgb, var(--mui-palette-info-light), #fff 90%);

Context

We have a micro frontend architecture where multiple apps need to share a consistent theme. To maintain this centrally, we've created a single CSS file containing MUI CSS variables (e.g., --mui-palette-info-light, --mui-palette-info-main, etc.) that gets loaded by all micro frontends.

Our createTheme configuration is minimal and only enables CSS variables support:

const theme = createTheme({
  cssVariables: true,
});

The actual color values are defined in our external CSS file:

:root {
  --mui-palette-info-light: #dfc21f;
  --mui-palette-info-main: #d3b613;
  --mui-palette-info-dark: #b89f11;
  /* ... etc */
}

This approach is similar to the pattern described in the MUI documentation for aliasing color variables.

Expected benefit: We can update colors centrally in the CSS file and all micro frontends immediately reflect the changes without rebuilding or redeploying individual applications.

Your environment

Order ID: 115541

Search keywords: Theme, CSS Variables, palette

Metadata

Metadata

Labels

customization: themeHigher level theming customizability.package: material-uiSpecific to Material UI.scope: systemThe system, the design tokens / styling foundations used across components. eg. @mui/system with MUI

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions