96

I am using Material UI components in a React.js project, for some reason I need customization in some components to make it responsive according to screen width.

I have added media query and pass it as style attribute in the components but not working, any idea?

I am using code like this:

const drawerWidth = {
  width: '50%',
  '@media(minWidth: 780px)' : {
    width: '80%'
  }
}

<Drawer
  .....
  containerStyle = {drawerStyle}
 >
 </Drawer>

The code is working for the web only, on mobile devices no effect. Even CSS code is not applying I've checked in the developer console. I am using Material UI version 0.18.7.

Any help would be appreciated.

PS: As per requirement I need to make some changes according to screen size using CSS.

Olivier Tassinari
  • 8,238
  • 4
  • 23
  • 23
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
  • how to define in declaration block for react, can you please answer with an example ? i am quite new to React thts why – Pardeep Jain Aug 23 '17 at 18:53

10 Answers10

160

By using the breakpoints attribute of the theme, you can utilize the same breakpoints used for the Grid and Hidden components directly in your component.

API

theme.breakpoints.up(key) => media query

Arguments

key (String | Number): A breakpoint key (xs, sm, etc.) or a screen width number in pixels.

Returns media query: A media query string ready to be used with JSS.

Examples

const styles = theme => ({
  root: {
    backgroundColor: 'blue',
    [theme.breakpoints.up('md')]: {
      backgroundColor: 'red',
    },
  },
});

For more information check this out

Olivier Tassinari
  • 8,238
  • 4
  • 23
  • 23
amin khademian
  • 2,012
  • 1
  • 8
  • 14
90

You were almost right, but you need to use min-width instead of minWidth:

const styles = {
  drawerWidth: {
    width: '50%',
    '@media (min-width: 780px)': {
      width: '80%'
    }
  }
}
Isti115
  • 2,418
  • 3
  • 29
  • 35
V. Lipunov
  • 1,172
  • 8
  • 5
  • 3
    Good answer. Reference https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/styles/createBreakpoints.js#L24 to see that the methods `up()` and `down()` output a string like shown in the answer, within the square brackets. – nbkhope Jul 15 '18 at 04:23
  • 16
    `['@media (min-width:780px)']` having media query inside array will give `no-useless-computed-key`. Used only '@media (min-width:780px)' and it works. – Deke Feb 19 '19 at 07:03
  • This worked for me: const sx = { '@media (min-width: 780px)': { width: '80%' }}; return ...; – Damien Golding May 29 '23 at 22:56
41

You have a typo in the media query. You should use the following syntax and it will work as expected:

const drawerWidth = {
  width: '50%',
  '@media (min-width: 780px)' : {
    width: '80%'
  }
}

instead of

const drawerWidth = {
  width: '50%',
  '@media(minWidth: 780px)' : {
    width: '80%'
  }
}
croraf
  • 4,332
  • 2
  • 31
  • 50
29

In MUI v5, breakpoints can be declared in sx props by specifying an object where the keys are the breakpoint names and the values are the CSS values.

You can see MUI default breakpoints here. The breakpoint names and values can be overrided using createTheme():

const theme = createTheme({
  breakpoints: {
    values: {
      xxs: 0, // small phone
      xs: 300, // phone
      sm: 600, // tablets
      md: 900, // small laptop
      lg: 1200, // desktop
      xl: 1536 // large screens
    }
  }
});
return (
  <ThemeProvider theme={theme}>
    <Box
      sx={{
        // specify one value that is applied in all breakpoints
        color: 'white',
        // specify multiple values applied in specific breakpoints
        backgroundColor: {
          xxs: "red",
          xs: "orange",
          sm: "yellow",
          md: "green",
          lg: "blue",
          xl: "purple"
        }
      }}
    >
      Box 1
    </Box>
  </ThemeProvider>
);

In the example above, xs: "orange" means set the Box color to orange if the screen width is inside xs range [300, 600).

You can also set the breakpoints using an array consists of the values from the smallest to largest breakpoint:

return (
  <ThemeProvider theme={theme}>
    <Box
      sx={{
        backgroundColor: [
          "red",
          "orange",
          // unset, screen width inside this breakpoint uses the last non-null value
          null,
          "green",
          "blue",
          "purple"
        ]
      }}
    >
      Box 2
    </Box>
  </ThemeProvider>
);

Edit 45847090/media-queries-in-material-ui-components

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
11

Similiar answer to @Lipunov's, based on @nbkhope's comment

const styles = {
  drawerWidth: {
    width: '50%',
    [theme.breakpoints.up(780)]: {
      width: '80%'
    }
  }
}
5

I've solved this problem by doing something like this:

const dropzoneStyles = 
window.screen.availWidth < 780 ? 
{ 'width': '150px', 'height': '150px', 'border': 'none', 'borderRadius': '50%' }
: { 'width': '200px', 'height': '200px', 'border': 'none', 'borderRadius': '50%' };

and then appending it as an attribute in the Material UI element:

<Dropzone style={dropzoneStyles} onDrop={this.handleDrop.bind(this)}>

So the key is to find out the window screen using window.screen.availWidth. And you would be doing this in the render() function. Hope that helps!

limawebdev
  • 75
  • 2
  • 3
5

In my case I just needed the breakpoint on one component and I found the createTheme approach a little bit too much. I ended using useMediaQuery and useTheme.

I see that with useMEdiaQuery you can be quite granular

import { useTheme } from '@mui/material/styles';
import useMediaQuery from '@mui/material/useMediaQuery';

const Component = () => {
  const theme = useTheme();
  const matchesSM = useMediaQuery(theme.breakpoints.down('sm'));
  const matchesMD = useMediaQuery(theme.breakpoints.only('md'));
  const dynamicStyles = {
    ...matchesSM && {margin: '10px 0'},
    ...matchesMD && {margin: '20px 0'}
  }

  return (
    <Grid item xs={12} md={4} sx={{...dynamicStyles}}>
      <div>Children</div>
    </Grid>
  )
}
byverdu
  • 373
  • 7
  • 7
3

In the style property on React you can only define properties that you can define in a normal DOM element (You can't include media queries for example)

The way you can include media queries for that component would be passing a class name to the Drawer Component

<Drawer containerClassName="someClass" /> 

And then in a CSS file you do something like this

@media(min-width: 780px){
  .someClass {
    width: 50%!important;
  }
}
Miguel
  • 1,577
  • 2
  • 15
  • 29
  • That i know, but this does't make any sense, it will just add Css Class to that container no to the inner element or container where needed, isn't it ?. – Pardeep Jain Aug 23 '17 at 19:07
  • Sorry It should be containerClassName instead of className (Updating the answer), that should add the class to the drawer container and then you can easily modifed its width with CSS – Miguel Aug 23 '17 at 19:20
  • I just used this (drawer) as example, in none of the case my media queries are working. – Pardeep Jain Aug 23 '17 at 19:27
  • It seems like the drawer add inline-styles to the container so that styles have more precedence, you can try with !important at the end of the width – Miguel Aug 23 '17 at 19:33
  • yes , i did agree that it works, but only on the parent container not on child of that container. also this is not a preferred way anyways. – Pardeep Jain Aug 26 '17 at 06:23
  • This answer should be deleted, it relies on an external css file instead of properly using the theming break points. No need to use the framework if you just going to use vanilla css. – Husk Rekoms Dec 13 '20 at 21:48
2

CSS media queries are the idiomatic approach to make your UI responsive. The theme provides five styles helpers to do so:

theme.breakpoints.up(key)
theme.breakpoints.down(key)
theme.breakpoints.only(key)
theme.breakpoints.not(key)
theme.breakpoints.between(start, end)

In the following stress test, you can update the theme color and the background-color property live:

const styles = (theme) => ({
  root: {
    padding: theme.spacing(1),
    [theme.breakpoints.down('md')]: {
      backgroundColor: theme.palette.secondary.main,
    },
    [theme.breakpoints.up('md')]: {
      backgroundColor: theme.palette.primary.main,
    },
    [theme.breakpoints.up('lg')]: {
      backgroundColor: green[500],
    },
  },
});
<Root>
  <Typography>down(md): red</Typography>
  <Typography>up(md): blue</Typography>
  <Typography>up(lg): green</Typography>
</Root>

Know more

MD SHAYON
  • 7,001
  • 45
  • 38
1

Create a variable and then use that variable anywhere in the function

import React from 'react';
import { createMuiTheme, ThemeProvider, useTheme } from '@materialui/core/styles';
import useMediaQuery from '@material-ui/core/useMediaQuery';
    

function MyComponent() {
  const theme = useTheme();
  const matches = useMediaQuery(theme.breakpoints.up('sm')); // Variable for media query 
  return <span hidden={matches}>Hidden on screen size greater then sm </span>;
}

const theme = createMuiTheme();

export default function ThemeHelper() {
  return (
    <ThemeProvider theme={theme}>
      <MyComponent />
    </ThemeProvider>
  );
}
Syed Ali Shahzil
  • 1,079
  • 1
  • 11
  • 17