I'm trying to generate a grid that is three deep. Example below:
I want the black box to stretch the length of the screen, and the three boxes to be aligned on the same row. I have put the three boxes in another grid that will go to a max-width of 1200px. this is so it looks neat on large screens. I want the three boxes to stack on mobile.
below is my code
const Landing = (props) => {
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
},
blackBox:{
color:'white',
backgroundColor:'black',
width:'100%',
},
white:{
color:'white',
}
}));
const classes = useStyles();
return (
<div className={classes.root}>
<Grid container className={classes.blackBox}>
<Container maxWidth="sm">
<Grid cols={1} item xs={12} sm={4} className={classes.white}>
<Typography>Text </Typography>
</Grid>
<Grid cols={1} item xs={12} sm={4} className={classes.white}>
<Typography>Text </Typography>
</Grid>
<Grid cols={1} item xs={12} sm={4} className={classes.white}>
<Typography>Text</Typography>
</Grid>
</Container>
</Grid>
</div>
);
}
export default Landing;
The issue is when I add the element that only allow the three boxed to go max 1200 they all stack. When I remove it they don't stack but stretch the full with of the screen.
can anyone point me in the right direction?
You should put the Container outside the Grid
<Grid container className={classes.blackBox}>
<Container maxWidth="sm">
<Grid container>
<Grid cols={1} item xs={12} sm={4} className={classes.white}>
<Typography>Text </Typography>
</Grid>
<Grid cols={1} item xs={12} sm={4} className={classes.white}>
<Typography>Text </Typography>
</Grid>
<Grid cols={1} item xs={12} sm={4} className={classes.white}>
<Typography>Text</Typography>
</Grid>
</Grid>
</Container>
</Grid>
Related
I am trying to use MUI Card in Grid layout
<Grid container spacing={5} alignContent="center" alignItems="center" justifyContent="center" columnSpacing={{ xs: 1, sm: 2, md: 3 }}>
<Grid item xs={4} md={6} alignItems="center">
<Card />
</Grid>
<Grid item xs={4} md={6}>
<Card />
</Grid>
<Grid item xs={4} md={6}>
<Card />
</Grid>
<Grid item xs={4} md={6}>
<Card />
</Grid>
</Grid>
But the problem is its not center aligns with in each grid item. I have added border for each item
What I did wrong?
Also as you can see the grid overlap on the title above
So i was following a tutorial, where the items would change depending on the size of the screen. And would take up the whole width if defined the following way:
class App extends Component {
render() {
return (
<div>
<Grid container>
<Grid>
<Paper item xs={12} sm={6} md={3}>1</Paper>
</Grid>
<Grid>
<Paper item xs={12} sm={6} md={3}>2</Paper>
</Grid>
<Grid>
<Paper item xs={12} sm={6} md={3}>3</Paper>
</Grid>
<Grid>
<Paper item xs={12} sm={6} md={3}>4</Paper>
</Grid>
</Grid>
</div>
);
}}
Well for me, no matter the width of my screen it always looks like this:
I see no reason for it not to work, but somehow the elements don't stretch to take up the complete width of the screen.
The mistake you are doing here is that you are adding the Grid properties (item xs={12} sm={6} md={3}) in the Paper component instead of the Grid item.
Here is the right code:
return (
<div>
<Grid container>
<Grid item xs={12} sm={6} md={3}>
<Paper>1</Paper>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<Paper>2</Paper>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<Paper>3</Paper>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<Paper>4</Paper>
</Grid>
</Grid>
</div>
);
Here is a working codesandbox with your example
I'm attempting to have a grid looking like this for larger displays:
and this for smaller sizes:
Currently the skeleton of my grid is succinctly like this:
<Fragment>
<Grid container spacing={3}>
<Grid item xs={12} md={6}>
<img src={img1} />
</Grid>
<Grid item xs={12} md={6}>
<Paper>Text Content 1...</Paper>
</Grid>
</Grid>
<Grid container spacing={3}>
<Grid item xs={12} md={6}>
<Paper>Text Content 2...</Paper>
</Grid>
<Grid item xs={12} md={6}>
<img src={img2} />
</Grid>
</Grid>
</Fragment>
How can I do such as in mobile view (width 12) the first Grid item from the second Grid container (Text content 2) displays after (below) the image?
Use like this:
<Grid container>
<Grid spacing={3} container item xs={12} md={12}>
<Grid item xs={12} md={6} className={classes.item}>
<img
width="50px"
src="https://cdn3.vectorstock.com/i/1000x1000/37/37/simple-cartoon-a-cute-bear-vector-18823737.jpg"
alt="abc"
/>
</Grid>
<Grid container item xs={12} md={6} className={classes.item}>
<Paper>Text Content 1...</Paper>
</Grid>
</Grid>
<Grid container item spacing={3} xs={12} md={12} className="flexgrid">
<Grid container item xs={12} md={6} className={classes.item}>
<Paper>Text Content 2...</Paper>
</Grid>
<Grid container item xs={12} md={6} className={classes.item}>
<img
width="50px"
src="https://cdn3.vectorstock.com/i/1000x1000/37/37/simple-cartoon-a-cute-bear-vector-18823737.jpg"
alt="abd"
/>
</Grid>
</Grid>
</Grid>
SOURCE
PREVIEW
for re-order added:
<Grid container item spacing={3} xs={12} md={12} className="flexgrid">
and css :
#media screen and (max-width: 720px) {
.flexgrid{
flex-direction: column-reverse
}
}
I have the following code in reactjs using material ui theme
const useStyles = makeStyles((theme) => ({
root2: {
maxWidth: 345,
flexGrow: 1
},
paper2: {
padding: theme.spacing(1),
color: theme.palette.text.secondary,
}
})
<div className={classes.root2}>
<Grid container spacing={3}>
<Grid item xs={12}>
<Paper className={classes.paper2}>
<Grid container alignItems="center" spacing={3}>
<Grid item>
<h3>Instructions</h3>
</Grid>
<Grid item>
<IconButton>
<ExpandMoreIcon />
</IconButton>
</Grid>
</Grid>
</Paper>
</Grid>
</Grid>
</div>
What i want is
I saw an example at https://material-ui.com/system/flexbox/#flex-grow
which uses the code:
import React from 'react';
import Box from '#material-ui/core/Box';
export default function FlexGrow() {
return (
<div style={{ width: '100%' }}>
<Box display="flex" p={1} bgcolor="background.paper">
<Box p={1} flexGrow={1} bgcolor="grey.300">
Item 1
</Box>
<Box p={1} bgcolor="grey.300">
Item 2
</Box>
<Box p={1} bgcolor="grey.300">
Item 3
</Box>
</Box>
</div>
);
}
I am new to material ui. Here the example uses Box instead of Grid.
So which is the best way to handle this
I am used to bootstrap 4: IN bootstrap4 i can do this same thing using
https://getbootstrap.com/docs/4.4/utilities/flex/#justify-content
<div class="d-flex justify-content-between">...</div>
How to do something like this in material UI
Adding justify="space-between" to Grid container component will solve your issue.
Please refer this.
<Grid container spacing={3}>
<Grid item xs={12}>
<Paper className={classes.paper2}>
<Grid container alignItems="center" justify="space-between">
<Grid item>
<h3>Instructions</h3>
</Grid>
<Grid item>
<IconButton>
<ExpandMoreIcon />
</IconButton>
</Grid>
</Grid>
</Paper>
</Grid>
</Grid>
How do I stack a CircularProgress component on top of a Typography component (both of which are in a Backdrop component)?
Currently, the progress component is on the left and the text is right next to the progress bar. I've attempted to use grid items/containers but no luck so far.
<Backdrop open={loading} style={{zIndex: 20, opacity: 0.8, backgroundColor: '#FFFF', color: '#0000'}}>
<Grid container alignItems='center' justify='center' alignContent='center'>
<Grid item sm={12} md={12} lg={12} align='center'>
<PurpleProgress/>
</Grid>
<Grid item sm={12} md={12} lg={12} align='center'>
<Typography variant="body2" style={{color: '#000000'}}><b>{message}</b></Typography>
</Grid>
</Grid>
</Backdrop>
You are probably having this issue on small screens because you do not have the xs prop set to take up the entire grid. To solve this issue, if you want the stacking to be consistent across all screen sizes, you can just use xs={12}
<Grid container alignItems='center' justify='center' alignContent='center'>
<Grid item xs={12} align='center'>
<PurpleProgress/>
</Grid>
<Grid item xs={12} align='center'>
<Typography variant="body2" style={{color: '#000000'}}><b>{message}</b></Typography>
</Grid>
</Grid>