Overriding makeStyles CSS in Material UI Styling

When creating classes using makeStyles and useClasses, I've noticed that MUI CSS tends to take precedence over my custom styles unless I use the '!important' rule. Is there a way to override MUI styles without having to rely on '!important'? It seems inconsistent as some default MUI CSS fails when necessary. Interestingly, the logo in the same code functions properly even without using '!important'. Here is an example of MUI CSS overriding makeStyles CSS.

import React, { useState, useEffect } from "react";
import AppBar from "@mui/material/AppBar";
import { makeStyles, createStyles } from "@mui/styles";
import Tabs from "@mui/material/Tabs";
import Tab from "@mui/material/Tab";
import Toolbar from "@mui/material/Toolbar";
import logo from "../../assets/logo.svg";
import Button from "@mui/material/Button";
import { Link } from "react-router-dom";

const useStyles = makeStyles((theme) => ({
    toolbarMargin: {
        ...theme.mixins.toolbar,
        marginBottom: "3em",
    },
    logo: {
        height: "8em",
    },
    tabContainer: {
        marginLeft: "auto",
    },
    tab: {
        ...theme.typography.tab,
        minWidth: 10,
        marginLeft: "25px !important",
    },
    button: {
        ...theme.typography.estimate,
        borderRadius: "50px !important",
        marginLeft: "50px !important",
        marginRight: "25px !important",
        height: "45px !important",
    },
    logoContainer: {
        padding: "0px !important",
        "&:hover": {
            backgroundColor: "transparent",
        },
    },
}));


export default function Header(props) {
    const classes = useStyles();
    return (
        <React.Fragment>
            <ElevationScroll>
                <AppBar position='fixed'>
                    <Toolbar disableGutters>
                        <Button
                            disableRipple
                            className={classes.logoContainer}
                            component={Link}
                            to='/'
                        >
                            <img src={logo} alt='company Logo' className={classes.logo}></img>
                        </Button>
                        <Tabs
                            className={classes.tabContainer}
                            value={0}
                            textColor='#fff'
                            onChange={handleChnage}
                            indicatorColor='primary'
                        >
                            <Tab
                                className={classes.tab}
                                component={Link}
                                to='/'
                                label='Home'
                            />
                            <Tab
                                className={classes.tab}
                                component={Link}
                                to='/services'
                                label='Services'
                            />
                        </Tabs>
                        <Button
                            variant='contained'
                            className={classes.button}
                            color='secondary'
                        >
                            Free Estimate
                        </Button>
                    </Toolbar>
                </AppBar>
            </ElevationScroll>
        </React.Fragment>
    );
}

Answer №1

If you prefer a cleaner organization, you can consolidate all these styles into a CSS file and then import that file at the end of your HTML document.

Alternatively, you have the option to use the !important tag on each style that has been overridden:

const customStyles = makeStyles((theme) => ({
    toolbarMargin: {
        ...theme.mixins.toolbar,
        marginBottom: "3em" !important,
    },
    logo: {
        height: "8em" !important,
    },
    tabContainer: {
        marginLeft: "auto",
    },
    tab: {
        ...theme.typography.tab,
        minWidth: 10px !important,
        marginLeft: "25px !important",
    },
    button: {
        ...theme.typography.estimate,
        borderRadius: "50px !important",
        marginLeft: "50px !important",
        marginRight: "25px !important",
        height: "45px !important",
    },
    logoContainer: {
        padding: "0px !important",
        "&:hover": {
            backgroundColor: "transparent",
        },
    },
}));

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

Strategies for transferring retrieved data to the getServerSideProps function

I am currently utilizing the Context API to retrieve data and then pass that data to the getServerSideProps function, but encountering the following error: The React Hook "useContext" is being called in a function "getServerSideProps" that is neither a Re ...

Get the QR code canvas image with a customized background by downloading it

I am having trouble with downloading a QR code canvas image with a background. The download works fine, but my device is unable to scan the code properly due to an incomplete border. I need to add a white background behind it to make it larger. Current im ...

I'm having an issue with my NextJS timer where it doesn't stop and ends up going into negative numbers. Any

Here is the code snippet for my timer component. It fetches the 'createdAt' prop from the database and functions correctly. However, I have encountered an issue where the timer does not stop at 0 but continues running indefinitely. I attempted to ...

I am currently working on creating a system for displaying rating stars for products. The rating value, which ranges from 0 to 5, has already been provided

