Position the Form in the center of the page using Flexbox and Material UI styling

I am currently attempting to center align a button on the page. In addition, I plan to include a form in that same area, so I require a container or box that is positioned exactly in the middle of the page. Despite specifying a maximum height and width of 100%, the button is centered, which is great, but it remains at the very top.

It seems like I may need an additional box to serve as a container around the form components that will be nested inside the root Box.

I am unsure if I am providing the correct property names such as alignItems, since this involves styled objects where you cannot use align-items as a prop, for example.

import React from 'react';
import Button from '@material-ui/core/Button';
import { Box } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles({
    root: {
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        width: '100%',
        height: '100%'
    },
    button: {
        background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
        border: 0,
        borderRadius: 3,
        boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
        color: 'white',
        height: 48,
        padding: '0 30px'
    }
});

function Login() {
    const classes = useStyles();
    return (
        <Box className={classes.root}>
            <Button className={classes.button}>Login</Button>
        </Box>
    );
}

export default Login;

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

Answer №1

The reason for this issue is that the "root" class is applied to the Box component, which requires its parent element to have a specified height. Otherwise, the Box component will not be able to adjust its own height to 100%, as it will be unclear what exactly that percentage refers to.

To resolve this, try setting the height of the html, body, and any other parent elements.

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden
}
.root {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 100%;
    }
    .button {
        background: linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%);
        border: 0;
        border-radius: 3;
        box-shadow: 0 3px 5px 2px rgba(255, 105, 135, .3);
        color: white;
        height: 48;
        padding: 0 30px;
    }
<Box class="root">
    <Button class="button">Login</Button>
</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

Balanced Height Flexbox Columns

After searching and utilizing code from SO, it's evident that I am making a critical error. Typically, I rely on Bootstrap or Materialize, but this time, I wanted to start from the ground up. I have constructed a basic Flexbox layout featuring an ima ...

Error: Unable to locate module: Unable to resolve 'next/web-vitals'

I keep encountering the following error message: Module not found: Can't resolve 'next/web-vitals' I am interested in utilizing the **useReportWebVitals** feature from next/web-vitals. For more information, please visit: Optimizing: Analyt ...

Error encountered while trying to load the fonts

Upon attempting to load fonts into the Spectacle boilerplate, I encountered the following error message: Failed to decode downloaded font: http://localhost:3000/assets/fonts/Overpass-Bold.ttf OTS parsing error: invalid version tag I've includ ...

Ways to Customize the new Date() Function to Display Only Specific Fields

I am attempting to format my this.state.date to appear as DD/MM/YYYY. To achieve this, I utilized the library found at https://github.com/mmazzarolo/react-native-modal-datetime-picker Currently, I can select my desired date, however, when I assign it to t ...

Issue with React MUI Timepicker - Unable to selectively disable certain times

Is there a way to limit the hours on the clock? Check out this link for more information minTime={new Date(0, 0, 0, 8)} maxTime={new Date(0, 0, 0, 18, 45)} I'm having trouble getting this to work as intended. Do you have any tips on how to make it fu ...

Hiding BottomTabNavigator on specific screens using React Navigation (4.x)

Looking for a way to hide the bottom tab navigator specifically in the 'Chat' screen of my React Native and React Navigation app. Here is what I have so far: const UserNavigation= createStackNavigator({ Profile:{screen:Profile}, Search:{ ...

Different methods for creating a dynamic asp.net web Grid for mobile user interface (utilizing block display and selective column hiding)

Currently, I am developing a web application using asp.net mvc-4 and incorporating bootstrap 2.0 into the design. The main tool I am utilizing in almost every controller is the asp.net mvc web grid due to its efficient sorting, paging, and minimal code req ...

Is there a way to eliminate the transform style from a draggable element?

I am looking to enhance the CDK drag and drop example by adding a preview of the dragged element with specific left and top positions, without utilizing the transform style. HTML <div class="example-boundary"> <div class="example- ...

Track the cursor's movement

Looking to add an animation effect to my website. I want the navbar to follow the cursor within a limited space when hovered over. Check out this example for reference: . Here's the code I have so far, but it's not quite achieving the desired res ...

encountering this issue during the initialization of a React.js application

PS C:\Windows\System32\WindowsPowerShell\v1.0> npm start npm ERR! code ENOENT npm ERR! syscall open npm ERR! path C:\Windows\System32\WindowsPowerShell\v1.0\package.json npm ERR! errno -4058 npm ERR! enoent E ...

What is the best way to horizontally center an image with a transparent background using CSS?

A search button triggers a processing gif image to appear in the center with a transparent background. The content of this function is described below. Thank you for all responses. .img-loader{ text-align: center; width: 50px; display: block; ma ...

The primary division containing two inner divisions, one centered and the other positioned to the right

I would like the layout to resemble the image provided. The div header contains two nested divs - the logo div should be centered, and the info div should be positioned to the right with some margin. ...

Problem with selecting odd and even div elements

I have a div populated with a list of rows, and I want to alternate the row colors. To achieve this, I am using the following code: $('#PlatformErrorsTableData').html(table1Html); $('#PlatformErrorsTableData div:nth-child(even)').css(" ...

Configuring modules using Sass, CSS-Modules, Webpack, React, and TypeScript

I'm facing an issue with extracting types from my .scss files. I've tried various configurations and solutions, but nothing seems to work. Specifically, my goal is to utilize modules in a React app with TypeScript. Below is my webpack configura ...

Utilizing Bootstrap 4 to ensure the final DIV is vertically stacked and fills the remaining space in the container

Having trouble creating a responsive layout using Bootstrap 4? Specifically, when two divs wrap vertically on smaller devices, the top div pushes the lower div down, exceeding the parent container. I want the lower div to fit within the container. How can ...

WP Highlighted Image

I'm facing a small issue where the featured image on my posts is not displaying in the desired size. I want the featured image to match the total width of the post, but it keeps showing up in its original size. I'm clueless about how to fix this ...

Nested divs with independent scrolling capabilities

I am interested in developing a gantt chart that displays days, months, and years at the top with tasks listed below. Here is my progress so far: https://i.stack.imgur.com/tr5y4.png In the image above, the scroll-x functionality works on everything incl ...

It is not possible to utilize PopOver within the Header Menu Item

I am looking to create a header with a popover component. import React from "react"; import { Layout, Menu, Button } from "antd"; const { Header, Popover } = Layout; const { SubMenu } = Menu; const Index = (props) => { const content = ( & ...

The TextField Label in material-ui's @mui/material library remains unaffected by the RTL direction setting in a Next.js project

I am currently integrating Material-Ui v5 into my React project! import React, { useState } from "react"; import rtlPlugin from "stylis-plugin-rtl"; import { CacheProvider } from "@emotion/react"; import createCache from &quo ...

How can I position a Font Awesome icon in the center of an image with Bootstrap 3?

I'm attempting to center a font-awesome icon on top of an image within bootstrap 3. Below is the image code enclosed in a div: <div id="quickVideo"> <a data-fancybox href="https://player.vimeo.com/video/153113362?autoplay=1&color=54 ...