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

I've encountered some issues with importing pagination from modules after installing SwiperJs

Having some issues with importing pagination from modules in SwiperJs for my nextjs project. The error message "Module not found: Package path ./modules is not exported from package" keeps popping up. I have tried updating the module to the latest version ...

What is the best way to incorporate custom CSS into an angular-fullstack application?

I'm interested in incorporating an image into a jumbotron and adjusting its width correctly, similar to what was discussed in this question: Bootstrap image to jumbotron width However, I am unsure of where to place my custom CSS code to achieve the d ...

Struggling to deploy a Typescript React / NestJS application on Heroku due to the error message "error TS2307: Cannot find module"?

Switching from a Typescript React/Express app to using Nest.JS has caused complications when deploying to Heroku. The app runs smoothly locally, but encounters build failures on Heroku. Despite efforts to troubleshoot, it remains unclear whether the issues ...

How can I center a Bootstrap modal vertically and make it responsive?

Is there a way to vertically center the bootstrap modal? I have been searching for a solution, but none seem to be responsive or effective. My website is using Bootstrap 3. The modal does not adjust properly on smaller screens or when the browser window i ...

Having trouble getting the CSS class to work in MVC4 with jQuery implementation

I recently created a view page where I implemented some jQuery code: <script type="text/javascript"> $(document).ready(function () { var hdrname = $('#headername').text(); alert(hdrname); if (hdrname == "Pending Assignment") { ...

"Reconfigure web page layout to be perfectly centered

Currently, I am utilizing Angular Drywall to construct my website. By default, the alignment is centered which does not suit the template requirements that call for it to occupy the full 100% body width. body{ width:100%; margin-left:0; paddin ...

React.js - "Encountered error: Unable to access 'map' property of undefined variable"

I've tried various methods I found through searching, but none of them seem to be working. Why is it still showing that map is undefined? import TextField from '@material-ui/core/TextField'; import Autocomplete from '@material-ui/lab/A ...

The script type `(close: any) => Element` cannot be assigned to type `ReactNode`

Encountering a problem while adding a popup in my TypeScript code Error: Type '(close: any) => Element' is not compatible with type 'ReactNode'. <Popup trigger={ <button className="fixed bott ...

Creating dynamic CSS in Angular 2 and beyond

When working with a JavaScript variable containing style information, I encountered different approaches in AngularJS and Angular2+: menuBgColor = "red"; In AngularJS, I could use dynamically embedded styles like this: <div> <style> ...

How to ensure that a React Material-UI Fab Button remains fixed in a specific position on the screen

My project involves creating a CRUD application using React and Material UI. One of the requirements is to have a main screen that displays records in a table format, with a floating Fab button positioned above the table. The challenge is to ensure that th ...

The alignment of the text next to the icons is not correct

Is it necessary to use flexbox, or is there an easier way to achieve the same result? It has been a while since I last practiced and I can't seem to remember much. .sadaka-contacts p { color: #115c9b ...

Guide on how to retrieve a clicked element

When I click on the <ul> inside this div, I want to retrieve the id="nm". <div id="suggestionTags"> <ul> <li class="nm">Commonwealth</li> <span class="cty"> x GB</span> <li class="hse">C ...

Learn how to alter the website's overall appearance by changing the background or text color with a simple click on a color using Angular

Is there a way to dynamically change the background color or text color of the entire website when a user clicks on a color from one component to another? I know I need to use the Output decorator, but how can I implement this? style.component.html <di ...

Interactive pop-up window featuring conversational chat boxes

Trying to create a comment box within a Modal dialog box that is half of the width. The comments in this box should be read-only and created using div tags. Attempted reducing the width and using col-xs-6, but ending up with columns spanning the entire w ...

Is there a way to collapse child elements in a hover sidebar using Vuetify?

My project includes a sidebar that opens when I hover over it with the mouse. Everything works fine, but within the sidebar, there is a collapse dropdown menu. When I open this menu and then move the mouse away from the sidebar and back again, the collapse ...

The alignment of the website design is off

I'm in the process of creating a custom template for my website and everything was going smoothly until I encountered an issue with the news bar placement. Instead of appearing below the navbar and welcome message bar, it's showing up above the n ...

What is preventing me from accessing React state within Tracker.Autorun?

I'm feeling lost on this one. I have implemented a Tracker.autorun function to monitor when my Mongo subscription is ready for querying (following the advice given in this previous Meteor subscribe callback). It seems to be working fine as it triggers ...

Removing API request in React.js

My approach: deleteSample = () => { this.sampleService .deleteCall(this.props.id) .then((response) => { window.location.reload(false); }) .catch((error) => { console.log ...

Tips for adjusting the positioning of a div element in a responsive design

I am currently working on my website and facing a layout issue. In desktop mode, there is a sidebar, but in mobile view, the sidebar goes down under the content displayed on the left side. I would like the sidebar to appear at the top in mobile view, fol ...

What are some strategies for consistently monitoring the visibility of my React application on the webpage?

I am looking for a way to ensure that a specific React application is always present on my website. If the URL is broken or if there is an error in the React app, I want to be notified. How can I continuously monitor a given URL to check if the page contai ...