Is it possible to configure mui v5 to display class names without the css-xxx prefix?

Working with mui has truly been a delightful experience. Each developer seems to have their own unique approach when it comes to styling and layout in react. The flexibility provided by mui must present quite the challenge for library developers.

In customizing mui, I decided to focus on working with the global theme. This involved creating specific class names to apply to elements like MuiContainer. Initially, I relied on selectors within the overrides, but now mui offers component-specific styleOverrides prop.

Unfortunately, my selectors are not functioning properly due to the following change:

// Class names in v4
.MuiContainer-root

// Class names in v5
.css-1oqqzyl-MuiContainer-root

I'm left wondering if there is a way to ensure uniform rendering of class names by the theme engine. Could this shift be indicative of mui's reliance on emotion?

On a side note, when viewing the rendered output in v4, the class names included my custom classes as well:

.MuiContainer-root.Luci-AppLayout.root

An additional observation in v5 reveals that each element now possesses three sets of class names:

  • mui without prefix
  • mui with prefix (absent in v4)
  • my custom classes remain untouched

Interestingly, only the mui class names with prefix show up in the inspection window, indicating which classes are actually being used to style the element.

Addendum

As per feedback from @Ryan Cogswell, both versions of class names are present in the html. However, during inspection using dev-tools, only the prefixed class names are utilized to style the elements.

To replicate the issue, please refer to the sandbox link below and inspect the code in the dev-tools:

https://codesandbox.io/embed/sad-varahamihira-pv73t5?fontsize=14&hidenavigation=1&theme=dark

Here's the code snippet from the sandbox example:

import Button from "@mui/material/Button";
import "./styles.css";
import { createTheme } from "@mui/material/styles";
import { ThemeProvider } from "@mui/styles";

const theme = createTheme({
  components: {
    // Component name
    MuiButton: {
      styleOverrides: {
        // Slot name
        root: {
          // CSS styling
          color: "red",
          "&.Custom": {
            color: "green"
          }
        }
      }
    }
  }
});

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      <div>
        <h1>Hello CodeSandbox</h1>
        <Button className="Custom">Testing</Button>
      </div>
    </ThemeProvider>
  );
}

Answer №1

We identified two issues with the code in your sandbox.

  1. The theme had a typo where it was using ccomponents instead of components. This seems to be a specific typo in your sandbox and not present in your actual codebase.
  2. You were importing the ThemeProvider from "@mui/styles" instead of "@mui/material/styles". The former is for the legacy JSS styling approach, while the latter integrates with the "@mui/material" styling engine and makes the theme visible to it.

A corrected version of your sandbox code is provided below:

import Button from "@mui/material/Button";
import { createTheme } from "@mui/material/styles";
import { ThemeProvider } from "@mui/material/styles";

const theme = createTheme({
  components: {
    MuiButton: {
      styleOverrides: {
        root: {
          color: "red",
          "&.Custom": {
            color: "green"
          }
        }
      }
    }
  }
});

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      <div>
        <h1>Hello CodeSandbox</h1>
        <Button className="Custom">Testing</Button>
      </div>
    </ThemeProvider>
  );
}

https://codesandbox.io/s/condescending-dew-r1kwc1?fontsize=14&hidenavigation=1&theme=dark

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

Unconventional border effect while hovering over image within navigation container of Bootstrap

Whenever I hover over the image inside the navigation bar, there is a strange white/gray border that appears. It's quite annoying. Does anyone know how to remove this border? Here is the code snippet causing the issue: <ul class ...

Datepicker dilemma: Unclear minimum date

As a newcomer to MUI, I am working on a simple application where I encountered an issue with the Datepicker. import React from "react"; import { Container, Grid, Typography } from "@mui/material"; import { DatePicker } from "@mui/x ...

Issues with peer dependencies arise during the installation of npm packages

Seeking help with resolving the following errors: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: x npm ERR! Found: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1 ...

Tips for creating a stylish ReactJs Header component that stays fixed at the top of the page

Having an issue with my Header component - I want it to remain fixed at the top while scrolling down. Here's the current code: I've attempted using "position = fixed", but it caused my header to resize. Then, I tried setting its width to "width ...

