Adjusting the avatar size in Material UI to match the parent element's dimensions by styling the child elements to fill the entire space

Query on Dimension

How does Material-UI determine the width and height of elements within the <Avatar/> component?

Specific Scenario

My specific issue revolves around the <AccountCircle/> icon, which you can find as an example here: account circle, not adequately occupying the space as its parent.

My aim is to have an avatar size of 80x80, but due to the numerous random classes with varying margins and paddings added by Material-UI, the result is not as expected. The generated code contains an <svg> with a <path> element that measures around 66.67px.

const avatarImageStyle = {
    width: 80,
    height: 80,
};

JSX

<AccountCircle style={Object.assign({}, avatarImageStyle, {color: 'grey' })} />

Answer №1

I encountered a similar problem while working on a current project. I managed to resolve it by adjusting the fontSize of Avatar and ensuring the icon utilized its parent's font size.

It is unnecessary to specify the width and height of the icon because Material-UI icons (and likely icons in general) are symmetrical in shape. However, we must indicate the dimensions of the Avatar to prevent it from remaining the same size as before as the icon within it enlarges (resulting in cropping). By increasing the size of the Avatar's contents using fontSize, setting the dimensions to auto will suffice.

Therefore, for the Avatar's style, utilize:

{
    fontSize: '80px',
    width: 'auto',
    height: 'auto',
    ...
}

and when adding your icon, use:

<AccountCircle style={Object.assign({color: 'grey' })} fontSize='inherit' />

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

Unwanted extra border and padding are showing up at the bottom of my HTML

Recently, I decided to experiment with the jquery superslide plugin on a website of mine. To my surprise, there was an unexpected padding at the bottom of the page. My suspicion is that the display area is not being calculated correctly or that a margin is ...

Tips for creating consistent spacing between elements using Tailwind CSS version 3

I am currently utilizing Tailwind CSS in conjunction with next.js for my project. I have a total of 8 images, each varying in size, and my goal is to display 4 images per row on medium and large devices, while displaying 2 images per row on small devices. ...

Resizing rectangular images with CSS/Bootstrap into perfect squares

Imagine there is a rectangular image with dimensions of 1400px (height) x 700px (width). I am looking to turn it into a square while maintaining its original proportions and having white sides added. I specifically do not want to crop the image - I want it ...

The background banner is failing to display within the divbox

Having some trouble with my top banner in the HTML and CSS code I'm using to create a master page for ASP.NET. Oddly, the banner does show up on the design tab in Visual Studio, but it's not displaying properly. Here's the relevant code, can ...

Encountering issues with Material-UI sample code compilation

As a newcomer to React and programming in general, I may be overlooking an obvious mistake here, for which I apologize. While working on my first components in React, I have been heavily relying on sample code from Material-ui. I managed to successfully bu ...

What is the process for determining the area of the section?

Take a look at this page as an example - scrolling down on responsive devices reveals related navigation areas which I aim to change with scrollspy (affix fixed navigation). Having a bootstrap navbar, when on the corresponding section of the navbar while u ...

What is the best way to make img-fluid function properly within Bootstrap Carousel?

I've been struggling to make my carousel images responsive by using the img-fluid tag, but I haven't had any success. I've attempted using !important and display: block, but nothing seems to work. I'm not sure what's causing the is ...

Equal row sizes in the Bootstrap framework

I am trying my best to explain the situation. Currently, I am utilizing bootstrap for a project where I have two columns - one containing an image on the left and the other with a title and description on the right. My goal is to make the photo fill the en ...

Issue occurs when attempting to remove a row from state within the Material-UI DataGrid, resulting in a "Cannot

Below is an example of a material UI Datagrid: <DataGrid className={classes.datagrid} page={page} pageSize={rowsPerPage} rows={rows} ...

What is the best way to utilize a single component from Twitter Bootstrap?

Is there a way to use the Twitter Bootstrap plugin from this website ( https://github.com/silviomoreto/bootstrap-select ) for only one specific element on my webpage? I don't want all elements on my site to adopt the Twitter Bootstrap styling (such as ...

jQuery - encountering issues setting CSS properties within an AJAX callback function

I'm struggling with a jquery ajax callback function to change the background color of a table cell. Despite no errors in Firebug, my code isn't working as expected. Here's the code I have: $(".tariffdate").click(function () { var proper ...

Personalized FileInput within a card - Bootstrap 5

I am attempting to incorporate a custom file selection feature within a Bootstrap card: Here is my attempt: <style> label { cursor: pointer; } .custom-input-image { opacity: 0; position: absolute; z-index: -1; } </style> <div ...

The CSS file stubbornly refuses to connect, opting instead to redirect back to the HTML file

Something strange has occurred. I was working on a React app and everything was functioning properly until today when I returned to it, and my CSS file wasn't linking for some reason. The only actions I took were running npm run build and npm run depl ...

Timer countdown: Looking for a countdown timer that will reset to 3:00 automatically each time it reaches 0 minutes, and can do so

Seeking a continuous countdown timer that starts from 03:00 and counts down to 0:00, then automatically resets back to 03:00, in an infinite loop. The condition is that no one should be able to manually stop this logic. After researching, I found a Ja ...

Troubleshooting: CSS3 transform issue unresolved

I'm attempting to add a 10-degree rotation to my menu items. While this CSS effect works in Firefox, I can't seem to get it to work in Chrome and Safari. I already know that IE doesn't support this particular CSS3 property, so that's no ...

What is the best way to create divs that can close and hide themselves when clicked from inside the div itself?

I have a situation in my code where clicking a link reveals a div, and then the same link must be clicked again to hide it. However, I want to modify this so that any link within the div can also hide it when clicked. I currently have eight divs set up lik ...

Use jQuery to set a Firebase image as the background of a div element

Is there a way to fetch an image from Firebase and use it as the background for a div element? I've tried several approaches without success. Could someone share some examples on how to achieve this? <div class="museBGSize rounded-corners grpelem" ...

Ways to remove the default classes added by ngbootstrap

My current challenge involves utilizing ngbootstrap for popovers, yet desiring to override its default styling. Specifically, I have a form that should function as a popover triggered by a button click, and it needs to maintain its own distinct styles. Up ...

Efficiently select multiple classes in Material UI with just one target

I'm currently working with Material UI and I want to update the color of my icon class when the active class is triggered by react-router-dom's NavLink Here is my code: import React from "react"; import { makeStyles } from "@mater ...

Creating a Form Right in the Middle

I'm new to web development and I'm trying to figure out how to center my form on the page using CSS and HTML. Can someone help me out? <form action="login.php" method="post"> username: <input type="text" id="txtusername" /><br /&g ...