57

I'm using Material-UI version 1. installed by this command:

npm install -S material-ui@next

Everytime I want to use , an unwanted horizontal scroll appears in the page.

Code:

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles, createStyleSheet } from 'material-ui/styles';
import Paper from 'material-ui/Paper';
import Grid from 'material-ui/Grid';


/* project imports */
import NavMenu from './Page-Parts/NavMenu';
import LoginPanel from './Login-Parts/LoginPanel';

const styleSheet = createStyleSheet('FullWidthGrid', theme => ({
  root: {
    flexGrow: 1,
    marginTop: 0,
  },
  paper: {
    padding: 16,
    textAlign: 'center',
    color: theme.palette.text.secondary,
    marginTop: "3rem"
  },
}));

function Login(props) {
    const classes = props.classes;
    return (
    <div className={classes.root}>
      <Grid container gutter={24} justify='center'>
        <Grid item xs={12} md={12} sm={12} lg={12}>
          <NavMenu/>
        </Grid>
        <Grid item xs={6} sm={6} md={6} lg={6}>
          <Paper className={classes.paper}>
            <LoginPanel />
          </Paper>
        </Grid>
      </Grid>
    </div>
    );
}

Login.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styleSheet)(Login);

Bootstrap and other grid layout options are in conflict with this library. When I use <Grid> in other parts of a component(for example in drawer), horizontal scroll appears makes the UI ugly NavMenu and LoginPanel are some self-made components and they work and using them without doesn't cause horizontal scroll.

Qiniso
  • 2,587
  • 1
  • 24
  • 30
reza
  • 1,746
  • 6
  • 16
  • 32

10 Answers10

50

I had the same issue. I figured out a couple solutions but neither feel very elegant:

Disable spacing
Unfortunately this removes all padding from child grid items within the container:

<Grid container
  spacing={0}>

Manually fix the CSS
This is what I ended up doing:

<Grid container
  style={{
    margin: 0,
    width: '100%',
  }}>
bmaupin
  • 14,427
  • 5
  • 89
  • 94
  • The attribute `gutter` is no longer available. Now, they use `spacing`. – Jeremy Sullivan Aug 22 '17 at 14:14
  • 1
    @bmaupin do you think this is a bug? I feel like only the Grid items should have this behavior. – coolboyjules Sep 03 '17 at 18:12
  • @coolboyjules It does seem like a bug. In my own app I ended up ditching most of my use of Grid for vanilla flexbox since there was too much magic happening, otherwise I probably would've created an issue on github. – bmaupin Sep 05 '17 at 12:05
  • @bmaupin There is actually an issue on the repo. See [here](https://github.com/callemall/material-ui/issues/7466). I ended up going with your solution but the first solution in that answer is not bad either. – coolboyjules Sep 05 '17 at 14:25
  • Thank you very much! This overflow-x was so annoying – Idan Lottan Jul 13 '18 at 18:27
  • 7
    [This comment](https://github.com/mui-org/material-ui/issues/7466#issuecomment-509150645) solved it for me – Mohamad A. Hasan Nov 19 '19 at 12:38
  • issue resolved after adding overflow-x: hidden; on the parent, like inside Container have mui-datatable, so added overflowX: 'hidden' to container – Somnath Kadam Jun 23 '20 at 09:57
  • Not elegant, Check [my solution](https://stackoverflow.com/a/64806844/5826265). Worked well! – Somangshu Goswami Nov 12 '20 at 15:29
29

Copied the easy solution from comment:

added xs={12} to <Grid container>

<Grid container spacing={3} xs={12}>

credit to https://github.com/mui-org/material-ui/issues/7466#issuecomment-509150645

derek
  • 9,358
  • 11
  • 53
  • 94
5

This is caused by spacing. Sometimes we can still use spacing by limiting the Grid under a Container.

<Container maxWidth={false}>
  <Grid container spacing={6}>
    Omit
  </Grid>
</Container>
Lane
  • 4,682
  • 1
  • 36
  • 20
4

The best solution here is to wrap the grid in a container with the max width

<Container>
    <Grid container spacing={2}>
        <Grid item sm={6}></Grid>
        <Grid item sm={6}></Grid>
        <Grid item sm={6}></Grid>
        <Grid item sm={6}></Grid>
   </Grid>
</Container>

This way the overflow is taken care by the container and the grid always expands responsively into the parent. This by far is the most elegant solution I have found.

Tip: If your are using this library to create something like a dashboard then always have the parent for content area as <Container>, This way the overflow problem never occurs. Give it a shot. Worked well for me after struggling for some time and only finding non elegant solution everywhere. I must say this should be documented well in the react Material UI pages.

Somangshu Goswami
  • 1,098
  • 11
  • 27
2

This worked for me!

<Box style={{overflow: 'auto'}}>
<Grid>...</Grid>
</Box>

Aditya Patnaik
  • 1,490
  • 17
  • 27
1

The root issue is now fixed in the latest version of Material-UI (v5.0.0-alpha.30). See https://github.com/mui-org/material-ui/issues/7466#issuecomment-820736245.

Olivier Tassinari
  • 8,238
  • 4
  • 23
  • 23
0

I was facing the same issue. Remove spacing from the Grid container didn't solve it.

Solution:

Instead of setting with on the parent of the Grid container, setting maxWidth fixes the issues and assigns the desired width. For example, if we set maxWidth on the Box that is the parent of Grid container, then the Grid doesn't overflows horizontally.

We don't need to set width 100% on the Grid container because its purpose is to adapt to 100% width by default.

 <Box style={{ maxWidth: 600}}>
     <Grid container spacing={3}>
     ...
     </Grid>
 </Box>
Rusty
  • 4,138
  • 3
  • 37
  • 45
  • 2
    meh solution.... todays development require at most responsive design and that what grid is for... static size is not so helpful – adir abargil Jul 03 '20 at 12:11
0

well the idea i came up is

<Grid container>
 <Grid item xs={12} md={4}>
    <div style={{width:"97%",margin:"0 auto"}}>
     .....Your content
     </div>
  </Grid>
  <Grid item xs={12} md={4}>
    <div style={{width:"97%",margin:"0 auto"}}>
     .....Your content
     </div>
  </Grid>
  <Grid item xs={12} md={4}>
    <div style={{width:"97%",margin:"0 auto"}}>
     .....Your content
     </div>
  </Grid>
 </Grid>
Abdul Kàbéèr
  • 237
  • 1
  • 11
0

This is a known limitation of the grid with negative margins. https://material-ui.com/components/grid/#limitations.

  1. Don't use Grid spacing and manually configure your spacing.

  2. Add padding equal to at least half of the spacing to the parent, For Example:

    12 = 3 (spacing) * 8 (theme spacing pixels) / 2

<Box p={12}> {/* or style={{ padding: 12 }} */}
  <Grid ... spacing={3}>
    ..additional configuration
  </Grid>
</Box>

The downside to this solution is that it changes the look of the component.

  1. set overflow-x: hidden
<Box style={{overflowX: "hidden"}}>
  <Grid ... spacing={3}>
    ..additional configuration
  </Grid>
</Box>

The downside to this solution is that it (in my testing) causes issues with touchscreens trying to scroll vertically...

Nicholas_Jones
  • 253
  • 2
  • 8
0

For some reason none of the answers worked for me, I fixed it in Container like this:

<Container component="div" sx={{ maxWidth: "100vw" }}>
  <Grid container spacing={3}>
    ...
  </Grid>
</Container>
yashas123
  • 270
  • 5
  • 23