I am using Material UI and have a Grid container which contains three Grid items. The furthest left item should have a fixed width of 240px and the contents should be left aligned. The middle Grid item should be justified to the left and can be any width - the content contains some buttons (which I have left out of the code below) and should be left aligned. The right Grid item should be right aligned and ideally be about 240px or a proportion of the screen (e.g. xs="3"). It must be mobile responsive. Here's a picture:
I have tried using justify="flex-end" and justify="space-between" but the trouble is that it messes up the layout. The contents end up stacking on top of each other. Can anyone remend a good approach?
import React, { useState } from 'react';
import { makeStyles } from '@material-ui/core/styles';
const useStyles = makeStyles(theme => ({
root: {
minHeight: 40,
alignItems: 'center',
padding: theme.spacing(0, 2),
},
fixedWidthContainer: {
width: '240px',
},
titleSpacing: {
marginRight: theme.spacing(2),
},
}));
const AdminBar = () => {
const classes = useStyles();
return (
<Grid container className={classes.root}>
<Grid item className={classes.fixedWidthContainer}>
<Typography variant="body2">
Title: MyTitle
</Typography>
</Grid>
<Grid item>
<Typography
className={classes.titleSpacing}
variant="body2"
ponent="span"
>
Some stuff to go on the left
</Typography>
</Grid>
<Grid item className={classes.fixedWidthContainer}>
Some stuff to go on the right
</Grid>
</Grid>
);
};
export default AdminBar;
Many thanks,
Katie
I am using Material UI and have a Grid container which contains three Grid items. The furthest left item should have a fixed width of 240px and the contents should be left aligned. The middle Grid item should be justified to the left and can be any width - the content contains some buttons (which I have left out of the code below) and should be left aligned. The right Grid item should be right aligned and ideally be about 240px or a proportion of the screen (e.g. xs="3"). It must be mobile responsive. Here's a picture:
I have tried using justify="flex-end" and justify="space-between" but the trouble is that it messes up the layout. The contents end up stacking on top of each other. Can anyone remend a good approach?
import React, { useState } from 'react';
import { makeStyles } from '@material-ui/core/styles';
const useStyles = makeStyles(theme => ({
root: {
minHeight: 40,
alignItems: 'center',
padding: theme.spacing(0, 2),
},
fixedWidthContainer: {
width: '240px',
},
titleSpacing: {
marginRight: theme.spacing(2),
},
}));
const AdminBar = () => {
const classes = useStyles();
return (
<Grid container className={classes.root}>
<Grid item className={classes.fixedWidthContainer}>
<Typography variant="body2">
Title: MyTitle
</Typography>
</Grid>
<Grid item>
<Typography
className={classes.titleSpacing}
variant="body2"
ponent="span"
>
Some stuff to go on the left
</Typography>
</Grid>
<Grid item className={classes.fixedWidthContainer}>
Some stuff to go on the right
</Grid>
</Grid>
);
};
export default AdminBar;
Many thanks,
Katie
Share edited Apr 13, 2023 at 15:45 Rajiv 3,7922 gold badges18 silver badges32 bronze badges asked Jan 5, 2021 at 12:54 Katie7Katie7 8892 gold badges22 silver badges39 bronze badges1 Answer
Reset to default 3provide the size ration to the Grid
items as you want. like for the side items provide xs={3}
and for center item set flex-grow:1
.
<Grid container className={classes.root}>
<Grid item xs={3} className={classes.fixedWidthContainer}>
<Typography variant="body2">Title: MyTitle</Typography>
</Grid>
<Grid item style={{ flexGrow: "1" }}>
<Typography
className={classes.titleSpacing}
variant="body2"
ponent="span">
Some stuff to go on the left
</Typography>
</Grid>
<Grid xs={3} item className={classes.fixedWidthContainer}>
Some stuff to go on the right
</Grid>
</Grid>
Sandbox link:- https://codesandbox.io/s/xenodochial-flower-ge65z
Personal Opinion:- It'd be better to use just div
and then style them accordingly in this case, as you've fixed-width requirement here. I've added this method in the sandbox
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744924362a4601338.html
评论列表(0条)