The default height setting for React MUI Avatar div is a fixed value of 40 pixels

I am working with a MUI Avatar component in my project:

<Avatar
  sx={{ width: 180, height: "auto", maxHeight: 180, margin: 2 }}
  variant="square"
  alt={""}
  src={
    global.config.baseUrl +
    "/image/logo" +
    this.state.user.userId +
    ".jpg"
  }
/>

when rendered, it creates the following HTML:

<div class="MuiAvatar-root MuiAvatar-square css-eqjmj4">
    <img alt="" src="/image/logo1.jpg" class="MuiAvatar-img css-1hy9t21">
</div>

and is styled using the following CSS class:

.css-eqjmj4 {
  position: relative;
  display: flex;
  -webkit-box-align: center;
  align-items: center;
  -webkit-box-pack: center;
  justify-content: center;
  flex-shrink: 0;
  height: 40px;
  font-family: Roboto, Helvetica, Arial, sans-serif;
  font-size: 1.25rem;
  line-height: 1;
  border-radius: 0px;
  overflow: hidden;
  user-select: none;
  width: 180px;
  max-height: 180px;
  margin: 16px;
}

I need to ensure that the width remains at 180px while allowing the height to scale proportionately (up to a maximum height of 180px)

Currently, the CSS class enforces a height of 40px; which I want to override. How can I achieve this?

Answer №1

It appears that the `square` version of the `Avatar` is essentially just CSS defining the `height` and `width`, which may not be very user-friendly.

My recommendation would be to include the `aspect-ratio` style (which is supported by all modern browsers) yourself with a value of `1 / 1`, as shown in the code below:

<Avatar
  sx={{
    width: 180,
    aspectRatio: "1 / 1", // <-- specify here
    height: "auto",
    maxHeight: 180,
    margin: 2,
  }}
  variant="square" // no longer necessary, can be omitted
  src="/static/images/avatar/3.jpg"
/>

This setup will function alongside `max-height` when you set a larger `width` than it.

Here's an operational illustration for reference.

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

Displaying asynchronous promises with function components

Apologies if this post appears duplicated, I am simply searching for examples related to class components. Here is the code snippet I am working with: export const getUniPrice = async () => { const pair = await Uniswap.Fetcher.fetchPairDat ...

Tips for keeping the footer consistently at the bottom of the page without using a fixed position (to avoid overlapping with the main content)

I've been struggling to keep the footer at the bottom of my webpage. I came across a solution online suggesting to use fixed CSS positioning. However, when I applied this fix, the footer ended up overlapping with the actual content on the page. Is the ...

React Native Flatlist does not deselect onPress

In my app, I have a flat list that allows users to select items by touching a row. When a user touches a row, the item is selected and its id is stored in an array. However, when the user tries to un-select the item by touching it again, the item is not re ...

Modify the surrounding container of the chosen selector

I have a complex website with multiple containers. My objective is to apply a color to a container with the class "colorme" when another container, located far away from it, has the class "active". Here is how the code is structured: .tooltip:has(tool ...

Resize lowercase letters to fit the box model

Currently, I am in the process of enhancing the python word cloud library available here by adding an HTML export option alongside the existing image export feature. The main challenge I'm facing right now is related to how the underlying python imag ...

Attempting to retrieve response information through a specified URL

Exploring a simple concept where I am making a request from the front-end to fetch JSON data on the back-end. My tech stack involves ReactJS on the front-end and NodeJS/ExpressJS on the back-end. I'm a bit confused about the correct way to access JSO ...

Expanding the height of Bootstrap 4 cards

I'm having an issue with Bootstrap 4's card columns where the height of the shorter card ends up stretching to match the one beside it. This only occurs when I add the 'row' class to the parent div. If I remove the 'row' class ...

Is There a Glitch in IE9? Unexplained Expansion of DIV Element

Here is a small code sample that I have: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Test</title> <style type="text/css"> table.Sample { width: 600px; table-layout: fixed; } table.Sample ...

The layout of the table is not formatted in a single continuous line

I recently implemented the material-ui table and noticed that the header has a multiline break space. I am looking for a way to make it display in a single line instead. Is there any solution for achieving this using material UI or CSS? Feel free to chec ...

Reverse the color of H1 text within a vertical div

Is it feasible to reverse the coloring of a segment within an h1 element using a div, horizontally? Refer to the illustration shown in the image below. https://i.sstatic.net/QAKwD.jpg ...

defining initial state values for a React class component using TypeScript

Here is the current state of the component: class Feed extends Component<FeedProps, FeedState> { constructor(props) { super(props); this.state = { isEditing: false, posts: [], totalPosts: 0, editPost: null, sta ...

Adjust the column width dynamically in an Ionic framework

Need help with changing the size of a Column element dynamically. <ion-col [size]="this.size" [size-xl]="this.size_xl" [size-md]="this.size_md" [size-sm]="this.size_sm" [size-xs]="this.size_xs"> Faci ...

Unable to view numbers on iPhone or ios device

I am facing an issue with displaying a phone number in the contact section on my website. The number shows up on desktop but not on my iPhone, even when I resize the screen to be smaller than my phone's display size. This issue has been tested on both ...

Issue arose while attempting to use Jest on a React Native application integrated with TypeScript (Jest has come across an unforeseen token)

Seems like everyone and their grandmother is facing a similar issue. I've tried everything suggested on Stack Overflow and GitHub, but nothing seems to work. It should be a simple fix considering my project is basic and new. Yet, I can't seem to ...

Is it possible to incorporate percentage-based widths within a fixed layout design on a webpage?

I've been delving into the concepts of fixed, fluid, and responsive web page design. One of the resources I stumbled upon is this article about fixed vs. fluid layouts. In the article, it mentions that "A fixed website layout has a wrapper that is a f ...

Responsive Ionic columns for listing items

Can you please try resizing this code snippet: http://codepen.io/anon/pen/zxGbbx <ion-content> <ul class="list"> <a class="item" ng-repeat="thread in data" ng-href="{{thread.url}}" target="_blank"> <li class=""> ...

Mastering the integration of an array of objects with the ej2 Syncfusion data grid

Trying to figure out how to map a complex array of objects into the ej2 Syncfusion React data grid. Here is an example of my array: [ { "id_team": "1",, "team_name": "Testing", "adviser": " ...

The React button fails to refresh the display

I am working on creating a dynamic input form that, when clicking a button, will generate a new row of description input fields. To keep track of these descriptions, I am using an array within the state: state = { descriptions: [ { ...

Troubleshooting Problems with CSS `transform` in JQuery UI's `Slide` Transition

While implementing JQueryUI's slide transition on an element with a CSS transform, I encountered an issue where the top half of the element gets hidden during the animation. Is there a way to tweak either my JQueryUI animation or CSS to avoid this pro ...

Creating a CSS section that expands with a minimum height of 100% and perfectly centers its content vertically

Seeking an elegant solution for having sections with a minimum height of 100% (window height). The div should expand to fit the content if it exceeds 100%, and vertically center the content within the div if it is smaller. I have managed to find a simple ...