Skip to content

6 min read

EngineeringMUITailwind

MUI keeps overriding your Tailwind classes? Put it in its own CSS layer

Why a Tailwind class on an MUI component does nothing, and the setup that fixes it for good: cascade layers, enableCssLayer, one small workaround for MUI's layer order, and a home for your component styles. From the way we build our admin templates.

By the Colored Strategies team

You put rounded-3xl on an MUI Card and nothing changes. You open DevTools and there is your Tailwind class, crossed out, under a generated rule from MUI. You add ! and it works. Then you add ! to the next one, and the one after that.

We have been there. Our admin templates, Gogo and Acorn, use MUI for components and Tailwind for styling, including the styling of the MUI components themselves, and we went through important, ! and theme overrides before landing on a setup that simply works. This is that setup, in the order you would apply it.

Why MUI wins

MUI styles its components with Emotion, which writes <style> tags into the page at runtime. When an MUI rule and a Tailwind class set the same property, the browser picks the more specific selector, and on a tie, the one that comes later. MUI tends to win both: its selectors often combine two classes, such as .MuiButton-root.MuiButton-sizeLarge, and its tags can land after your stylesheet.

That is why !, Tailwind's important option and sx all appear to work. Each one outshouts MUI on that one element. None of them fixes the order, so the fight starts again on the next component.

Step 1: give MUI a layer of its own

CSS cascade layers decide the winner before specificity is even looked at: a rule in a later layer beats any rule in an earlier one. Tailwind 4 already puts its own output in layers, so the only thing missing is a layer for MUI and an agreed order.

Declare the order at the top of your global stylesheet, before Tailwind:

global.cssCSS
@layer theme, base, mui, components, utilities;
@import "tailwindcss";

Then tell MUI to put everything it generates into mui:

TSX
// Vite or any client-rendered React app
<StyledEngineProvider enableCssLayer>
  <App />
</StyledEngineProvider>

// Next.js App Router, in the root layout
<AppRouterCacheProvider options={{ enableCssLayer: true }}>
  {children}
</AppRouterCacheProvider>

Now Tailwind's reset sits in base, under MUI, and every utility sits in utilities, above it. A class on an MUI component wins because of where it lives, however specific MUI's selector is.

Step 2: make sure your order is the first one the browser reads

This is the part that cost us the most time. A layer's position is fixed the first time the browser sees its name. If MUI's @layer mui { ... } is read before your @layer theme, base, mui, components, utilities; line, mui becomes the lowest layer and the order you declared no longer holds.

That happens when Emotion's style tags are placed at the top of <head>, above your stylesheet, which is what the prepend option of MUI's cache provider does. Two ways out:

  • Do not prepend. Leave prepend off, so MUI's tags come after your stylesheet.
  • Pin the order in front of everything. We first tried a second stylesheet that re-declared the order. It cannot work: a stylesheet is never guaranteed to load ahead of a tag that MUI inserts at runtime. What does work is inserting the order as the very first <style> tag, from the client:
theme/mui-layer-override.tsxTSX
import { useEffect } from "react";

export default function MuiLayerOverride() {
  useEffect(() => {
    const style = document.createElement("style");
    style.id = "mui-layer-override";
    style.textContent = "@layer theme, base, mui, components, utilities;";
    document.head.insertBefore(style, document.head.firstChild);
    return () => style.remove();
  }, []);
  return null;
}

Render it once inside your theme provider. It is a workaround, and we say so in the comment above the layer line in our stylesheet, but it has held through every MUI and Tailwind upgrade since.

To check either way: open DevTools, select an MUI button, and look at the Styles panel. MUI's rules should be grouped under @layer mui, and your utilities under @layer utilities, listed above them.

Step 3: give your component styles a home

A template, like most products, is mostly restyled MUI: your buttons, your cards, your inputs. Writing that as sx on every call site, or as long class strings in the theme's defaultProps, is where most ! come from.

We keep it in plain CSS instead: one file per MUI component, grouped the way the MUI documentation groups them, written against MUI's stable class names with Tailwind's @apply:

Text
style/
  global.css          the layer order, Tailwind, and the imports below
  surfaces/           card.css, paper.css, accordion.css
  inputs/             button.css, input.css, select.css, switch.css
  navigation/         tabs.css, menu.css, drawer.css, pagination.css
  data-display/       chip.css, avatar.css, list.css, tooltip.css
  feedback/           alert.css, dialog.css, snackbar.css
  mui-x/              data-grid.css, chart.css, date-time-picker.css
surfaces/card.cssCSS
@layer utilities {
  .MuiCard-root {
    @apply bg-background-paper rounded-3xl p-3;
  }
}

Put these files in utilities, not components. We started with components, which sounds right, and kept needing ! to override our own defaults at the call site. In utilities the default and the class you add later sit in the same layer, and the later one wins as you would expect.

The same files are a good place for what MUI does not ship. A custom button size is one line of TypeScript and one CSS rule:

theme/mui-extend.tsTypeScript
declare module "@mui/material/Button" {
  interface ButtonPropsSizeOverrides {
    tiny: true;
  }
}
inputs/button.cssCSS
@layer utilities {
  .MuiButton-sizeTiny {
    @apply min-w-7 rounded-xs px-3.5 py-1.5 text-sm;
  }
}

Step 4: classes at the call site

With the order fixed, MUI keeps behaviour and props, and classes do the styling:

TSX
<Button variant="outlined" size="tiny" className="w-full">
  Save
</Button>

Run class strings through tailwind-merge, so a default and an override that conflict resolve to the last one instead of both landing in the DOM:

TypeScript
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

To give a sense of scale: across the Gogo source there are about 14,000 className attributes, two sx props and a handful of !, each of them a known exception where an MUI state selector (focus, loading) is more specific than one class.

One more thing: one source for colours

Once the cascade is sorted, the next drift is colour: MUI's palette in JavaScript, Tailwind's in its config. Keep each colour once, as HSL channels in a CSS variable, and point both at it:

CSS
:root {
  --primary: 203 51% 48%;
}
tailwind.config.tsTypeScript
colors: { primary: "hsl(var(--primary))" }

// MUI theme
palette: { primary: { main: "hsl(var(--primary))" } }

Dark mode and colour themes then become a class on <html> that redefines the variables, and both libraries follow without re-creating the MUI theme.

The short version

  1. Declare @layer theme, base, mui, components, utilities; before Tailwind.
  2. Turn on enableCssLayer.
  3. Make sure that order is the first @layer statement on the page.
  4. Move component styling into one CSS file per MUI component, in @layer utilities.
  5. Delete ! as you go. Each one you can remove is a place where the cascade now agrees with you.

If you would rather start from a codebase where all of this is already done, it is what Gogo and Acorn are built on.

Hit something like this in your own product?

Senior designers and engineers, around twenty years each. We take on admin systems, distributed applications and platforms that have to stay up under load.