将 Mui 卡片操作对齐到右上角

huangapple go评论84阅读模式
英文:

Align Mui Card Action to the Top Right

问题

我试图将一个复选框与一个MUI卡片顶部右对齐,但一直在努力很长时间。这是我当前的样子将 Mui 卡片操作对齐到右上角。我想要按钮与右上角对齐(即在窗口缩小时,我仍希望复选框位于右上角)。

这是我目前用于创建这些卡片以及样式的代码:

<Card
    key={index}
    variant="outlined"
    className="course card"
>
    <CardHeader
        title={
            <Typography
                className="course title"
                sx={{
                    fontFamily: "Computer Modern Sans, sans-serif",
                    color: "black"
                }}
            >
                {course}
            </Typography>
        }
    ></CardHeader>
    <CardActions disableSpacing>
        <Checkbox size="small" color="primary">
        </Checkbox>
    </CardActions>
</Card>


.course.card{
    margin-right: 1%;
    min-height: 100px;
    max-width: 25%;
    min-width: 20%;
    flex: 1 1 20%;
    border-radius: 15px;
    display:flex;
    justify-content: center;
    align-items: center;
}
英文:

I am trying to align a checkbox within a MUI card to the top right but have been struggling for a long time do so. This is an image of what I currently have将 Mui 卡片操作对齐到右上角. I would like the button to be aligned to the top right corner (dynamically aka when the window shrinks I still want the checkbox to be on the top right).

This is the code I currently have to create these cards as well as the styling

&lt;Card
    key={index}
    variant=&quot;outlined&quot;
    className=&quot;course card&quot;
&gt;
    &lt;CardHeader
        title={
            &lt;Typography
                className=&quot;course title&quot;
                sx={{
                    fontFamily: &quot;Computer Modern Sans, sans-serif&quot;,
                    color: &quot;black&quot;
                }}
            &gt;
                {course}
            &lt;/Typography&gt;
        }
    &gt;&lt;/CardHeader&gt;
    &lt;CardActions disableSpacing&gt;
        &lt;Checkbox size=&quot;small&quot; color=&quot;primary&quot;&gt;
        &lt;/Checkbox&gt;
    &lt;/CardActions&gt;
&lt;/Card&gt;


.course.card{
    margin-right: 1%;
    min-height: 100px;
    max-width: 25%;
    min-width: 20%;
    flex: 1 1 20%;
    border-radius: 15px;
    display:flex;
    justify-content: center;
    align-items: center;
}

答案1

得分: 1

由于Card没有CardContent,可以尝试为CardHeader添加flex: 1来将CardActions推到右侧,并将CardActions样式化以将Checkbox放在右上角。

stackblitz上有一个精简示例。

CardActions中的padding可以进行编辑以进一步自定义Checkbox的位置。

如果稍后向组件添加CardContent,也许可以将其属性设置为flex: 1以保持布局。

英文:

Since the Card does not have CardContent, perhaps try add flex: 1 for CardHeader to push CardActions to the right, and style CardActions to place the Checkbox to the top right corner.

Minimized example on: stackblitz

The padding in CardActions can be edited to further customize the position of Checkbox.

&lt;Card key={index} variant=&quot;outlined&quot; className=&quot;course card&quot;&gt;
  &lt;CardHeader
    sx={{
      display: &quot;flex&quot;,
      flex: &quot;1&quot;,
    }}
    title={
      &lt;Typography
        className=&quot;course title&quot;
        sx={{
          fontFamily: &quot;Computer Modern Sans, sans-serif&quot;,
          color: &quot;black&quot;,
          display: &quot;flex&quot;,
          justifyContent: &quot;center&quot;,
        }}
      &gt;
        {course}
      &lt;/Typography&gt;
    }
  &gt;&lt;/CardHeader&gt;
  &lt;CardActions
    disableSpacing
    sx={{
      alignSelf: &quot;stretch&quot;,
      display: &quot;flex&quot;,
      justifyContent: &quot;flex-end&quot;,
      alignItems: &quot;flex-start&quot;,
      // &#128071; Edit padding to further adjust position
      p: 0,
    }}
  &gt;
    &lt;Checkbox size=&quot;small&quot; color=&quot;primary&quot;&gt;&lt;/Checkbox&gt;
  &lt;/CardActions&gt;
&lt;/Card&gt;

If CardContent is added to the component later, perhaps it could have the property flex: 1 instead to maintain the layout.

huangapple
  • 本文由 发表于 2023年1月9日 09:32:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定