Tips for ensuring an image fits perfectly within a MUI grid

I am currently working on an application that involves a collection of cards. My tech stack includes React and MUI v5.

One issue I've been facing is with the grid layout, specifically in trying to keep the image size consistent within the grid item. Despite trying various solutions such as using different containers for the image or experimenting with object-fit and background images, I haven't been able to find a satisfactory fix.

I suspect the problem lies in the parent element not having a fixed dimension. Is there a way to maintain dynamic sizing while utilizing MUI Grid?

<Grid container item xs={12} border={1} direction="column">
  <Grid item xs={2} border={1}>
    TITLE
  </Grid>
  <Grid item xs={5} border={1}>
    <img src="https://mui.com/static/images/cards/paella.jpg" style={{ maxWidth: "100%" }} />
  </Grid>
  <Grid item xs={3} border={1}>
    description
  </Grid>
  <Grid item xs={2} border={1}>
    footer
  </Grid>
</Grid>

The display appears properly when it's smaller, you can see it here.

However, the issue arises when the display is larger, resulting in something like this here.

Answer №1

The image appears to be a thumbnail size, consider replacing it with a larger image for optimal display on desktop. Alternatively, you can set the image width to 100% in the style attribute to fill the entire column.

<img
  src="https://mui.com/static/images/cards/paella.jpg"
  style={{ maxWidth: "100%" }}
/>

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

Strategies for avoiding the issue of multiple clicks on a like button while also displaying an accurate likes

My latest project involves creating a Like button component that features a button and a likes counter text field. The challenge I am facing is that each time I click on the button, it toggles between like and dislike states. However, if I rapidly press ...

Saving Files in Your React Web Application: Tips and Tricks

Currently, I am working on a React web application that requires the temporary storage of Torrent pieces for streaming purposes using a web player. Any recommendations on how to properly store this data temporarily in order to facilitate the streaming pro ...

What is the best way to position an image using css?

I am looking to position an image on the left side of my homepage using CSS instead of HTML. However, I am having trouble figuring out how to make it work. The image I want to use is called Nobullying.jpg. HTML: <html> <head> <link re ...

Is there a way to increase the spacing between the titles of these progress bars?

Ensure your responsiveness by enabling the Toggle Device Toolbar on your browser I'm attempting to create a progress bar, but I'm facing some challenges. Firstly, I want to increase the height of the semi-transparent black background, but I can& ...

Troubleshoot: Inability to highlight a column and row in a table using ::after and ::before

To create a hover effect where the corresponding column and row are highlighted when the mouse is on a cell, as well as highlighting the row for player 1's actions and the column for player 2's actions, I attempted the following code: /* CSS Cod ...

Having trouble making changes to MUI TextFields once they've been filled in with data from an

My goal is to make MUI TextFields editable even after they have been filled with data from an API. The TextFields are getting populated successfully. I attempted using an onChange function, but it only allowed one additional character to be entered befor ...

Is it possible to make consecutive axios requests?

I have a ReactJs application with two functions: AuthService.addUser(newUser); AuthService.userCategories(usercategories); When the submit button is clicked, I want to run these functions separately. The Axios request of the second function should be ca ...

What is the most effective way to retrieve IDs from Firestore and add them to the object arrays?

Currently working on a project that involves React and Firestore, I'm attempting to retrieve the Ids back into the objects array. Below is the code snippet: function grabIds() { setIsLoading(true); reference.onSnapshot((querySnapshot) => ...

Is my JSON data causing the error of invalid React child components?

Although this question has been asked multiple times on Stack Overflow, the solutions provided have not worked for me. My main goal is to retrieve notifications from a JSON file located here: I suspect that my JSON file may be poorly structured, especial ...

Error message encountered: "Element not located- Capybara unable to find element despite its existence

I’m currently in the process of writing a selenium test using capybara, and I’ve encountered an issue when trying to populate a pop-up box. Specifically, I am unable to input the element’s name using the fill_in method in capybara. Strangely, this me ...

When the drawer is opened, there is a strange phenomenon of all buttons being mysteriously clicked

Currently, I am working on a single-page web application utilizing React and Material UI, along with React-Mini-Router for routing. The app features a side drawer that is activated by clicking a hamburger icon located in the top app bar. Each item in the d ...

Strategies for effectively mocking and testing sub-components of third-party React components within a library

Is it possible that I am not using the correct term, but I have a component that utilizes react-icons specifically the Font Awesome ones. import { FaDog, FaHome, FaFileContract } from "react-icons/fa"; These icons are then passed as components t ...

Error in Typescript: The element is implicitly assigned an 'any' type due to the inability to use a 'string' type expression as an index

I'm a beginner with TypeScript and I encountered an error that's confusing to me while trying to follow an online tutorial on sorting data in Reactjs using React hooks. Here is the section of my component code where the error occurs: Element imp ...

Using React with Material UI Select native does not display an empty value when the selected value is not found

In my project, I am incorporating Material UI Select in a native way using the following code snippet: <Select value={values.teamId} native onChange={handleSelectChange}> <option aria-label="None" value="" ...

Encountered an issue connecting Firebase with NextJs during build process

Recently delving into NextJS, I successfully created a project using Firebase. However, upon running "npm run build," an error has surfaced: @firebase/firestore: Firestore (X.X.X): Could not establish connection with Cloud Firestore backend. Backend faile ...

What causes the scrollTop to appear erratic?

There is a simple issue that I find difficult to explain in text, so I have created a video demonstration instead. Please watch the video at this link: The functionality on my page works perfectly when scrolling down, as it replaces images with the next i ...

Customizing the appearance of Twitter Bootstrap based on the AngularJS input class $invalid

I'm struggling with getting error messages to display properly in my Bootstrap form when using AngularJS. I followed the instructions on this link: but I still can't figure out what I'm doing wrong. Any advice? Thanks <div class=" ...

Ways to convert an object with values into an array containing those values

To process the JSON data and convert it into an array with the same values for insertion into my PostgreSQL database using pool.query(message, values), where values should be an array if multiple are present. Currently, my object structure is as follows: { ...

Learn how to align a form to the right using HTML, CSS, and Bootstrap

I am completely new to front-end technology. I have been attempting to write some code for my backend app using Html, Css, and Bootstrap. Currently, I am trying to place a search form on the right side of my webpage. Can anyone help me figure out what I am ...

What is the best way to simulate a library in jest?

Currently, I am attempting to simulate the file-type library within a jest test scenario. Within my javascript document, this particular library is utilized in the subsequent manner: import * as fileType from 'file-type'; .... const uploadedFil ...