CSS is functioning properly on a local level, but fails to take effect once the "npm run build" command is executed in the React application's build

I am attempting to override the CSS of Muidrawer-paper. It works locally after running "npm start", but it does not take effect when building the package.

.css-12i7wg6-MuiPaper-root-MuiDrawer-paper {
    position: absolute !important;
    top: 50px !important;
    outline: 0;
    left: 10px !important;
    max-width: 35px;
    padding-right: 35px !important;
    background: color-mix(in srgb, #D8D8D8, transparent 18%) !important;
}

Locally:

CSS takes effect.

After building the package:

The CSS is not being applied.

Why isn't my CSS taking effect in the build package?

The CSS should remain consistent both locally and in the build package when the React application is running.

Header.js

...

Header.css

...

App.js:

...

App.css:

...

Answer №1

When you compile your React application for production, Material UI automatically alters all class names and compresses your code. This is why any customized CSS styles you apply may not work since the classes are modified or removed in the production build.

The recommended method for customizing MUI components involves creating a theme and implementing it at the root level or adding inline styles. While both approaches can be utilized, I will provide an example using inline styles as it is easier to understand before diving deep into MUI customization.

To adjust the styling of the paper component within the Drawer, you must include the PaperProps prop in the drawer element. Here's an illustration of how this can be achieved:

An excerpt from your Header.js Code (Modified)

return (
  <Box sx={{ display: 'flex' }}>
    <AppBar position="fixed" color="default" >
      <Toolbar variant="dense">
      </Toolbar>
    </AppBar>
    <Drawer 
      variant="permanent" 
      open={open}
      PaperProps={{
         sx: {
            position: "absolute !important",
            top: "50px !important",
            outline: 0,
            left: "10px !important",
            maxWidth: "35px",
            paddingRight: "35px !important",
            background: "color-mix(in srgb, #D8D8D8, transparent 18 %) !important"
         }
      }}
    >
      <List>
        {[
          "Home",
          "Layers",
        ].map((text, index) => (
          <ListItem key={text} disablePadding sx={{ display: "block" }}>
            <ListItemButton
              sx={{
                minHeight: 48,
                justifyContent: open ? 'initial' : 'center',
                px: 2.5,
              }}
            >
              <ListItemIcon
                sx={{
                  minWidth: 0,
                  mr: open ? 3 : 'auto',
                  justifyContent: 'center',
                }}
              >
                <Tooltip title="Home">
                  {index === 0 ? <HomeIcon /> : null}
                </Tooltip>
                <Tooltip title="Layers">
                  {index === 1 ? <LayersIcon /> : null}
                </Tooltip>
              </ListItemIcon>
              <ListItemText primary={text} sx={{ opacity: open ? 1 : 0 }} />
            </ListItemButton>
          </ListItem>
        ))}
      </List>
    </Drawer>
  </Box>
);

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

Modify the directory (app) within the Next.js app router

Hey there, I am looking to change the default app-router folder (app) in Nextjs I have tried searching for a solution but couldn't find one I want to rename it from: src/app/layout.tsx to src/nextapp/layout.tsx Does anyone know how to do this? I&ap ...

Enable the set body to have certain sections that are scrollable by setting overflow=hidden

I am currently working on a website that features the following structure: <body> <section></section> <section></section> ... </body> To prevent scroll bars on the body, I have set the CSS property body{overflow:hidden ...

Upgrading to React Router v6: Implementing Loader Functions with Context API

Having issues implementing loaders in React-Router V6 while making a request for a page through a function located in the context file. Unfortunately, I can't access the context from main.js where the router is defined. Main.js import ReactDOM from & ...

Issue: Unable to authenticate the initial certificate following the configuration of an id_rsa

Upon executing the commands below, I encountered an error while trying to start a nextJS app: ssh-keygen -t rsa -C "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2afbba7afa3abae82a7baa3afb2aea7eca1adaf">[email pr ...

What is the best way to show an element when hovering over another element?

I'm looking for a CSS-only solution (NO JQuery) to display a FontAwsome icon (<i class='fas fa-arrow-alt-circle-left fa-lg arrowbounce text-success hidearrowbounce'></i>) only when hovering over another element. However, despite ...

Exploring the World of React Variables

Recently, I've been diving into the world of React and encountered some challenges while styling my components. Personally, I find it more organized to style within the same JavaScript file instead of having a separate one. However, I'm curious a ...

image in the background following the header

Live Example: http://jsbin.com/opokev/54 I am currently trying to set this image as the background image while also including a header, but as you can see in the demo, the header is overlapping the image. Is there a way I can rearrange this so that the h ...

Can you provide guidance on how to pass props to a component through a prop in React when using TypeScript?

Hey there, I'm facing an issue with TypeScript where the JavaScript version of my code is functioning properly, but I'm having trouble getting the types to compile correctly. In an attempt to simplify things for this question, I've removed ...

I'm looking to keep the footer anchored at the bottom without using the absolute positioning

I am currently developing a website and have implemented a wrapper for the footer. My goal is to create a sticky footer, but when I minimize the screen, the footer ends up overlapping with the content and header. #footerWrapper { height: 177px; bottom: ...

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 ...

Adjust text to fit within a specified height container automatically

Is there a way to make text resize automatically based on the height and width of its container? I've looked at similar questions but none seem to provide the right solution for me. I tried using fitText, but it doesn't take the container's ...

The usage of the enzyme shallow() function combined with the addition of event listeners

A specific component is causing some issues in my application: class ProblematicComponent extends Component { componentDidMount() { this.monitorForClicks(); } monitorForClicks() { this.elementRef.addEventListener('click', () => ...

Struggling to update state using useState in ReactJS - value remains unchanged

I am facing an issue with updating an id from two tables using a table component that is being used twice on the same page. When I click on a row, the id should be updated to the parent component. I have attempted to set it to a constant using the useState ...

What is the best way to inject child nodes into parent nodes without causing the view to shift to the location where the element is being

While I am rendering elements and appending them to a parent div, my screen keeps jumping to the bottom-most element as it loads instead of maintaining the current view. Ideally, it should start at the top of the screen and remain there without any sudden ...

What is the best way to create a JavaScript function that can be used for multiple expandable cards in HTML and CSS?

I'm dealing with a situation where I have a list of cards, and I want to be able to expand the content individually when clicking on "more info". Can someone offer advice on how to achieve this using Javascript? Check out this CodePen for reference: ...

React Checkbox Sum Component - Effortlessly Improve User Experience

Looking for assistance with passing checkbox values to a React component in my app. I'm currently implementing a food list display using JSON data, but I'm unsure how to transfer the checkbox values to another component. For reference, here&apos ...

Aligning icons and text vertically in a list

I've encountered this issue multiple times and I'm always unsure of the best approach to resolve it. Here is a screenshot for better context: This is what it should look like... .container { max-width: 360px; font-size: 16px; } .talking-poin ...

Whenever I try to log in using axios and my credentials are incorrect, I don't receive any response back in the catch block

I cannot receive any response when calling the login API from Node.js. In the frontend, I am handling the catch as well. How do I retrieve the Invalid Credentials message from the backend API if the credentials do not match? The code for my backend logi ...

Is there a way to globally modify the styling of all Material UI Typography components within a single React component without having to individually apply a class to each element

I'm having trouble figuring out the best way to style React SPA with Material UI components. It seems like I might be missing a key concept. For example, consider this Header component: const Header = () => ( <AppBar position="fixed" ...

The React component designed to consistently render video frames onto a canvas is unfortunately incompatible with iOS devices

I'm facing an issue with my code snippet that is supposed to draw video frames on a canvas every 42 milliseconds. It's working perfectly on all platforms and browsers except for iOS. The video frames seem unable to be drawn on canvas in any brows ...