Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to over-ride an @media css for a material-ui react component

In this question I asked how to override a css property of a material-ui component and was provided a nice example. However, when trying to set the height of the Toolbar component I found I could not override the css due to an over arching @media specification. The following MuiTheme spec was my approach:

const theme = createMuiTheme({
    overrides: {
        MuiToolbar: {
            regular: {
               height: "32px",
               minHeight: "32px"
        }
    },
  }
});

Here is a visual of the css being over-ridden:

enter image description here

If I introduce a hack and add !important to the minHeight it works. A codesandbox showing this is here: https://codesandbox.io/s/4xmr2j2ny9

What is the proper way to overide an @media spec using MuiTheme?

like image 711
Glenn Avatar asked Aug 27 '18 16:08

Glenn


People also ask

How do you override CSS in material UI components?

To override the styles of a specific part of the component, use the global classes provided by Material UI, as described in the previous section "Overriding nested component styles" under the sx prop section.

How do you override a material UI SCSS?

You can however use the classes prop to override a material-ui className. For example <Button classes={{ root: styles. button }} /> . This will remove all material-ui styling though.

Can I use material UI with SCSS?

If you want to use CSS/SCSS class in MUI component, you should import the file directly in the Table component. But, it's not good to use SCSS with MUI component, you should use makeStyles or withStyles to style the MUI component.

How do I use mediaquery in material UI?

To use the useMediaQuery hook, first import it from Material-UI. import { useMediaQuery } from '@material-ui/core'; In the component, call the useMediaQuery hook and pass in a media query as the argument. This will return a true or false value.


1 Answers

For the record, the latest solution for "@mui/material": "5.0.4", combined from https://stackoverflow.com/a/52044661/646105 and @jannnik comment, would be:

const defaultTheme = createTheme();

const theme = createTheme({
  components: {
    MuiToolbar: {
      regular: {
        backgroundColor: "#ffff00",
        color: "#000000",
        height: "32px",
        minHeight: "32px",
        [defaultTheme.breakpoints.up('sm')]: {
          minHeight: "48px"
        }
      },
   }
});
like image 200
Titenis Avatar answered Oct 14 '22 08:10

Titenis