CSS Theme Variables
Material UI supports CSS variables (custom properties) for dynamic theming and improved runtime performance.Enabling CSS Variables
Enable CSS variables in your theme:Generated CSS Variables
With CSS variables enabled, Material UI generates variables like:Custom CSS Variable Prefix
Change the defaultmui prefix:
Color Scheme Selector
Control how color schemes are applied:Media Query (System Preference)
Class Selector
Data Attribute Selector
Root Selector
Customize where CSS variables are applied:Accessing CSS Variables
Use thetheme.vars object:
CSS Variable Fallbacks
CSS variables include fallback values:Skip Generating Variables
Exclude specific paths from CSS variable generation:Using CSS Variables in Components
sx Prop
styled API
Color Channels
CSS variables include color channels for alpha manipulation:Dynamic Theming
Change theme at runtime:Multiple Color Schemes
Define custom color schemes:Server-Side Rendering
CSS variables work seamlessly with SSR:Disable CSS Color Scheme
Prevent automaticcolor-scheme CSS property:
Native Color Support
Use native CSS color functions:Performance Benefits
CSS variables offer:- Faster theme switching: No component re-renders
- Smaller bundle size: Less runtime theme logic
- Better SSR: No hydration mismatches
- Native browser support: Leverage CSS engine
Comparison: CSS Variables vs Runtime Theme
Without CSS Variables:Migrating to CSS Variables
Update theme access:Browser Support
CSS variables are supported in all modern browsers:- Chrome 49+
- Firefox 31+
- Safari 9.1+
- Edge 15+
Source Reference
The CSS variables implementation can be found at:packages/mui-material/src/styles/createThemeWithVars.js:127packages/mui-material/src/styles/createTheme.ts:30- CSS variable generation:
packages/mui-system/src/cssVars/prepareCssVars.ts createGetCssVar:packages/mui-material/src/styles/createThemeWithVars.js:86