Creating two divs with equal heights in React using Material-UI at 50% each

I am currently utilizing a Material UI template within a react JS project.

My goal is to create a div (or Grid in MUI) that spans 100% of its container's height, containing two nested divs (or Grids) that are each set at 50% height. If the internal content exceeds the allocated 50%, it should be scrollable.

I have experimented with using sections but have not been successful in achieving the desired layout.

The approach I have taken so far only addresses the right column:

<Grid
        container
        direction="column"
        justifyContent="space-between"
        style={{ height: "100%" }}
      >
    <Grid item style={{ background: "red", height: "50%", overflowY: "auto" }} >
        <h1>OKKK</h1>
    </Grid>
    <Grid item style={{ background: "blue", height: "50%", overflowY: "auto" }} >
        <h1>OKKK</h1>
    </Grid>
</Grid>

This setup results in: https://i.stack.imgur.com/A8ojr.png

While this seems to work correctly for the first div where content can be added without issue, adding content to the second div causes the first div to extend beyond 50% of the total height as shown here: https://i.stack.imgur.com/olXPD.png

To address this challenge, how can I fix this layout issue?

Answer №1

Consider implementing

style={{ height: "100vh" }}
within your main element. This approach should resolve the issue at hand. To gain a deeper understanding of why this solution is effective, refer to the comprehensive explanation provided in this article

Answer №2

To include a Box component within your Grid, simply add the following code snippet:

<Box height="100vh"></Box>

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

Styling using CSS is effective only when applied locally, as it does not function properly on GitHub Pages

Although I have come across similar questions to the one I am facing, none of the solutions provided seem to work for me at the moment. My CSS file loads and functions as expected when tested locally, but once I upload it to GitHub, it stops working. In my ...

Encountering an issue with React Storybook after executing npx sb init command

I'm encountering some issues while attempting to set up Storybook in my project. After initializing create-react-app and navigating to the current directory, I am facing errors when running 'npx sb init' command: • Detecting project type. ...

How can I access the array of filtered options in MUI Autocomplete?

I am working on implementing a <Divider /> underneath each option displayed in the popper, except for the last row. Unfortunately, I am unable to share a picture at the moment. Please click here to see a screenshot of the issue. This is the current ...

Is it possible to utilize an array as a dependency in the useEffect() hook within React?

Exploring the usage of arrays as dependencies in the useEffect() hook in React. const [productCategories, setProductCategories] = useState([]); useEffect(() => { // Set something.. if(categories){ const filteredCategories = categories.fi ...

I am looking to split an array into smaller subarrays, each containing 5 elements, and assign a distinct class to the elements within each subarray. How can I

I have a group of "n" "li" elements that I would like to split into "x" subsets using jQuery. Each subset should contain 5 "li" elements, and I also want to assign a different class to each subset. <ul> <li>text1</li> <li>text2&l ...

What is the process for including or excluding a class from a horizontal scrollbar?

I've been trying to implement a back to top button on a horizontally scrolling page. However, I'm encountering difficulties in adding or removing the necessary class to show or hide the button. Here's the JavaScript code I'm using: $( ...

Having trouble configuring React-Router-Dom correctly

Hey there, I'm currently working on a project using react-router-dom. It seems like I may have missed a step in following the instructions because I'm encountering the following issues: bundle.js:1085 Warning: Failed prop type: The prop `history ...

Is it possible to refactor this forwardRef so that it can be easily reused in a function?

Currently, I am in the process of transitioning my application to Material UI V4 and facing challenges with moving my react router Link components into forwardRef wrapped components when setting the 'to' prop programmatically. The code below doe ...

Customizing Navbar for Authenticated Users in React

Currently, I am working with React and Firebase. My goal is to display different navigation bars based on whether the user is signed in or not. However, I am unsure about the best approach to take. Initially, I thought about creating two components and ren ...

Moving elements using CSS

Hi, I've been facing this issue with my web development since the start. Whenever I zoom in, the container also moves along. Is there any way to fix this problem? .darkslidecont{ width: 200px; height: 50px; position: relative; bac ...

You cannot utilize Lesson as a JSX Component in Next JS TypeScript

Below is my updated page.tsx code: import Aspects from '@/components/Aspects'; import FreeForm from '@/components/FreeForm'; import Lesson from '@/components/Lesson'; import React from 'react'; import { Route, Route ...

I need to find a way to show multiple blank spaces in a Material UI select component

Currently, I am encountering an issue with the Material UI select component. It is not displaying strings with multiple blank spaces as they appear in the array; instead, it is condensing them to just one space. How can I disable this feature? For example ...

Axios interceptor failing to intercept error statuses during API calls

My goal is to catch all 401 responses and redirect them to another page. Here is what I have implemented: const customInstance = axios.create({ baseURL: "https://api.example.com/", withCredentials: true }); // Intercept response customInstance.interc ...

Is there a way to retrieve the client's IP address from the server side within a Next.js application?

How can I determine the user's time zone and location in order to generate a server-side page tailored to their specific location and time zone? I am struggling to retrieve the user's IP address from the request or the localhost IP address (127.0 ...

Is it possible to turn the choices in the Material-UI autocomplete component into clickable hyper

import React, { useEffect, useRef } from "react"; import TextField from "@material-ui/core/TextField"; import Autocomplete from "@material-ui/lab/Autocomplete"; export default function FreeSolo(props) { const [vendors, setV ...

Tips for concealing BottomNavigation icons

Recently, I delved into the world of reactjs + material-ui library and am currently exploring how to hide icons within the BottomNavigation. Specifically, I'm looking for a way to achieve this: To showcase my progress, I have put together a minimal r ...

The React setState function isn't updating the state as expected when used within the useEffect hook

I'm new to using hooks and I'm facing an issue with setState when trying to update data received from an API. Despite logging the response data successfully, it seems that the state does not update as expected. My useEffect function is set to run ...

Issue with react router v6: Component fails to render even though route has been changed

The router seems to be experiencing an issue where it does not render a component. Specifically, on the home page, the Private Route is only rendered once. Clicking on a NavLink changes the URL to "/agreements", but the component itself is not being render ...

"Having trouble with defaultValue in react-hooks-form not functioning properly with NextUi Select component. Can

const fetchUserData = async (userId) => { const userApi = new UserAPI(); const result = await userApi.getInfoById(userId); const userData: Record<string, any> = result.data; const otherData = userData.other; for (const key in ...

What is the best way to create separation between the href attribute of an a tag and the

Currently, I am utilizing Material UI in React and encountering an issue with the spacing between an <a tag and some text both before and after it. The lack of space is causing them to merge together. <Typography style={{ fontSize: 14, textAlig ...