"Utilizing the 8-column layout feature in Twitter Bootstrap

Is it possible to set up 8 equal columns using the latest version of Twitter bootstrap? I know how to create 4 equal columns but I am struggling with figuring out how to achieve 8: <div class="col-sm-12"> <div class="row"> ...

Material-UI Checkbox exhibits an unexpected "jumping" behavior within a grid layout

I am in need of a grid structure for my Material-UI collapsible that looks like this: Here is the desired grid layout Despite trying to adjust the layout by changing the flex-direction, I have not been successful. Check out my attempt on CodeSandbox Wh ...

What is the most efficient way to execute useEffect when only one specific dependency changes among multiple dependencies?

My main objective is to update a state array only when a specific state (loadingStatus) undergoes a change. Yet, if I include solely loadingStatus as a dependency, React throws an error requesting all dependencies [loadingStatus, message, messageArray, set ...

I am looking to adjust the height of my MUI Grid component

Recently exploring React, and I'm looking to set a height limit for MUI Grid. I've structured my project into 3 sections using MUI grid components. My goal is to restrict the page height in order to eliminate the browser scrollbar. If the conten ...

Ways to prevent a centered list from shifting to the right

Although this question has appeared before, I am facing an issue with a list displayed as an inline block for use as a navigation bar. My goal is to center the nav bar on the webpage and ensure it scales down appropriately. <ul class="nav_bar"> ...

When using NextJs with Material UI, manually refreshing the page may trigger a warning message stating: "Prop `className` did not match. Server: `MuiTypography-root MuiLink`."

In my Next.js project with Material UI, I am facing an issue where hot reloading works fine when I make changes and run the server. However, if I manually refresh the page, all the styles get messed up and I receive a warning saying: Warning: Prop classNam ...

How to keep React Material UI Tooltip open on hover of List items?

In my project, I'm using Material UI to create a dynamic list of users. Each user is displayed as a ListItem with their profile image, and when hovering over the image, I want a tooltip to show up with additional user information. Although the basic ...

Customizing the appearance of a submenu header on a WordPress website

I need assistance in creating a sub-menu style. Please refer to the image linked below for guidance: Here is my website link: ...

The Push Over Menu is malfunctioning and not functioning properly

I am facing an issue with pushing my menu along with the content to the right. The JS code I have is not working as expected. When I click on <div class="menu-btn toggle"></div>, the menu does not trigger. Can someone help me understand why thi ...

Unable to apply Login Form Css to HTML

Creating a Login Form <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>Login Form</title> <link rel = "stylesheet" type="text/css" href="{{ ...

SignalR blocking axios requests (In a Waiting State)

I am currently developing a project that involves bidding on products in Auctions with real-time updates of the bid price. With potentially thousands or even millions of users worldwide participating in the bidding process. We have implemented SignalR for ...

When attempting to use the search bar to filter in ReactJs, an error occurs: TypeError - Unable to access properties of undefined (specifically 'filter')

When I receive data from my JSON server on the console, everything looks good. But when I try to type something in order to filter it, an unhandled error occurs with the message: 1 of 1 Unhandled Error Unhandled Runtime Error: TypeError: Cannot read prop ...

Discovering whether an image contains a caption with the help of JavaScript

There are various websites that display captions on images in paragraphs, h1 tags, or contained within a div along with the image. I am interested in learning how to determine if an image has an associated caption using JavaScript, especially when the cap ...

What is the best way to ensure my hover caption spans the entire size of the image?

I'm currently working on creating a hover caption that adjusts itself to fit the full width and height of an image even when it is resized. I attempted to implement a jQuery solution that I came across, but unfortunately, I am struggling to get it to ...

Every time I configure react.js and import an external link (such as Material UI or Bootstrap React), the screen gets disabled and turns completely white

After setting up my react.js, I encountered an issue where importing bootstrap components such as Container, Row, Col causes the screen to turn white. It seems that HTML and CSS are being disabled, resulting in a completely blank web page. Any ideas on ho ...

Tips for dynamically resizing the content area using CSS without the need for javascript

What I need is as follows: The blue area should resize when the browser window resizes. The header must be visible at all times. The blue area should start right after the header ends, not overlapping or appearing above it. The blue area should end befor ...