Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full Width Material-UI Grid not working as it should

I am trying to understand Material-UI@next grid layout system.

My goal is to have two papers that expand through the whole width screen and break whenever the screen gets smaller to just one paper. The documentation has the following code snippet:

  <Container>     <Grid container spacing={24}>       <Grid item xs={12}>         <Paper>xs=12</Paper>       </Grid>       <Grid item xs={12} sm={6}>         <Paper>xs=12 sm=6</Paper>       </Grid>       <Grid item xs={12} sm={6}>         <Paper>xs=12 sm=6</Paper>       </Grid>       <Grid item xs={6} sm={3}>         <Paper>xs=6 sm=3</Paper>       </Grid>       <Grid item xs={6} sm={3}>         <Paper>xs=6 sm=3</Paper>       </Grid>       <Grid item xs={6} sm={3}>         <Paper>xs=6 sm=3</Paper>       </Grid>       <Grid item xs={6} sm={3}>         <Paper>xs=6 sm=3</Paper>       </Grid>     </Grid>   </Container> 

This results in the following: enter image description here

I then modified the code to try to achieve my goal of just two papers, as this:

  <Container>     <Grid container spacing={24}>       <Grid item xs={12} sm={6}>         <Paper>xs=12 sm=6</Paper>       </Grid>       <Grid item xs={12} sm={6}>         <Paper>xs=12 sm=6</Paper>       </Grid>     </Grid>   </Container> 

But as you can see, this results into two papers which are not taking the whole screen: enter image description here

Can someone point me to a working example snippet that allows me to have two elements that take the full width?

like image 951
James Avatar asked Apr 12 '18 12:04

James


People also ask

How do I fix the size of the grid?

By default, a grid item cannot be smaller than the size of its content. Grid items have an initial size of min-width: auto and min-height: auto . You can override this behavior by setting grid items to min-width: 0 , min-height: 0 or overflow with any value other than visible .

What is Xs and MD in material UI grid?

Each breakpoint (a key) matches with a fixed screen width (a value): xs, extra-small: 0px. sm, small: 600px. md, medium: 900px.


1 Answers

I suspect the Container component is causing you problems. Since you haven't linked its implementation, see below for a working example of what you want.

Since Material uses flexbox they make use of property flexGrow

The flex-grow CSS property specifies the flex grow factor of a flex item. It specifies what amount of space inside the flex container the item should take up. The flex grow factor of a flex item is relative to the size of the other children in the flex-container.

This is the property that governs the growth of elements in the grid.

import React from 'react'; import PropTypes from 'prop-types'; import { withStyles } from 'material-ui/styles'; import Paper from 'material-ui/Paper'; import Grid from 'material-ui/Grid';  const styles = theme => ({   root: {     flexGrow: 1,   },   paper: {     padding: theme.spacing.unit * 2,     textAlign: 'center',     color: theme.palette.text.secondary,   }, });  function CenteredGrid(props) {   const { classes } = props;    return (     <div className={classes.root}>         <Grid container spacing={24}>           <Grid item xs={12} sm={6}>             <Paper>xs=12 sm=6</Paper>           </Grid>           <Grid item xs={12} sm={6}>             <Paper>xs=12 sm=6</Paper>           </Grid>         </Grid>     </div>   ); }  CenteredGrid.propTypes = {   classes: PropTypes.object.isRequired, };  export default withStyles(styles)(CenteredGrid); 
like image 58
Paul McLoughlin Avatar answered Sep 22 '22 07:09

Paul McLoughlin