Unable to override default styles with custom styles in React MUI 5

I've been attempting to replace the default MUI 5 Button styles with my custom styles using either withStyles or a className (created using makeStyles), but I'm running into an issue where the default styles take precedence over my custom ones. I managed to address this problem with withStyles by including those styles in the global style overrides when defining the theme, but the className-based approach is proving problematic.

Does anyone know how to resolve this? Below are some screenshots illustrating the strange behavior.

Edit: Here's the code used to define the custom styles:

import { Theme } from '@mui/material';
import { makeStyles } from '@mui/styles';

const BTN_WIDTH = 160;
const BTN_MIN_HEIGHT = 34;

export default makeStyles((theme: Theme) => ({
  btn: {
    width: BTN_WIDTH,
    minHeight: BTN_MIN_HEIGHT,
    paddingLeft: theme.spacing(2),
    paddingRight: theme.spacing(2),
  }
}));

https://i.sstatic.net/A1943S8J.png
^ A screenshot revealing the button styles in question. (Some default styles were excluded for brevity). The screenshot demonstrates how the default styles are overriding the paddings specified by the custom styles.

Answer №1

It appears that your class is being overridden due to having the same specificity as the MUI classes. You have three options to address this issue.

  1. Reverse the order of imports: Your class is being overridden because it has the same specificity and is imported after the MUI import. This option may be challenging to implement as managing imports can be complex.

  2. Add !important to all properties in your class (e.g: width: 100px !important). This approach is considered unclean and not recommended.

  3. Utilize specificity to your advantage by applying a higher-specificity class. For example, you could use a CSS rule like the following:

div {
  width: 100px;
  height: 100px;
}

.custom-div {
  background: red;
}

.important-div {
  background: black;
}

.important-div#very-important {
  background: green;
}
<div class="custom-div important-div" id="very-important"/>
In this example, despite the background property being defined multiple times, the rule with the highest specificity takes precedence (such as the one with an ID).

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

What are the reasons behind the inability to import an image file in Next.js?

Having an issue with importing image file in Next.js I'm not sure why I can't import the image file... The image file is located at 'image/images.jpg' In Chrome browser, there are no error messages related to the image, However, in ...

Concealing a hyperlink depending on the value chosen in a dropdown menu

Looking for guidance on how to use jQuery to read the current value of a drop-down list and hide a specific link based on that value. The drop-down list is for selecting the language/locale. For example, when "English" is selected, I want the link to be h ...

Changing the Title of UI Tabs

I am facing an issue with setting the title in tabs, as the title is appearing behind the background. If I set background: transparent; in the #tabss .ui-tabs-nav class, then my title shows up properly. Otherwise, it gets displayed behind the background. ...

Damaged background in Bootstrap interface modal

https://i.stack.imgur.com/nTAnK.png Is there anyone who can assist me in identifying the issue causing the broken or irregular backdrop in the background of overlays or pop-ups? $scope.Modal = $modal.open({ animation: true, ...

choose a selection hopscotch

Is there a way to prevent the input field from jumping out of alignment when an option is selected in these q-select inputs? I want to fix this issue so that the field remains in line with the horizontal alignment. https://codepen.io/kiggs1881/pen/RwENYEX ...

When viewed on a mobile device, the entire HTML document is resized to occupy less than half of the

Whenever I adjust the size of my webpage on different devices or mobile phones, I notice that the <div> tag appears to be only half (or even less) the width of the page. This gap seems to increase as the window size decreases. Refer to the screenshot ...

Error occurs when attempting to clear the cache storage

useEffect(async () => { caches.open('my-cache') }, [token, user_id]) Upon trying to log out of the application, const logoutFunc = () => { localStorage.clear() caches.keys().then((list) => list.map((key) => { ...

What could be the reason for the useEffect return function to execute before the previous function call?

Upon navigating to the Slider component, the logs will display in succession: i init i destroyed Interestingly, when I move away from the component, there are no logging occurrences. I anticipated seeing "i destroyed". My assumption was that upon enterin ...

Concealing information based on the value of a variable

Creating a dynamic price estimator. I need help writing a jQuery function that can hide or show a specific div element based on the value of a variable. For example, let's say I have an HTML div with the ID 'Answer': <div id="answer"&g ...

Exploring the functionality of using getStaticProps in conjunction with the next-redux

I am currently in the process of developing a straightforward next.js application. To manage state, I have opted for next-redux-wrapper. In the main page of my app, I aim to retrieve all the rooms stored in my database via my API using store.dispatch. Uti ...

Unable to find the designated ./uploads directory while attempting to upload a file in Express JS

I'm currently working on a project with the following structure: -app -uploads -client - dropzone.js -server.js My goal is to upload a file using dropzone js, Express JS, and React JS. However, whenever I try to drop a file in ...

Reactjs is failing to display the page

I am facing an issue where the components are not rendering in the browser even though there is no error being displayed. This makes it difficult to troubleshoot and resolve the problem. Can someone help me identify the root cause? import React, { Com ...

Importing filter function from Material UI to use in the AutoComplete component

Can't figure out if it's a bug or just my lack of understanding in react. No errors displayed, but the filter function isn't working as expected. When I place the function in the same file, the filter works fine. Am I missing something here? ...

Is it possible to load images top to bottom?

Is there a way to make browsers load images from the bottom to the top? Imagine I have an extremely long image that takes 60 seconds to load. The content is designed to be read from bottom to top. Is there any trick I can use to ensure that the image load ...

Why do I receive a URL like this when attempting to download a video from YouTube?

Can anyone explain to me why I am getting this URL error while trying to pull a video from YouTube? Error in console: GET http://localhost:3000/www.youtube.com/watch?v=IEDEtZ4UVtI 404 (Not Found) GET http://localhost:3000/www.youtube.com/watch?v=IEDEtZ4UVt ...

Guide to expanding the sidebar width

As I delve into the world of CSS, I find myself puzzled by the issue of expanding the sidebar to ensure that the "Gestion des utilisateurs" section appears on a single line. https://i.stack.imgur.com/xzFEA.png To provide context, I have included an illus ...

Is it possible to modify an HTML element when hovering over it in an ASP.Net page?

Is there a way to use jQuery to show the child span text - SomeClassChild3 string when hovering over the parent div - SomeClassParent in an aspx page? Here is the JavaScript section of the code: function ShowDiv(Somedata){ for(var v in Somedata){ va ...

Ways to apply distinct styles to various ids and common classes

When dealing with multiple IDs such as id1, id2, and id3 that share common classes like .selectize-input and .select-dropdown, it can become cumbersome to set styles individually for each ID. Instead of writing: #id1 .selectize-input, #id2 .selectize-inp ...

New Feature in Bootstrap4: Navigation Bar Alignment Shift to the Left

I need help aligning my links to the right side of the page I attempted to use mr-auto but it was not effective <body> <nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top mr-auto mt-2 mt-lg-0"> <a class="navbar-brand" hre ...

How can I locate the CSS selector for a text message?

Looking for a CSS selector to target the message "Congratulations! displayed after successfully filling out a registration form with Selenium Web driver. Need to assert the text "Congratulations, Your email has been verified". Here is the HTML code: <d ...