What is the best way to vertically align a Material UI component to occupy the remaining space within a container

Issue with Material UI/Grids for Login Page

As a newcomer to Material UI/Grids, I am currently working on creating a login page where the layout is split into two parts. The left side occupies 5 column spaces while the right side takes up 7 column spaces. Within the right side of the login page, there should be two main components: a login header and a login form. My goal is to have the login header occupy approximately 20-30% of the container height, with the login form centered both vertically and horizontally within the remaining space (60-70% of the container).

Progress So Far:

To better visualize the positioning of my containers and items, I have incorporated some styling in my code. While I believe I have successfully implemented most of the required functionality, there are still a few lingering issues.

Code Sample: https://codesandbox.io/s/material-ui-grid-forked-duszk?file=/src/App.js

Ongoing Challenges:

  1. The right side of the login page does not match the height of the left side.
  2. Although the login form is centered horizontally, it lacks vertical alignment and fails to utilize the available space within the grid.
  3. There appears to be no spacing between the grid items on the right-hand side.

Answer №1

  1. The height of the right side on the login page doesn't align with the left side.

    For the left side (<Grid sm={5} item>), make sure to set the

    style={{ height: "100vh" }}
    of <Grid> within the <Box> so that the grid container's height is maintained correctly. Here's an example:

    <Grid sm={5} item>
       <Box
           className={classes.sidebar}
           display={{ xs: "none", sm: "block" }}
           style={{ height: "100vh" }}  // adjust height here
       >
          <Grid>
          </Grid>
       </Box>
    </Grid>
    
  2. The login form is centered horizontally but not vertically, and it's not utilizing the available space in the grid.

    To solve this, set the parent's height for vertical alignment using justify-content.

    <Grid
         container
         item
         direction="column"
         alignItems="center"
         justify="center"
         style={{  height: "100vh" }}  // adjust height here
    >
         <Box className={classes.form}>
           <FormControl>
    
           </FormControl>
         </Box> 
    </Grid>
    
  3. After checking the codesandbox, it seems like you've resolved the issues mentioned above.

Answer №2

To tackle this issue, utilize the Grid component from @material-ui/core and makeStyle function from @material-ui/styles.

The important properties to focus on are justify and alignItems within the Grid component.

Here is the implementation:

import React, { useState } from "react";
import { Grid, Paper} from "@material-ui/core";
import { makeStyles } from "@material-ui/styles";

const useStyles = makeStyles(theme=>({
  root: {
    height: "100vh",
  },
  right: {
    width: "100%",
  },
  header: {
    height: "30%", 
  },
  body: {
    height: "70%",
  }
}));

export default function LDashboard1(props) {
  const classes = useStyles();
  return (
    <Grid container className={classes.root}>
      <Grid item xs={5}>
        <div>
          {/* Left area */}
        </div>
      </Grid>
      <Grid item xs={7} container direction="column" className={classes.right}>
        <Grid item className={classes.header}></Grid>
        <Grid
          item
          container
          direction="row"
          justify="center"
          alignItems="center"
          className={classes.body}
        >
          <Paper >This is a test.</Paper>
        </Grid>
      </Grid>
    </Grid>
  );
}

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

Error: Authentication error. fatal: Unable to access the remote repository." encountered while executing the command "yarn install

Today, while developing a web application, I encountered an issue with the "yarn install" command. Upon running "yarn install", the console displayed an error message: "Host key verification failed. fatal: Could not read from remote repository." I attemp ...

Arranging items in a flex container

My goal is to achieve the following layout with flexbox: #box { display: flex; flex-wrap: wrap; flex-direction: row; justify-content: center; justify-items: center; align-items: center; } #spotLight img { width: 15%; height: 15%; } # ...

Navigating to the detail page following a POST request in RTK query: A step-by-step guide

In my React RTK-query form, I am facing an issue where after a POST request is made, the form should navigate to the next step. However, in order to do that, I need to obtain the id of the newly created record. The backend auto-increments the id and does n ...

Unexpectedly, the child component within the modal in React Native has been resetting to its default state

In my setup, there is a parent component called LeagueSelect and a child component known as TeamSelect. LeagueSelect functions as a React Native modal that can be adjusted accordingly. An interesting observation I've made is that when I open the Lea ...

