Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TailwindCSS with Material UI?

I am using Next.js with Material UI. I am having troubles with using Tailwind with MUI. I've been following this guide but it still doesn't work. The file loads but the classes just don't apply. If someone could help, that would be wonderful!

My Tailwind Config

    module.exports = {
  important: "#__next",
  content: ["./pages/**/*.{js,jsx}"],
  theme: {
    extend: {},
  },
  plugins: [],
}

My _app.js

//import '../styles/globals.css'

//function MyApp({ Component, pageProps }) {
//  return <Component {...pageProps} />
//}

//export default MyApp

import '../styles/edit.css';
import * as React from 'react';
import PropTypes from 'prop-types';
import Head from 'next/head';
import { ThemeProvider, StyledEngineProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import { CacheProvider } from '@emotion/react';
import theme from '../config/themeConfig';
import createEmotionCache from '../functions/createEmotionCache';
import Layout from "../components/Layout";
//import '../styles/tailwind.css';


// Client-side cache, shared for the whole session of the user in the browser.
const clientSideEmotionCache = createEmotionCache();

export default function MyApp(props) {
  const { Component, emotionCache = clientSideEmotionCache, pageProps } = props;

  return (
      <Layout>
          <CacheProvider value={emotionCache}>
        <Head>
          <meta name="viewport" content="initial-scale=1, width=device-width" />
        </Head>
        <ThemeProvider theme={theme}>
            <StyledEngineProvider injectFirst>
                {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
                <CssBaseline />
                <Component {...pageProps} />
            </StyledEngineProvider>
        </ThemeProvider>
      </CacheProvider>
      </Layout>
  );
}

MyApp.propTypes = {
  Component: PropTypes.elementType.isRequired,
  emotionCache: PropTypes.object,
  pageProps: PropTypes.object.isRequired,
};

Thanks!


1 Answers

You didn't follow the guide ( https://mui.com/material-ui/guides/interoperability/#tailwind-css ) correctly.

In your tailwind.config.js file you need to set the corePlugins parameter to disable the preflight CSS. The preflight CSS is overriding some of the styles of MUI.

like image 144
Kernel James Avatar answered Oct 19 '25 02:10

Kernel James