【问题标题】:Same Height Cards in Material UI材质 UI 中的相同高度卡片
【发布时间】:2019-09-13 09:48:54
【问题描述】:

尝试使用 3 张水平卡片,但高度相同,并且反应灵敏。

喜欢

卡 A |卡 B |卡C

【问题讨论】:

    标签: css material-ui


    【解决方案1】:

    您可以使用 Grid 组件来实现:

    <Grid container alignItems="stretch">
    
      <Grid item component={Card} xs>
        <CardContent>
           // Card A content
        </CardContent>
        <CardActions>
          // Card A actions
        </CardActions>
      </Grid>
    
      <Grid item component={Card} xs>
        <CardContent>
          // Card B content
        </CardContent>
        <CardActions>
          // Card B actions
        </CardActions>
      </Grid>
    
      <Grid item component={Card} xs>
        <CardContent>
          // Card B content
        </CardContent>
        <CardActions>
          // Card B actions
        </CardActions>
      </Grid>
    </Grid>
    

    alignItems="stretch"(实际上不需要指定,因为stretch是默认的)当flex方向为row(也是默认方向)时会产生拉伸每个item高度的效果.您可以查看此答案以获取更多详细信息:https://stackoverflow.com/a/46956430/253693

    每个Grid item 上的xs 属性利用auto-layout,指示每张卡平均共享可用宽度。

    还有一些您可以解决的清理项目,即使用withStyles HOC 为每个固定间距的 Card 组件应用一个类,并确保 CardActions 保持在卡片的底部,而不管CardContent 的高度:

    const styles = {
      card: {
        // Provide some spacing between cards
        margin: 16,
    
        // Use flex layout with column direction for components in the card
        // (CardContent and CardActions)
        display: "flex",
        flexDirection: "column",
    
        // Justify the content so that CardContent will always be at the top of the card,
        // and CardActions will be at the bottom
        justifyContent: "space-between"
      }
    };
    

    然后您可以像这样将这些样式应用于每张卡片:

    <Grid item component={Card} xs className={classes.card}>
    

    这是一个将所有内容组合在一起的工作示例:https://codesandbox.io/embed/r9y95vr5n

    【讨论】:

    • 我想使用xs={whatever} 控制每个网格项的宽度。但是,当我这样做时,由于边距,项目不会分布在整行上(例如,xs={6} 的 2 个网格项目将显示在两个不同的行上)。如果我去掉边距,卡片就会相互接触。通常我使用网格容器中的spacing 属性来管理它,但它不会改变任何东西。您将如何调整您的答案以适应手动布局?
    • @7hibault,我遇到了同样的问题。不幸的是,正如您所注意到的,用&lt;Card&gt; 覆盖&lt;Grid item /&gt; 的渲染组件会弄乱间距。请参阅my answer below,了解如何实现卡片的垂直对齐而不会产生不必要的副作用。
    【解决方案2】:

    按照上面的答案中的建议,用&lt;Card&gt; 覆盖&lt;Grid item /&gt; 的渲染组件会弄乱间距(&lt;Grid container /&gt;spacing 属性停止工作)。请参阅下面我的解释,了解如何实现卡片的垂直对齐而不会产生不必要的副作用。

    &lt;Grid item /&gt; 上设置display: 'flex' 应该足以对齐所有&lt;Card /&gt; 元素的高度。

    但是,要获得很好地垂直拉伸 &lt;CardContent&gt;&lt;CardActions&gt; 的额外效果,还要在 &lt;Card /&gt; 元素上设置 display: 'flex', justifyContent: 'space-between', flexDirection: 'column'

    <Grid container alignItems="stretch">
      <Grid item style={{display: 'flex'}}>
        <Card style={{display: 'flex', justifyContent: 'space-between', flexDirection: 'column'}}>
          // contents of your Card
        <Card/>
      </Grid>
      <Grid item style={{display: 'flex'}}>
        <Card style={{display: 'flex', justifyContent: 'space-between', flexDirection: 'column'}}>
          // contents of your Card
        <Card/>
      </Grid>
    </Grid>
    

    【讨论】:

    • 看起来这种方法也破坏了xs={6}Grid item上的类似方法
    • 虽然width: 100% 上的Card 解决了这个问题。
    • 这与@StaffordWilliams 的评论一样有效!
    • 这在高度上按预期工作。但是,我注意到卡片的宽度不再是完整的网格宽度。知道为什么吗?
    【解决方案3】:

    我认为这是一种更简单的方法:

    https://github.com/creativetimofficial/ct-material-dashboard-pro-react/issues/45#issuecomment-442885173

    import React from "react"
    import { Card, CardBody, CardFooter } from "components"
    
    import withStyles from "@material-ui/core/styles/withStyles"
    
    const styles = {
    fullHeightCard: {
        height: "100%",
        },
    }
    
    const Item = (props) => {
        const {classes} = props
    
        // 1-5 paragraphs to create card height variance
        let paragraphs = 1 + Math.floor(Math.random() * Math.floor(5))
    
        return (
            <Card className={classes.fullHeightCard}>
                <CardBody>
                    {Array(paragraphs).fill().map((_,i) => (
                        <p>{i+1}</p>
                    ))}
                </CardBody>
                <CardFooter>
                {'Footer content'}
                </CardFooter>
            </Card>
        )
    }
    
    export default withStyles(styles)(Item)
    

    【讨论】:

      【解决方案4】:

      您必须将以下属性添加到您的Card

      .MuiCard-root.same-height {
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
      }
      

      它会在CardContentCardActions之间添加必要的空格

      这里回答了同样的问题:How to make Material-UI CardActions always stick to the bottom of parent

      【讨论】:

        【解决方案5】:

        我使用了一个更简单的解决方案:在父网格上设置 display: grid

        <Grid container style={{ display: 'grid' }}>
          <Grid item>
            <Card>
              // contents of your Card
            <Card />
          </Grid>
          <Grid item>
            <Card>
              // contents of your Card
            <Card />
          </Grid>
        </Grid>
        

        【讨论】:

          猜你喜欢
          • 2017-11-24
          • 1970-01-01
          • 2021-06-02
          • 1970-01-01
          • 2017-01-04
          • 2018-04-16
          • 1970-01-01
          • 2021-09-26
          • 2020-06-17
          相关资源
          最近更新 更多