Issue with Tailwind and MUI integration causing unanticipated error with white buttons in NextJS Project

I'm in the process of developing a project using NextJS, TailwindCSS, and MUI React UI library.

While integrating an MUI Button into my project, I noticed that the button's color remains white despite styling changes.

https://i.stack.imgur.com/JrTmC.png

The button behaves normally when hovered over or clicked, with the ripple effect intact. However, it reverts to white when not actively engaged.

If I remove the Tailwind directives from the global CSS file imported in the _app.{js,jsx.ts,tsx} file, the button functions as expected. This, however, means losing access to TailwindCSS altogether.

Is there a way to address this issue while retaining the directives, or is there an alternative method to include TailwindCSS?

UPDATE (15/8/2022)

The MUI Team has introduced support for Tailwind CSS. For detailed instructions, please refer to the following link: https://mui.com/material-ui/guides/interoperability/#tailwind-css

Answer №1

The issue at hand is that tailwinds preflight applies specific styles when included in your stylesheet:

@tailwind base;

These styles target button elements and similar types, which can conflict with material-ui's background-color definitions.

To resolve this conflict, you may need to remove "@tailwind base;" from your styles and create a custom reset-stylesheet instead. However, this approach comes with its own set of challenges: https://tailwindcss.com/docs/preflight

Answer №2

the solution that worked for me was to update the tailwind.config.js file located in the main directory. I added the necessary configuration under corePlugins. Here's an example of how it can be implemented:

/** @type {import('tailwindcss').Config} */

