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

半世苍凉 提交于 2019-12-08 17:09:51

问题


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:

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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/52043271/how-to-over-ride-an-media-css-for-a-material-ui-react-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!