材质用户界面-反应-将网格项放置在列中

时间:2020-03-11 23:58:36

标签: css reactjs material-ui

在使用Material UI时,我在定位网格项时需要一些帮助。

Codesandbox是我目前的位置(大致)。

我的问题很简单-如何获得2和3移到1的右边?我可以这样做,但是左侧和下方都有所有这些奇怪的空间,我不知道该如何处理。

那是我追求的最终结果: enter image description here

谢谢。

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

const useStyles = makeStyles(theme => ({
  container: {
    // display: "grid",
    gridTemplateColumns: "repeat(12, 1fr)",
    gridGap: theme.spacing(3)
  },
  paper: {
    padding: theme.spacing(1),
    textAlign: "center",
    color: theme.palette.text.secondary,
    whiteSpace: "nowrap",
    marginBottom: theme.spacing(1)
  },
  w: {
    height: "100px"
  },
  divider: {
    margin: theme.spacing(2, 0)
  }
}));

export default function CSSGrid() {
  const classes = useStyles();

  return (
    <div>
      <Grid container spacing={3}>
        <Grid container direction="column">
          <Grid item xs={8}>
            <Paper className={classes.paper + " " + classes.w}>xs=8</Paper>
          </Grid>
        </Grid>
        <Grid
          container
          style={{ display: "table", height: "100%" }}
          direction="row"
        >
          <Grid item xs={4}>
            <Paper className={classes.paper}>xs=4</Paper>
          </Grid>
          <Grid item xs={4}>
            <Paper className={classes.paper}>xs=4</Paper>
          </Grid>
        </Grid>
      </Grid>
      <Divider className={classes.divider} />
    </div>
  );
}

1 个答案:

答案 0 :(得分:0)

代码中的一些注意点

  • 为一个网格字段结构使用一个容器
  • 删除方向设置direction="column"
  • xs的总和必须小于或等于12,如果您想将它们设置为一行。

请仔细检查material-ui grid文档以了解更多信息

<Grid container spacing={3}>
  <Grid item xs={8}>
    <Paper className={classes.paper + " " + classes.w}>xs=8</Paper>
  </Grid>
  <Grid item xs={2}>
    <Paper className={classes.paper}>xs=2</Paper>
  </Grid>
  <Grid item xs={2}>
    <Paper className={classes.paper}>xs=2</Paper>
  </Grid>
</Grid>
<Divider className={classes.divider} />

enter image description here


更新:

<Grid container spacing={3}>
  <Grid item xs={8}>
    <Paper className={classes.paper + " " + classes.w}>xs=8</Paper>
  </Grid>
  <Grid item xs={4}>
    <Grid container>
      <Grid item xs={12}>
        <Paper className={classes.paper}>xs=4</Paper>
      </Grid>
      <Grid item xs={12}>
        <Paper className={classes.paper}>xs=4</Paper>
      </Grid>
    </Grid>
  </Grid>
</Grid>
<Divider className={classes.divider} />

enter image description here

在线尝试:

Edit Material demo