← All posts

· 6 min read

Accessible Dark Mode with MUI CSS Variable Tokens

Why CSS variables change the game for dark mode

The traditional approach to MUI theming creates two separate theme objects — one light, one dark — and swaps them by re-rendering the entire tree. This works, but it has a significant downside on SSR sites: the server does not know the user's preference, so it must guess. If it guesses wrong, the client corrects itself after hydration, causing a visible flash of the wrong theme.

MUI's CSS variables mode solves this. Instead of shipping JavaScript theme values, it emits all design tokens as CSS custom properties at build time. Switching themes is then a matter of toggling a data attribute on the <html> element, which the browser can do synchronously — before paint — using a small inline script.

Setting up the CSS variables theme

The key step is calling extendTheme instead of createTheme, and wrapping the app with CssVarsProvider:

import { extendTheme, CssVarsProvider } from '@mui/material/styles';

const theme = extendTheme({
  colorSchemes: {
    light: { palette: { primary: { main: '#1976d2' } } },
    dark:  { palette: { primary: { main: '#90caf9' } } },
  },
});

With this setup, MUI emits every palette value as a CSS variable like --mui-palette-primary-main. Components reference these variables directly; no JavaScript is needed to compute a color.

Reading tokens in custom styles

When writing custom component styles inside sx props or styled(), always reference tokens via theme.vars.*, not theme.*:

// CORRECT: resolves to the CSS variable at runtime
sx={{ color: theme.vars.palette.text.primary }}

// WRONG in CSS vars mode: bakes in the light-mode hex value
sx={{ color: theme.palette.text.primary }}

The second form bakes in the value at render time. If the theme switches after SSR, the element keeps the baked-in light-mode color while everything else turns dark.

Targeting dark mode in plain CSS

For styles that live outside MUI's system — global overrides, third-party widget wrappers — the correct selector is the attribute MUI actually sets:

[data-mui-color-scheme="dark"] .my-widget {
  background: var(--mui-palette-background-paper);
}

Contrast and accessibility

Accessibility in dark mode is less forgiving than in light mode. A few rules that helped here:

  • Never assume contrast ratios transfer between modes. A primary colour with 4.5:1 contrast on white may fall below 3:1 on a dark background. Run both light and dark palettes through a contrast checker separately.
  • Check focus rings. The default MUI focus ring is often a bright outline that disappears on a dark surface if it is the same hue as the focused element.
  • Test with forced-colors media query. Some users override system colours entirely. Meaningful icons and interactive affordances need text labels, not colour alone.

Eliminating the FOUC

A small inline <script> in the <head> reads the stored preference from localStorage and sets the data-color-scheme attribute before the first paint. Because the CSS variables are already in the stylesheet, the browser applies them immediately — no layout recalculation, no flash.