javascript 将材质UI< Grid>用于两列布局时出现问题

llmtgqce  于 2023-04-19  发布在  Java
关注(0)|答案(2)|浏览(143)

我试图实现一个两列的布局,两列的高度相等,每列占屏幕的一半。图片更好地解释了这一点,here is one
不工作的代码如下:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import CssBaseline from "@material-ui/core/CssBaseline";
import Grid from "@material-ui/core/Grid";
import Paper from "@material-ui/core/Paper";

const useStyles = makeStyles(theme => ({
  root: {
    flexGrow: 1,
    alignItems: "stretch"
  },
  column: {
    flexDirection: "column"
  },
  paper: {
    padding: theme.spacing(2),
    textAlign: "center",
    color: theme.palette.text.secondary
  }
}));

export default props => {
  const classes = useStyles();
  return (
    <>
      <CssBaseline />
      <Grid container className={classes.root}>
        {/* COLUMN ONE */}
        <Grid container item className={classes.column}>
          <Grid item xs={6}>
            <Paper className={classes.paper}>1: xs=6</Paper>
          </Grid>
          <Grid item xs={6}>
            <Paper className={classes.paper}>1: xs=6</Paper>
          </Grid>
          <Grid container item>
            <Grid item xs={3}>
              <Paper className={classes.paper}>1: xs=3 left</Paper>
            </Grid>
            <Grid item xs={3}>
              <Paper className={classes.paper}>1: xs=3 right</Paper>
            </Grid>
          </Grid>
        </Grid>
        {/* COLUMN TWO */}
        <Grid container item className={classes.column}>
          <Grid item xs={6}>
            <Paper className={classes.paper}>2: xs=6</Paper>
          </Grid>
          <Grid item xs={6}>
            <Paper className={classes.paper}>2: xs=6</Paper>
          </Grid>
        </Grid>
      </Grid>
    </>
  );
};

强制代码和框是here
有人能解释一下我做错了什么吗?

myss37ts

myss37ts1#

Material UI在布局嵌套网格容器here时有一个错误。解决方法-由@londonoliver发现-是在网格项目中嵌套容器:

<Grid container direction="row">
  <Grid item>
    <Grid container direction="column">
      <Grid item>1</Grid>
      <Grid item>2</Grid>
    </Grid>
  </Grid>
  <Grid item>
    <Grid container direction="column">
      <Grid item>3</Grid>
      <Grid item>4</Grid>
    </Grid>
  </Grid>
</Grid>
hvvq6cgz

hvvq6cgz2#

我遇到了一个类似的问题,解决方法是在一个Grid容器中嵌套两个Grid,如下所示:

<Grid container spacing={1}>
  <Grid container item spacing={2}>
    <Grid item xs={6}>1</Grid>
    <Grid item xs={6}>2</Grid>
  </Grid>
  <Grid container item spacing={2}>
    <Grid item xs={4}>3</Grid>
    <Grid item xs={8}>4</Grid>
  </Grid>
</Grid>

相关问题