module.exports = {
  // include this section to address the issue
  corePlugins: {
    preflight: false,
  },
  content: [
    "./app/**/*.{js,ts,jsx,tsx}",
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",

    // If using a `src` folder:
    "./src/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

Answer №3

There is an issue caused by preflight action in tailwind directives. The @base directive is conflicting with the mui styles! For a solution, refer to the following guidelines. According to the official MUI documentation, you need to disable preflight in the tailwind.config.js file as shown below:

 module.exports = {
    corePlugins: {
    preflight: false,
  },
 }

I strongly suggest taking a look at the complete Style library interoperability documentation.

Answer №4

My strong recommendation is to avoid mixing multiple CSS frameworks or libraries. Stick with one to prevent style conflicts that may arise. Different frameworks may use the same classes but have different underlying code, leading to conflicts. If you become accustomed to using Tailwind, you may lose interest in the components provided by Material UI and even Tailwind itself. It's important to remember that committing to a specific library or framework means aligning your design with it. Using two distinct ones could result in varying styles throughout your website.

If you're interested in achieving the MUI ripple effect, consider this CSS snippet:

span.ripple {
 position: absolute;
 border-radius: 50%;
 transform: scale(0);
 animation: ripple 600ms linear;
 background-color: rgba(255, 255, 255, 0.7);
}

@keyframes ripple {
 to {
  transform: scale(4);
  opacity: 0;
 }
}

Check out this CodePen for more insights on incorporating the ripple effect: https://codepen.io/vituja1/pen/oNWzNwq. The CodePen also includes JavaScript code.

This resource might also pique your interest, although I haven't personally tested it yet: https://www.npmjs.com/package/tailwindcss-ripple

Answer №5

The current workaround at the moment involves reverting MATERIAL-UI to version V ^4.12.3.

Answer №6

After updating Tailwind to version 3.1.6, I encountered a problem with my Material buttons. Previously, everything was working fine with Tailwind 2.2.x, but after the update, all buttons in my app became transparent due to Tailwind's base button classes conflicting with Material's styling. To fix this issue, I added a CSS wildcard rule at the attribute level in my global stylesheet specifically targeting primary Material buttons:

 button[class*='MuiButton-containedPrimary'] {
 background-color: #0081cb!important;
 }

This solution worked for me by setting the background color to the primary color (#0081cb) of Material buttons. You can customize other button variants like success and error using similar approaches as well.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Hover animation using CSS is a great way to add interactivity

I'm trying to create an animation in a moving box that flips upside down when the mouse is raised. I've attempted to use keyframes and hover to achieve this effect, but it's not working as expected. Any suggestions on how to make it work? ...

nextJS does not recognize the term 'Window'

I'm encountering the error "window is not defined" in my NextJS project. The variable isMobile determines whether the window size is less than 767.98 to handle the hamburger menu functionality. This code worked in ReactJS but seems to be causing issue ...

Challenges with displaying css/bootstrap classes within angular2

Experiencing difficulties with CSS/Bootstrap integration when displayed in an angular2 component. When attempting to display the CSS and HTML content in the Index.html file (with proper CSS and JS references), everything functions correctly. However, once ...

Arrangement containing 4 separate div elements - 2 divs are fixed in size, 1 div adjusts dynamically (without scroll), and the remaining div fills the

I am trying to implement a specific layout using HTML and CSS on a webpage. So far, I have successfully created the black, red, and blue areas, but I am facing challenges with the scrollable green content. It currently has a static height, but I need it to ...

Is it permissible to have multiple class tags in HTML?

Is the HTML code below correctly formatted? <div class="navbar" class="menu">foo</div> Can two classes be applied to the same element? ...

Tips for capturing everything in NextJs with getStaticPaths

My current challenge involves utilizing getStaticProps and getStaticPaths to dynamically generate pages at build time. This is my first experience working with CatchAll routes, so I embarked on a search for solutions. Unfortunately, none of the results al ...

Exploring the use of fonts in Next.UI in conjunction with react.js

I am currently struggling to find documentation on how to import and use different fonts in my React application. I am using the Next.UI styled component library (Next.UI) to create a theme for my app. While I have successfully created a theme with colors ...

Take advantage of the usePagination custom hook in conjunction with Material-UI by incorporating the next and prev functions as props within

Utilizing a custom hook for pagination with material-ui has been successful, displaying the first page properly. However, there are issues when trying to call next(), prev(), and jump(). Passing next={next} as props was attempted, but without success. The ...

Input element's click event is failing to trigger when using ref

Currently, I am working with React 17.0.2 and Material UI 4.11.4. My goal is to customize the appearance of a select element similar to the Chip component in Material UI. To achieve this, I have decided to utilize the Autocomplete component which renders ...

What are some ways to ensure keyboard accessibility for a CSS drop-down menu?

I have come across some solutions, but I am struggling to incorporate them into my code as some require elements that are not compatible with my project. Let's get to the question: I need help making an existing CSS drop-down menu accessible for key ...

What is the most efficient way to access a cell within an HTML table using jQuery or the Document Object Model (

I have an unchangeable HTML table styled with CSS. My goal is to extract the value from the first column for filtering purposes. I've been struggling to locate a suitable jQuery or DOM function to accomplish this task. Unable to find a way to access ...

Is there a way in React to specifically listen for changes in just one variable among several, without interfering with the other variables?

As I delve into using functional React to create my hobby website, I consistently encounter the following scenario: const [randomData, setRandomData] = useState(true); const [activePopup, setActivePopup] = useState(1); useEffect(() => { ... con ...

Troublesome button appearance

I set up two sections to gather user information using buttons. My goal is for the button styles to change when clicked, making it clear to users which option they have selected, even if they switch between sections. However, I encountered an issue where ...

Troubleshooting an Issue with CSS Sliding Doors

I'm having trouble getting the right image to show on my website. The left side of the image is fine, but I can't figure out how to make the right one appear. As a HTML/CSS beginner, I'm still learning the basics. For more information, vis ...

The deployment on Firebase was completed with more than 10,000 modifications

After successfully integrating my next.js project with Firebase, I encountered an issue when trying to use Firebase hosting. Surprisingly, it made over 10k changes to the project. This was unexpected as the project only consisted of around 28 files, esse ...

What is the process for including response headers to NextJS public assets?

How can I set the max-age response header for images and fonts in my NextJS project? I came across this documentation on NextJS, but as I am using next-compose-plugins, it's not very clear to me how to implement this in my current code. https://next ...

Tips for creating a clickable ::before image

I'm trying to create a hover effect where an image appears and is clickable when hovering over an element, similar to what is seen on many websites. For example, I want to display a bin icon when hovering over an element by using the ::before pseudo-e ...

Experiencing unexpected "Uncaught runtime errors" pop-ups following updates to the most recent versions of CRA and Node.js 18

Our DevOps team has notified us that they will be upgrading all servers to Node 18. However, our application was functioning correctly only with Node 16. To address this, I updated CRA to the latest version and initially everything seemed fine. But soon af ...

Switched from btao to Buffer, however encountering a TypeError while trying to push to Vercel

I am currently working on an application in Next.js where I need to encode my image to base64. Initially, I used btao and it worked well until I tried deploying to Vercel, which resulted in an error stating that btao was undefined. After researching a solu ...

Using a third-party React component within a web-based forms application

Is it possible to include a Material UI component (https://material-ui.com/getting-started/usage/) in a web forms application? Below is the code I currently have, but it is not displaying the Button component. I am working with an ASPX file and using UMD ...