How to Align React Material UI Grid with Varying Row Counts in Each Column

Objective

My goal is to design a component that showcases details about a device, including an icon and its current status. For instance:

Approach I've Taken

I am attempting to accomplish this by structuring a MUI Grid in the form of a 2x2 grid layout, with the first column containing one cell specifically for the icon. This particular cell should span the height of two cells.

Challenge Faced

Although I have managed to create a three-cell layout using a MUI Grid, I am struggling to expand the cell so that it covers two rows instead of just one. Currently, it occupies the top-left position rather than stretching across both the top-left and bottom-left cells. If achieving this layout with a MUI Grid is not feasible, I would appreciate alternative suggestions.

Code Snippet

<div>
  <Grid container spacing={24}>
    <Grid item xs={6}>
      <Paper>Display a MUI Icon over two rows instead of one</Paper>
    </Grid>           
    <Grid item xs={6}>
      <Grid container spacing={24}>
        <Grid item xs={12}>
          <Paper>Top right</Paper>
        </Grid>
        <Grid item xs={12}>
          <Paper>Bottom right</Paper>
        </Grid>
      </Grid>
    </Grid>
  </Grid>
</div>

Answer №1

After reviewing your code, the issue seems to be arising from using two different containers. However, you can achieve the desired outcome by utilizing one main container and then incorporating the second container as needed.

To further illustrate this concept, I have created a demo for you.

<div className="App">
  <Grid container sx={{ m: 1, p: 2 }}>
    <Grid
      item
      xs={6}
      style={{
        backgroundColor: "red",
        display: "flex",
        alignItems: "center"
      }}
    >
      Display a MUI Icon over two rows instead of one
    </Grid>
    <Grid item xs={6} style={{ backgroundColor: "green" }}>
      <Grid container direction="column">
        <Grid item>Top right</Grid>
        <br />
        <br />
        <Grid item>Bottom right</Grid>
      </Grid>
    </Grid>
  </Grid>
</div>

Answer №2

To achieve this layout, I would utilize the power of Grid and Stack components.

        <Grid container sx={{ m: 1, p: 2 }}>
      <Grid item xs={6}>
        <Paper>Displaying a Material-UI Icon across two rows instead of one</Paper>
      </Grid>
      <Grid item xs={6}>
        <Stack>
          <Paper>Top right corner</Paper>
          <Paper>Bottom right corner</Paper>
        </Stack>
      </Grid>
    </Grid>

You can view the CodeSandBox demo here.

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

Ways to generate multiple void components side by side using React

What is the most efficient method for creating multiple empty inline elements using React in a declarative manner? Suppose I need 8 empty divs, I tried the following approach but it didn't work. Is there a more effective way? render() { return ( ...

Having trouble retrieving the exported state variable from a custom hook

I am struggling with a custom hook that handles light/dark mode using MUI. The hook exports a state variable that I pass to MUI's createTheme, but when I try to access the state variable outside of the hook, it returns undefined in the console. Can an ...

Tips for preventing a table from showing up while scrolling unnecessarily when utilizing a heading with the CSS position property set to 'sticky'

Currently, I am facing an issue with creating a sticky header for my table. The problem arises when the header of the table has rounded edges (top right and top left) along with a box-shadow applied to the entire table. As the user scrolls through the tabl ...

Tips for sending a Python email with attachment and including an HTML body along with an alternative plain text body

Using Python, I have developed code to send emails with HTML bodies and attachments. Additionally, I would like to include a plain text email body for clients that cannot display HTML content properly. While my code works in most cases, I've encounter ...

Run the GetServerSideProps function every time the button is clicked

Struggling with my NextJS app development, I encountered an issue while trying to fetch new content from an API upon clicking a button. The server call is successful, but the update only happens when I refresh the page rather than triggering it with the bu ...

Personalizing the label of a select input using materialui