"Creating a navigational bar using Bootstrap in a one-page application

I am facing an issue with Bootstrap's navbar in my single-page application. The dropdown navigation menu for mobile is not collapsing properly after clicking. Although I tried to fix it by adding data-toggle="collapse" data-target="#navbar" attributes ...

Resizing an image that is being pulled from a database

I am currently working on a challenging database project that seems to have hit a minor cosmetic roadblock. The database I'm dealing with loads profiles into arrays for display using a PHP while loop, making it easy to display content and allow for a ...

material-ui Dropdown displaying selected item issue

Struggling with my material-ui select box that is powered by a state variable. Despite all my attempts, I just can't seem to display the chosen value when selecting an option. Why could this be happening? All I see is a blank bar every time. Even afte ...

VueJS component fails to remain anchored at the bottom of the page while scrolling

I am currently using a <md-progress-bar> component in my VueJS application, and I am trying to make it stay fixed at the bottom of the screen when I scroll. I have attempted to use the styles position: fixed;, absolute, and relative, but none of them ...

Sass flex order has no effect on elements that are generated

I encountered an issue with the rendering of SVGs in my project. Currently, they are displayed correctly in desktop mode, but in mobile portrait mode, I need to change the order of one specific SVG element within the row. To achieve this, I decided to use ...

What is the solution to resolving unmet dependencies in react?

Having some trouble reinstalling an older react project and encountering strange errors that are difficult to decipher. I attempted to update the version of react to a newer one, but unfortunately that did not resolve the issue. Currently running npm 8.11. ...

When using React.JS / Next.JS with @material-ui/core/styles, you may encounter the `ERR_UNSUPPORTED_DIR_IMPORT` error

When importing { makeStyles } from '@material-ui/core/styles', an error is encountered:</p> <pre><code>- information Collection page data ..Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/usr/src/sellerportal/node_mo ...

Incorrect synchronization in the SVG arrow animation

How come the arrow doesn't start moving at the same time as the line? Is there a synchronization issue? I want the arrow to begin its journey simultaneously with the line. .container{ width:100%; padding:0px; background-color: black; } .squig ...

What is the best way to elegantly finish a live CSS animation when hovering?

Currently, I am working on a planet orbit code where I want to enhance the animation speed upon hover. The goal is for the animation to complete one final cycle at the new speed and then come to a stop. I have been successful in increasing the speed on hov ...

Modify the background color of React-select when it is in a disabled state

I found this interesting tip on color customization in React Select When the select field is disabled, I want to change its color to something different. isDisabled={true} To achieve this, I am modifying the code as follows: > import React from &q ...

Struggling to set up the connection between React-Redux connect and the Provider store

Currently utilizing Redux with React Native for state management. I've set up the store and Provider successfully it seems, as I can utilize store.getState() and store.dispatch(action()) from any component without issue. However, I'm encountering ...

Aligning text and lists using Bootstrap on both desktop and mobile devices

I want to format a text with a list similar to the images provided https://i.stack.imgur.com/6giDH.png for desktop and tablet view, but for mobile display I would like it to look like this https://i.stack.imgur.com/7zSXi.png This is my current approach ...

The reverse is concealed following a CSS flip transition

After applying a CSS flip animation to a card, the backside isn't visible once flipped. Here is the HTML code for the card component named "ptree-card": <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <!-- Rest of th ...

Looking to achieve a scroll effect with mix-blend-mode using JavaScript?

I am seeking a method to adjust the background color of a specific element based on the background itself. While CSS offers the mix-blend-mode property, I am looking to specify the color manually. Ideally, I would like to achieve the same effect using an ...

"Position centrally on the inside, using flexbox to fill the entire

Seeking assistance with aligning the text in the center of the flexboxes so that the h4 and p elements in the large box are centered within their respective boxes, as well as ensuring the text in the two smaller boxes is also centered. Any help would be gr ...

Issue with closing submenu in React using Material UI

I have a structured menu layout that includes nested items: menuList = [ { "key": "key1", "caption": "Text1" }, { "key": "key2", "caption": "Text2", ...