简体   繁体   中英

Material UI : CardMedia Round Image

I'm new in full stack developing and I'm trying to code something to understand better frontend with React JS and Material UI. I've used a card component to show posts in backend but I want to show profile image (via CardMedia) circular, so I've decided to override the component like this:

import React, { Component } from 'react'
import {Link} from 'react-router-dom';
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';

import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import DeletePost from './DeletePost'
import PostDialog from './PostDialog'
//MUI
import withStyles from '@material-ui/core/styles/withStyles'
import CardMedia from '@material-ui/core/CardMedia';
import CardContent from '@material-ui/core/CardContent';
import Card from '@material-ui/core/Card';
import Typography from '@material-ui/core/Typography';
import ChatIcon from '@material-ui/icons/Chat'
import MyButton from '../../utils/MyButton';

const styles = {
    card: {
        display: "flex",
        marginBottom:20,

    },
    image:{
        minWidth: 100,
        minHeight: 100,
        borderRadius: '50%',
        objectFit: 'cover'
        
    },
    content:{
        objectFit: 'cover'
    }
    
    

}

class Post extends Component {
return (
            <Card className={{root: classes.card}}>
                <CardMedia
                    image={userImage}
                    title="Profile image"
                    className={classes.image}
                    />
                    
                    
                    <CardContent className={classes.content}>
                        <Typography 
                            variant="h5"
                            component={Link} 
                            to={`/users/${userHandle}`} 
                            color="primary">
                            {userHandle}
                        </Typography>
                        {deleteButton}
                        <Typography variant="body2" color="textSecondary">
                            {dayjs(createdAt).fromNow()}
                        </Typography>
                        <Typography variant="body1">{body}</Typography>
                        
                    </CardContent>
            </Card>
        )
}
Post.propTypes = {
    user: PropTypes.object.isRequired,
    post: PropTypes.object.isRequired,
    classes: PropTypes.object.isRequired,
    openDialog:PropTypes.bool
}

const mapStateToProps = state =>({
    user: state.user
})


export default connect(mapStateToProps)(withStyles(styles)(Post));


But the image looks oval and stretched. How can I resolve this?

From your css for the image, you've set a minHeight and minWidth. You'd want to explicitly set the height and width to equal px. Let me know if that works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM