"Create visually appealing designs with Material UI by incorporating round images in Card

Recently delving into full stack development, I've been experimenting with coding to enhance my understanding of frontend using React JS and Material UI. In the process, I incorporated a card component to display posts from the backend. However, I encountered a challenge when attempting to showcase the profile image in a circular form using CardMedia. To address this issue, I decided to customize the component in the following manner:


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));


After implementation, I noticed that the circular image appeared oval and stretched. Does anyone have a solution or suggestion to rectify this visual discrepancy?

Answer №1

Based on the CSS code you provided for the image, it appears that you have specified a minimum height and width. In order to ensure consistency, I recommend explicitly setting the height and width in pixels. Please try this solution and let me know if it resolves the issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for minimizing padding in X-Range graphs

Is there a way to reduce the overall height of my X-Range chart by adjusting the padding on the Y axis around each series? I have experimented with different properties, such as adding groupPadding: 0, pointPadding: 0, and other parameters using this jsfi ...

Preventing Shift: How to Only Replace the Chosen Vue Draggable Item Without Affecting Other Grid Items

Let's try an example to demonstrate: https://codesandbox.io/s/j4vn761455?file=/src/App.vue:112-116 Step 1: Drag item 4 to where 5 is. Notice how 4 and 5 switch positions. Step 2: Now, drag item 1 to position 6. Watch as 1 moves to where 6 was, pushi ...

ReactJS does not trigger a re-render when changes are made to CSS

I'm curious about the reason why ReactJS does not automatically reapply CSS to child components even when componentDidUpdate() is called. I conducted a small demo where adding a new box doesn't change the color of existing boxes, even though the ...

Troubleshooting a useContext error in Next.js with TypeScript

I've been working on an app using next.js for the frontend, and I encountered an issue while trying to stringify an object. Here's a snippet of the error message: Argument of type '{ auth: dataObject; }' is not assignable to parameter o ...

Converting JSON array to custom object array in TypeScript

As a newcomer to this area, please excuse any inaccuracies in my language. Don't hesitate to ask for more details if needed. I currently have some TypeScript interfaces: export interface Item { id: string type: string state: string } ex ...

How can I emphasize only a portion of a word in ReactJS and Material UI?

I am attempting to replicate this design: https://i.stack.imgur.com/H8gKF.png So far, this is what I have been able to achieve: https://i.stack.imgur.com/TJXXb.png My project utilizes Material UI and ReactJS. Below is a snippet of the code: bodyTitle: { ...

After attempting to follow a guide, I encountered a scenario where a view was returning None because the is_ajax function was not

After diving into the world of ajax, I encountered a puzzling issue that I can't seem to crack. My hunch is that it involves the comment_id versus the blog_id. (I was following this tutorial: https://www.youtube.com/watch?v=VoWw1Y5qqt8&list=PLKILt ...

Setting the ContentType in Node.js using res.writeHead() for HTML files that contain JavaScript

I encountered an error message that says Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. Could someone advise me on what s ...

How can I achieve a floating effect for a div element that is contained within another element?

I have a container located within the body of my webpage. This container has margins on the left and right, as well as a specified width. Inside this container, there is a div element that extends beyond the boundaries of the container to cover the entire ...

What is causing Firefox to consistently shave off 1px from the border of table cells?

Why is Firefox removing 1px from the border's value as defined in the CSS file? .aprovGriditem th { border-collapse: collapse; border: 4px solid #BBC6E3; padding: 0; } UPDATE <table cellpadding="0" cellspacing = "1" runat="server" id= ...

Overlap between sidebar and navbar

I am currently working on developing an admin panel for one of my projects. I designed a sidebar and implemented a standard bootstrap 4 navbar. However, I encountered a minor issue where the bootstrap 4 navbar is extending to the full width of the page ins ...

Exploring Javascript parameters with the power of jquery

Is it possible to pass a parameter from PHP into a JavaScript function within HTML? I am currently facing an issue where my code crashes when it reaches a certain condition. This is the code snippet causing the problem: $str="<input type='submit&a ...

Tips for updating an Observable array in Angular 4 using RxJS

Within the service class, I have defined a property like this: articles: Observable<Article[]>; This property is populated by calling the getArticles() function which uses the conventional http.get().map() approach. Now, my query is about manually ...

In Next.js 14, the cache cookie is obtained when navigating to pages, but only if there is no hard refresh

I am currently in the process of developing a small booking application using Next.js 14 without implementing any authentication. Within this app, there is a page called /events that displays a list of events, each with a "Book" button that triggers a func ...

Disable the button until all input fields contain text in ASP

Curious if anyone knows how to disable a button until all text boxes have input in ASP.NET and C#. Here is an image showing the scenario I'm referring to - wanting to gray out the commit button. Thanks, Chris! ...

AngularJS and PHP - Issue with HTML tags not being decoded properly

I'm currently working on a project using angularJS with a PHP backend and MySQL database. The PHP API is responsible for sending data to the angularJS frontend for display. Unfortunately, I'm facing an issue where HTML tags are not being decoded ...

Encountering a parsing failure in the _app.js file of a new Next.js project: Unexpected token

When starting a new Next.js project, I use the following command: npx create-next-app After moving into the project folder and running npm run dev, I encounter the following error: C:/Users/mikke/Documents/Projects/next-project/pages/_app.js 4:9 Module pa ...

Concerns about the Dependency Tree in React

I need some assistance with my current issue. I'm having trouble installing the mui search bar component. npm i --save material-ui-search-bar Unfortunately, I'm encountering this error message: PS Z:\WebDev\ApplyWithin\frontend> ...

Which type of components is more suitable for React - functional components or class-based components?

One aspect that has me confused is how both functional components and class-based components in React can now utilize State and Props, albeit in different ways. As someone more accustomed to Angular development, I find class-based components more familiar ...

Creating a dynamic table in AngularJS with rotating values for rows and columns

I am seeking assistance in creating a table with a custom number of rows and columns. The table should have two input fields for specifying the number of rows and columns, and upon submission, the table should dynamically adjust to display the specified nu ...