Reputation: 8374
I'm trying to build a mobile web application using react material ui library.
But it seems the material UI components are not responsive. They display very well on desktop, but when I open it on my mobile browser, the fonts are very small.
Isn't material UI meant to be used to build mobile App? Why it's even not responsive?
Upvotes: 22
Views: 17384
Reputation: 319
Assuming that you did add the viewport, you can use breakpoint to make your webpage responsive as suggested by @Lane.
Steps:
Declare a variable. This returns true if the breakpoint is hit otherwise false.
const matches = useMediaQuery('(max-width:600px)');
Reference: https://mui.com/components/use-media-query/
Use the sx
props to set the width using the variable from step 1 with ternary operator
<Paper sx={{ width: matches === false ? '40vw' : '80vw' }} />
Reference: https://mui.com/system/the-sx-prop/
Example:
const Component = () => {
const matches = useMediaQuery('(max-width:600px)');
return (
<Box
sx={{
'& .MuiTextField-root': { **width: matches === false ? '35vw' : '70vw'** },
}}
>
{/* ... */}
</Box >
);
}
Upvotes: 1
Reputation: 1179
you have a index.html
file in your public folder.
add this line at the beginning of your <head>
tag.
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
Upvotes: 6
Reputation: 4996
https://material-ui.com/customization/breakpoints/
https://material.io/design/layout/responsive-layout-grid.html#columns-gutters-and-margins
Use the breakpoint like:
<Grid container>
<Grid xs={12} md={6} lg={5} item>
</Grid>
<Grid xs={12} md={6} lg={7} item>
</Grid>
</Grid>
Upvotes: 2
Reputation: 463
You must add the directive in the section.
Add to your project in which you initiate the application:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
The viewport is the user's visible area of a web page. It varies with the device, and will be smaller on a mobile phone than on a computer screen.
A viewport element gives the browser's instructions on how to control the page's dimensions and scaling.
The width = device-width part sets the width of the page to the screen-width of the device.
The initial-scale = 1.0 part set the first zoom in the browser.
Upvotes: 34