【问题标题】:Using Material-UI's Grid component, how do I get one cell to take up the remaining horizontal width?使用 Material-UI Grid 组件,如何让一个单元格占据剩余的水平宽度?
【发布时间】:2020-08-17 11:57:06
【问题描述】:

我正在尝试在我的 React 16.13.0 应用程序中使用 material-ui 的 Grid 组件。我想要一行包含三个项目。前两项应该只占用他们需要的空间(如果可能的话,我不想硬编码像素值)。我希望第三个项目占据剩余的水平空间并最远地浮动到 rigth (虽然我发现 React 不喜欢“float: right”作为一种样式)。我有这个

const styles = (theme) => ({
  root: {
    textAlign: "left",
    margin: theme.spacing(2),
    paddingBottom: theme.spacing(1),
    color: theme.color.secondary,
  },
  cardHeader: {
    paddingBottom: theme.spacing(0),
  },
  cardContent: {
    width: "100%",
    paddingBottom: theme.spacing(1),
  },
  rowBody: {
    width: "100%",
    flexWrap: "nowrap",
    alignItems: "center",
  },
});
...
      <CardContent className={classes.cardContent}>
        <Grid container className={classes.rowBody}>
          <Grid item>
            <img height="20" src={require('../../img/apple.svg')} alt="" />
          </Grid>
          <Grid item>
            {title}
          </Grid>
          <Grid item>
            <InfoIcon />
          </Grid>
        </Grid>

但不幸的是,这会将所有内容都捆绑在一起

如何调整样式以达到我想要的效果?

编辑:这是根据@Mohsen007给出的答案显示的内容...

【问题讨论】:

    标签: css reactjs grid material-ui


    【解决方案1】:

    按照下面的代码:

    rowBody: {
        width: "100%",
        flexWrap: "nowrap",
        alignItems: "center",
        display: "flex",
    },
    
    <Grid container className={classes.rowBody}>
         <Grid item>
             <img height="20" src={require('../../img/apple.svg')} alt="" />
         </Grid>
         <Grid item style={{flex: 1,}}>
             {title}
         </Grid>
         <Grid item>
             <InfoIcon />
         </Grid>
    </Grid>
    

    【讨论】:

    • 谢谢你,虽然这不是我想要的(在我的问题中包括一个屏幕截图和你的建议的结果)。现在一切都居中,但我更喜欢将行左对齐,并且只有第三个单元格(带有信息图标的单元格)占用尽可能多的空间。最后,我希望信息图标在其单元格中右对齐。
    • 哦,抱歉...好的,这是您希望将信息图标粘贴在屏幕右侧的内容吗?
    • 我想你忘了将“flex: 1”添加到第二个网格,不是吗??
    猜你喜欢
    • 2016-01-08
    • 1970-01-01
    • 2010-11-06
    • 2011-11-03
    • 1970-01-01
    • 2019-03-18
    • 2021-10-24
    • 2017-02-12
    • 2011-11-04
    相关资源
    最近更新 更多