Achieving Equal Heights in Two Grid Sections using React MUI

How can I create two separate grids next to each other using MUI, both taking up 50% of a larger grid?

In the image displayed below, I am struggling to make the heights of the two sections match. I want the smaller grid items (cards) to dynamically adjust in height to align with the right portion.

Is there a way to achieve this using Mui?

Below is the code snippet that I am currently working with:

import React from "react";
import Grid from "@mui/material/Grid";
import Box from "@mui/material/Box";
import Card from "@mui/material/Card";
import CardContent from "@mui/material/CardContent";
import Typography from "@mui/material/Typography";
import Chart from "./testChart.js";

function GeneralDashboard(props) {
  const defaultStats = [
    { name: "Opportunities Entered", value: 102 },
    { name: "Wins Reported", value: 23 },
    { name: "Win Rate", value: "60%" },
    { name: "Total Revenue", value: "$20m" },
  ];
  return (
    <>
      <Box sx={{ flexGrow: 1 }}>
        <Grid
          container
          spacing={{ xs: 1, sm: 2, lg: 2 }}
          columns={{ xs: 8, sm: 8, md: 8, lg: 8 }}
        >
          <Grid item xs={8} sm={8} md={4} lg={4}>
            <Box sx={{ flexGrow: 1 }}>
              <Grid
                container
                spacing={{ xs: 1, sm: 2, lg: 2 }}
                columns={{ xs: 4, sm: 4, md: 8, lg: 8 }}
              >
                {defaultStats.map((stat) => {
                  return (
                    <>
                      <Grid item xs={2} sm={4} md={4}>
                        <Card>
                          <CardContent>
                            <Typography
                              sx={{ fontSize: 14 }}
                              color="text.secondary"
                              gutterBottom
                            >
                              {stat.name}
                            </Typography>
                            <Typography variant="h3" component="div">
                              {stat.value}
                            </Typography>
                          </CardContent>
                        </Card>
                      </Grid>
                    </>
                  );
                })}
              </Grid>
            </Box>
          </Grid>
          <Grid item xs={8} sm={8} md={4} lg={4}>
            <Box sx={{ flexGrow: 1 }}>
              <Grid container spacing={{ xs: 1, sm: 1, lg: 1 }}>
                <Grid item xs={12}>
                  <Card>
                    <CardContent>
                      <Typography
                        sx={{ fontSize: 14 }}
                        color="text.secondary"
                        gutterBottom
                      >
                        <h5>
                          <span>
                            <span className="fw-semi-bold">Re-entry</span>{" "}
                            timing by industry
                          </span>
                        </h5>
                      </Typography>
                      <Chart />
                    </CardContent>
                  </Card>
                </Grid>
              </Grid>
            </Box>
          </Grid>
        </Grid>
      </Box>
    </>
  );
}

https://i.sstatic.net/VAssV.jpg

Answer №1

To ensure proper display, make sure to specify the height as 100% for all containers and items:

<Box style={{ flexGrow: 1, height: "100%" /* <----------------------- (1) */ }}>
  <Grid
    style={{ height: "100%" /* <----------------------- (2) */ }}
    container
    spacing={{ xs: 1, sm: 2, lg: 2 }}
    columns={{ xs: 4, sm: 4, md: 8, lg: 8 }}
  >
    {defaultStats.map((stat) => {
      return (
        <>
          <Grid item xs={2} sm={4} md={4}>
            <Card style={{ height: "100%" /* <----------------------- (3) */ }}>

https://codesandbox.io/s/69683726-react-material-ui-matching-the-heights-of-two-grid-sections-rsr95?file=/demo.js

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

Issue with utilizing the useDisclouse() function in Chakra UI within a forEach iteration

Is there a way to use useDisclouse() in Chakra with a button within a forEach loop? Whenever I click on a Button, it seems to trigger all Buttons. const {isOpen:isOpenEdit,onOpen:onOpenEdit,onClose:onCloseEdit} = useDisclosure(); ... ...

What is the best way to reset Material UI LinearProgress once it has finished?

I have a progress bar that goes from 0 to 100 over the course of a few seconds, then resets back to 0 and repeats the process. <LinearProgress variant="determinate" value={progress}/> Initially, the animation looks great. However, after r ...

"Enhance Your Vue Files with the Image Overlay Extension for FilePond

I'm trying to add a review button to an image, but I can't seem to find the right attribute for it. I've set the imagePreviewMarkupShow = true but it doesn't seem to be working. Check out the package here This is My Template < ...

Accept only requests from my Chrome extension

I have successfully set up a NodeJS server with Express on DigitalOcean. My Chrome extension is able to make GET calls to the server without any issues. However, I am looking to enhance the security of the middleware below: // Add headers app.use(function ...

Is it possible to retrieve all dependencies listed in a package-lock.json/yarn.lock file without actually installing them?

Seeking a way to organize dependencies from the NPM registry in a Nexus NPM proxy repository for any JavaScript project without having to run npm install or yarn install due to potential C/C++ compilation requirements for NodeJS C/C++ add-ons. One method ...

Navigating JSON data in a Kendo JavaScript Template

Currently, I am attempting to create a Kendo template that will iterate through a JSON array retrieved from an AJAX request. The structure of the data object is as follows: [{"Id":5, "CreatedBy":"testuser1"}, {"Id":6,"Archived":false,"CreatedBy":"testuser ...

Is there a different method to retrieve language bundles with next-i18next instead of using a customized Node server?

Currently, I am developing a Next.js application that will utilize i18next translations organized in the recommended file structure for bundles. For example: static/locales/en/common.js static/locales/de/common.js You can refer to this article: https://m ...

Which tool would be better for starting a new Angular project: angular-seed or yeoman?

I'm having trouble deciding on the best approach to create a new AngularJS application. There seem to be various methods available, such as using angular-seed from https://github.com/angular/angular-seed or yeoman - http://www.sitepoint.com/kickstar ...

The filter in React table is reorganizing the data

While working on my project, I encountered an issue with implementing column filtering in a grid. Whenever I click on the filter input box for the ID column, that column gets sorted unintentionally. I attempted to use e.stopPropagation() to address this pr ...

Utilize PHP drop down menus for efficient data sorting and retrieval

On my website, there are two drop-down menus - one for courses and the other for students. I am trying to set it up so that when a course is selected from the first menu, only the students enrolled in that specific course will appear in the second dropdown ...

Revamp the style of the date field in a Material UI dialog

I'm currently working on a React project where I am using Material-UI's date picker component to display date items. However, I find that the default style does not meet my requirements. I would like to use a selector for displaying dates in the ...

Combining jQuery within a single container/segment/div tag

Currently, I am experimenting with adding jQuery to my Cargo Collective website: I have been working on a mouse-tracking jQuery effect that I really like, but I am facing an issue where the script does not stay contained in a specific section of the site. ...

Learn how to automatically refresh your Firebase IdToken using the Apollo Reactjs client

When using Apollo Client in ReactJS to communicate with a GraphQL API, we implement Firebase authentication and JWT to protect private data from being exposed publicly. However, there is an issue with the expiry of Firebase tokens every hour or so. Curren ...

Selecting a framework with the appropriate level of flexibility

I am currently in the process of choosing a new framework to replace our existing one for our middle-sized project. Our previous project was developed using angularJS, and our team created the core of the application which was then distributed to our clie ...

In VueJS, the v-for directive allows you to access both parameters and the event object within

Imagine having nested elements that repeat using v-for in the following structure: <div id="container"> <div v-for="i in 3"> <div v-for="j in 3" v-on:click="clicked(i,j)"> {{i+&a ...

Experiencing a Number TypeError Issue with Mongoose Schema?

The server encountered a 500 internal error with the message: Error: TypeError: path must be a string The specific line causing the error in ItemCtrl.js is line 35. console.log('Error: ' + data); The stack trace for this error is as follows: ...

Navigation bar set to align in the center

I'm struggling to align the navigation bar I've designed in the center of the page. Here is the HTML Code: <div class="menu"> <ul> <li><a href="#">Home</a></li> <li><a href="#">About Mr. A</a& ...

How to implement dynamic aggregate functions with parameters in Express and Mongoose

I have implemented an aggregate function in mongoose to fetch some data, with a static implementation. app.get("/male",function (req,res) { Record.aggregate([ { $match: {"gender": "male"} }, { $group:{ _i ...

The Width of Material UI Divider

Currently seeking a method to increase the line thickness of the Material UI Divider, whether by stretching horizontal lines vertically or stretching vertical lines horizontally. I have reviewed the documentation for Material UI v5 on https://mui.com/mate ...

What's stopping the error exception from showing up on the client side?

Here's the scenario: I have an action method called SavePoint that contains some logic and throws a System.ArgumentException with the message "Error, no point found". Additionally, there is an ajax function named saveFeature which makes a GET request ...