class PostBodyRatingStars extends React.Component { render() { const { classes } = this.props var rating = this.props.rating function createMappingArray(rating) { var arr = [] for (i = 0; i < 5; i++) { if (rating >= ...

Setting up Material-UI in an ASP.NET Core project for a sleek and responsive

Looking to incorporate material design into my asp.net core project using Visual Studio 2019. https://i.stack.imgur.com/J2aJe.png https://i.stack.imgur.com/3Axk1.png Now the question is, how do I integrate these designs into my project? A sample code sni ...

Steps to create a hover effect similar to that of a website (increasing grid size on hover)

Looking to create a hover effect for my boxes similar to the one on this website: I've examined the code of the website above and searched extensively for a similar feature without any luck. Could anyone offer assistance with this, please? ...

Design your own custom next and previous buttons for material ui pagination

Is there a way to replace the next and prev icons with text "next" and "prev" in material-ui pagination? I have not found any prop for this. Can anyone suggest a workaround? ...

The imported font used in Firefox is displaying with a striking underline text-decoration

I am currently using the Cardiff font in a project and attempting to apply the style text-decoration:underline to it. While this works perfectly in Chrome (Version 35.0.1916.114), in Firefox (Version. 29.0.1) the underline appears to be crossing through t ...

Implementing the useCallback hook to retrieve information when a button is clicked

In my current scenario, I need to fetch data when a button is clicked and then store it in a context variable. Here is the code snippet for reference: const [products, setProducts] = useState([]); const handleButtonClick= useCallback(() => { if (c ...

What is the best way to implement a dynamic back button in Next.js?

Being familiar with creating a standard back button, I am now eager to craft one that directs the user back by one step in the URL rather than returning to the previous page. This way, I can utilize the button in various locations without needing to alter ...

The useRef hook does not seem to be returning the ref object when used in multiple shared components

One challenge I am facing is accessing the refs in a shared BaseTable component. An example scenario would be on a parent route component: function Route() { <div> <BaseTable /> <BaseTable /> </div> } function BaseTable ...

Tips on adding an item to an array with React hooks and TypeScript

I'm a beginner with a simple question, so please bear with me. I'm trying to understand how to add an Object to the state array when a form is submitted. Thank you for your help! interface newList { name: string; } const ListAdder = () => { ...

What is the best way to restrict the size of a table that is filled with data from a database?

Currently using a combination of React, Node, Express, and Postgres to populate a table with data retrieved from Postgres. The issue arises when the table becomes overly long, prompting the need to display only 5 rows at once while adding a scroll bar for ...

The crash during compilation is triggered by the presence of react-table/react-table.css in the code

My code and tests are functioning properly, but I am facing a challenge with my react-table file. The react-table.js API specifies that in order to use their CSS file, I need to include import "react-table/react-table.css"; in my react-table.js file. Howev ...

Error: The module 'react' was not found in Material-ui List

Hey there, I recently completed the React official tutorial and got my tic-tac-toe game up and running smoothly. Excited to enhance it with some material-ui components, I went ahead and installed them using: npm install --save @material-ui/core npm instal ...

Discover the Magic Trick: Automatically Dismissing Alerts with Twitter Bootstrap

I'm currently utilizing the amazing Twitter Bootstrap CSS framework for my project. When it comes to displaying messages to users, I am using the alerts JavaScript JS and CSS. For those curious, you can find more information about it here: http://get ...

Explore the world of React with the captivating react-image-gallery on JSFiddle

Currently in the process of setting up a React JSFiddle that incorporates react-image-gallery. Utilizing UNPKG, I have managed to successfully connect to the package: https://unpkg.com/browse/[email protected] / However, there are still some obstac ...

What is the best way to design an element in HTML/CSS with a curved semicircle border at the top?

I'm looking to design an HTML/CSS element that features a semicircle at the top-middle with a border. Inside this semicircle, I want to place a smaller circle containing a single character (similar to the X in the provided image). Additionally, there ...

Next.js not displaying Contentful Richtext Editor

I'm currently in the process of setting up a new blog using Next.js and Contentful. Everything was going smoothly until I encountered an issue with the Richtext editor. While the content is displaying, there seems to be no line spacing between the pa ...

Displaying a div when a radio button is clicked in React

I am attempting to create a functionality where clicking on one radio button will display another div in React. As a beginner in React, I have tried implementing the code below but encountered issues with hiding and displaying the content based on user inp ...