材质用户界面中的高度相同的卡片

Pan*_*iou 3 material-ui

尝试拥有3张水平的卡片,但高度相同且反应灵敏。

喜欢

卡A | 卡B | 卡C

Ale*_*sha 18

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

设置display: 'flex'on<Grid item />应该足以对齐所有<Card />元素的高度。

然而,为了更好地拉伸<CardContent><CardActions>垂直的额外效果,也设置display: 'flex', justifyContent: 'space-between', flexDirection: 'column'<Card />元素上。

<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>
Run Code Online (Sandbox Code Playgroud)

  • 尽管“Card”上的“width: 100%”解决了这个问题。 (4认同)

Rod*_*osa 8

我认为更简单的方法是:

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)
Run Code Online (Sandbox Code Playgroud)


And*_*ler 7

您可以使用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>
Run Code Online (Sandbox Code Playgroud)

alignItems="stretch"(实际上不需要指定,因为默认是拉伸),而在flex方向row(也是默认方向)时,将具有拉伸每个项目的高度的效果。您可以看到此答案以获取更多详细信息:https : //stackoverflow.com/a/46956430/253693

xs各属性Grid item花费的优点自动布局,指示每张卡平均分配可用的宽度。

您还可以解决其他一些清理问题,即使用withStyles HOC对每个Card组件应用一个类来固定间距,并确保无论CardContent的高度如何,CardActions都位于卡片的底部。 :

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"
  }
};
Run Code Online (Sandbox Code Playgroud)

然后,您可以将这些样式应用于每张卡片,如下所示:

<Grid item component={Card} xs className={classes.card}>
Run Code Online (Sandbox Code Playgroud)

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

  • 我想使用“xs={whatever}”控制每个网格项的宽度。然而,当我这样做时,由于边距的原因,项目不会分布在整个行上(例如,带有“xs={6}”的 2 个网格项目将显示在两个不同的行上)。如果我去掉边距,卡片就会相互接触。通常我使用网格容器中的“spacing”属性来管理它,但它不会改变任何东西。您将如何调整您的答案以适应手动布局? (2认同)

Dar*_*gel 6

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

.MuiCard-root.same-height {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
Run Code Online (Sandbox Code Playgroud)

它将在CardContent和 之间添加必要的空间CardActions

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