14

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?

2
  • Usually css consider the last update of the css. For an example if your have a css on line 5 and another one on line 10, but both the same element and same property. Then css consider the line 10th one is the valid one. That's how your css is over ridden.Arrange the code properbly like the media query for largest device on the top and smaller device after it. Commented Aug 27, 2018 at 16:17
  • Good point, however, this is not a regular css file but a theme override for material-ui.com components. My question relates to how to specify (if possible) an @media replacement for the Toolbar component using their theme over-ride capability. Commented Aug 27, 2018 at 16:49

3 Answers 3

26

You have to add it like below. The media query statement should be enclosed in single quotes.

 MuiToolbar: {
      regular: {
        backgroundColor: "#ffff00",
        color: "#000000",
        height: "32px",
        minHeight: "32px",
        '@media (min-width: 600px)': {
          minHeight: "48px"
        }
      },

Please find the codesandox - https://codesandbox.io/s/q8joyrrrwj

Sign up to request clarification or add additional context in comments.

3 Comments

This works nicely! Your code sandbox link however points to a different solution. Please update or remove your sandbox link and I'll mark this as the answer.
Image
Sorry Glenn, i updated the same codesandbox for another question. I have created a new CodeSandbox with the required solution.
The bad thing about this solution is that we need to know which media query we need to override. If the component's internal code changes, the theme override will lose its effect. This can be solved with something like [defaultTheme.breakpoints.up('md')]: { insted of '@media (min-width: 600px)': {
4

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"
        }
      },
   }
});

Comments

0

MuiThemeProvider is optional for v1.x material-ui and We use the higher-order component created by withStyles to inject an array of styles into the DOM as CSS, Here is working example without changing @media annotation:

https://codesandbox.io/s/7klzx84z71

2 Comments

This works when doing a single element but I want to have it apply to all instances of Toolbar without having to code it into each component that uses it. The solution from Vijay allows it to be applied at a global theme level which is what I'm looking for.
Yes in that case Vijay approach is feasible. I second that. Actually I my solution was to customise your toolbar individually if that’s the case. But in your case you want sane styles to be applied on all toolbars. Anyways good work ))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.