【问题标题】:How to create corner ribbon for Material UI Card? [closed]如何为 Material UI Card 创建角落功能区? [关闭]
【发布时间】:2020-06-08 05:29:25
【问题描述】:

如何在Material-UI 卡片上添加角丝带?对于样式,我使用来自Material UImakeStyles

【问题讨论】:

  • 欢迎来到 Stack Overflow!请通读help center,尤其是How do I ask a good question?,你最好的选择是做你的研究,search 以获取有关 SO 的相关主题,然后试一试。如果您在进行更多研究和搜索后卡住并且无法摆脱卡住,请发布您的尝试minimal reproducible example,并具体说明您卡在哪里。人们会很乐意提供帮助。

标签: javascript css reactjs material-ui


【解决方案1】:

您可以在您的 Material Ui 卡上添加 4 个色带,它们是(左上角、右上角、左下角和右下角)。

  1. 只需使用 Material ui 设计制作卡片。

  2. 并将卡片的位置设置为相对于父容器的位置,并在 div 中设置,并将其位置设置为绝对。

  3. 下面看代码你也可以添加css伪元素来调整功能区边框。

    import React from 'react';
    import { makeStyles } from '@material-ui/core/styles';
    import Card from '@material-ui/core/Card';
    import CardActionArea from '@material-ui/core/CardActionArea';
    import CardActions from '@material-ui/core/CardActions';
    import CardContent from '@material-ui/core/CardContent';
    import CardMedia from '@material-ui/core/CardMedia';
    import Button from '@material-ui/core/Button';
    import Typography from '@material-ui/core/Typography';
    
    root: {
        margin: '0 auto',
        width: 500,
    },
    card: {
        maxWidth: 360,
        marginTop: 40,
        position: "relative",
    },
    ribbon: {
        backgroundColor: 'skyblue',
        position: "absolute",
        color: 'white',
        width: 150,
        zIndex: 3,
        textAlign: 'center',
        textTransform: 'uppercase',
        padding: 5,
        font: 'Lato',
        '&::before': {
            position: "absolute",
            zIndex: -1,
            content: '',
            display: 'block',
            border: '5px solid #2980b9',
        },
        '&::after': {
            position: "absolute",
            zIndex: -1,
            content: '',
            display: 'block',
            border: '5px solid #2980b9',
        },
        transform: 'rotate(-45deg)',
        top: 60,
        marginLeft: -40,
    },
    span: {
    
    }
    
    export default function RibbonMaterialCard() {
      const classes = useStyles();
    
      return (
        <div className={classes.root}>
           <div className={classes.ribbon}><span className={classes.span}>ribbon</span> 
           </div>
        <Card className={classes.card}>
        <CardActionArea>
            <CardMedia
            component="img"
            alt="Contemplative Reptile"
            height="140"
            image="/static/images/cards/contemplative-reptile.jpg"
            title="Contemplative Reptile"
            />
            <CardContent>
            <Typography gutterBottom variant="h5" component="h2">
                Lizard
            </Typography>
            <Typography variant="body2" color="textSecondary" component="p">
                Lizards are a widespread group of squamate reptiles, with over 6,000 
                species, ranging
                across all continents except Antarctica
            </Typography>
            </CardContent>
        </CardActionArea>
        <CardActions>
            <Button size="small" color="primary">
            Share
            </Button>
            <Button size="small" color="primary">
            Learn More
            </Button>
        </CardActions>
        </Card>
    </div>
    );
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-06
    • 2021-06-15
    • 2021-03-26
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    相关资源
    最近更新 更多