Working on customizing a select element using the "styled" method: const StyledSelect = styled(Select)` height: 2rem; color: #fff; border-color: #fff; & .${selectClasses.icon} { color: #fff; } & .${outlinedInputClasses.notchedOutl ...

Tips for transforming Material UI style helpers with Jest

Currently, I am in the process of writing Jest unit tests for ReactJS components that are utilizing Material UI's makeStyles. Majority of the tests are passing without any issues; however, an error regarding Unexpected token is being thrown specifica ...

Adding a div with a canvas element is malfunctioning

I've been experimenting with using canvg to convert an SVG within a div into a canvas. So far, the conversion is working fine but when I try to copy the innerHTML of the div to another div, it's not functioning properly. The canvas is being gener ...

You can eliminate the display flex property from the MuiCollapse-wrapper

Having trouble removing the display flex property from the MuiCollapse-wrapper, I did some research and found the rule name for the wrapper in this link https://material-ui.com/api/collapse/ I have been unsuccessful in overwriting the class name wrapper t ...

Design an ARIA menu role with HTML and CSS code

Currently, my code is set up, but I'm unsure about integrating the tab/arrows to navigate to the sub-menu. You can view the code on jsfiddle: https://jsfiddle.net/Fep5Q/60/ This snippet shows a portion of the HTML code I've implemented: <di ...

Detect if the user is using Internet Explorer and redirect them to a different

My web application is having trouble rendering in Internet Explorer. In the meantime, I would like to detect if the user is using IE and redirect them to a different page specifically for IE visitors. What is the best way to accomplish this? Should I use ...

Customizing the size of an SVG shape using CSS properties

I am having trouble adjusting the width of the icon to 100% of the SVG container. This is what I've encountered: And here is the code for the SVG icon <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w ...

The collapsible list feature that includes the use of plus and minus signs is malfunctioning

Below is the script that I wrote to address this issue, but for some reason the + and - swapping is not functioning correctly. $('.showCheckbox').click(function(e) { var dynamicBox = $(this).attr('val'); var collapseSign = $(th ...

Centering all td elements except for those with a specific class

Consider the following code snippets: <tr> <td class="foo">chuchu</td> <td>chuchu</td> <td>chuchu</td> <td>chuchu</td> <td>chuchu</td> </tr> Is there a way to center align thes ...

Error: The function onChangePage is not defined in React Material-UI's TablePagination component

After updating from Material-UI v4 to v5, I encountered the following error message: TypeError: onChangePage is not a function function handleNextButtonClick(row) { > onChangePage(row, page + 1); } It seems that the onChangePage prop has become un ...

Having trouble getting the jQuery script to properly check file size in an HTML form before uploading?

I've created an HTML form with a jQuery script to verify the file size when the SAVE button is clicked. Despite my efforts, the check doesn't seem to be functioning properly in the HTML Form. Please assist me with this and thank you in advance ...

Utilizing Jquery for Enhancing Annotations

I am in the process of developing a website for essay writing tests. I have implemented a feature where users can input their essays using a text area, and now I want to be able to make comments on that text similar to PDF annotations or highlighting. I at ...

Auto-fit HTML Webpage Resizer Simplified

Just finished my very first jQuery project, a simple full-width slider. I've been focusing on HTML & CSS and currently working with C#. The problem is that I don't want the page to be scrollable; I want it to autofit to the webpage. Imagine ope ...

The feature in Chrome that automatically reloads generated CSS is failing to refresh the page after SASS recompiles the CSS

I'm attempting to set up Chrome's DevTools to automatically reload a page when I save changes to a watched SCSS file that compiles and updates the CSS file. Although I have checked the Auto-reload generated CSS option, it is unfortunately not fu ...

Error encountered when starting Npm: events.js:292 triggers an unhandled exception

As I dive into learning React, I decided to create a new React app using the create-react-app tool. However, upon running npm start after successfully creating the app, I encountered the following error message: events.js:292 throw er; // Unhandled ...