【问题标题】:React Material UI Nested Grid - Not workingReact Material UI 嵌套网格 - 不工作
【发布时间】:2019-11-06 21:55:37
【问题描述】:

我正在尝试生成一个三深的网格。下面的例子:

我想让黑盒子拉伸屏幕的长度,并且三个盒子在同一行对齐。我已将三个框放在另一个网格中,最大宽度为 1200 像素。这样在大屏幕上看起来很整洁。我希望这三个盒子在移动设备上堆叠。

下面是我的代码

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;   

问题是当我添加只允许三个装箱到最大 1200 的元素时,它们都可以堆叠。当我删除它时,它们不会堆叠,而是会拉伸整个屏幕。

谁能指出我正确的方向?

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    你应该把Container放在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>
    

    【讨论】:

    • 然后在这个 Component 周围放一个不受 maxWidth 限制的 Container 并将黑条和上面的小容器一起放在这个容器中。 @ChrisEvans
    • 从material-ui material-ui.com/getting-started/templates的部分模板源代码中获取灵感
    • 这对@Julian 有效,就像电影《盗梦空间》,网格中的网格中的网格中的网格中的网格。感谢您的帮助!
    • 哈哈,你说得对。有一些初始风格。分成不同的组件,总是以Container 开头,这样就不会那么混乱了。很高兴我们能解决这个问题:)
    • @ChrisEvans 考虑将问题标记为已解决
    猜你喜欢
    • 2019-07-10
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    相关资源
    最近更新 更多