Unusual behavior exhibited by MUI Grid in the presence of spacing

I am working on a code snippet that looks like this:

<Box sx={{height:'100vh', background: '#f5f7fc', overflow:'auto'}}>
      <Grid container justifyContent={'center'} padding={2}>
        <Grid item xs={12}>
            <Typography align="center" fontSize={30}>Welcome! Please enter your childs information</Typography>
        </Grid>
        <Grid item xs={10}>
          <Paper>
            <Grid container padding={2}>
              <Grid item xs={6}>
                <TextField fullWidth label='First Name'/>
              </Grid>
              <Grid item xs={6}>
                <TextField fullWidth label='Last Name'/>
              </Grid>
            </Grid>
          </Paper>
        </Grid>
        
      </Grid>
  </Box>

Everything seemed fine until I decided to add some spacing with the following code:

<Paper>
   <Grid container padding={2} spacing={2}>

Suddenly, the layout changed and now the Paper element is sticking out above the Grid, causing an imbalance in the padding.

This unexpected behavior has left me puzzled and my question is: what could be causing this issue?

Answer №1

Instead of using the Paper component as a Grid container, you can make it a Grid itself and add spacing to the inner Grid element. Here's how:

<Box sx={{ height: "100vh", background: "#f5f7fc", overflow: "auto" }}>
  <Grid container justifyContent={"center"} padding={2}>
    <Grid item xs={12}>
      <Typography align="center" fontSize={30}>
        Welcome! Please enter your child's information
      </Typography>
    </Grid>
    <Paper component={Grid} container>
      <Grid container padding={2} spacing={2}>
        <Grid item xs={6}>
          <TextField fullWidth label="First Name" />
        </Grid>
        <Grid item xs={6}>
          <TextField fullWidth label="Last Name" />
        </Grid>
      </Grid>
    </Paper>
  </Grid>
</Box>

Updated solution:

It has been noted that the Grid component may have limitations with negative margin behavior, impacting spacing. To resolve this issue, it is suggested to apply display: flex on the parent component.

For more details, refer to: https://mui.com/material-ui/react-grid/#limitations

<Grid item xs={10}>
  <Paper
    style={{
      display: "flex",
    }}
  >
    <Grid container padding={2} spacing={2}>
      <Grid item xs={6}>
        <TextField fullWidth label="First Name" />
      </Grid>
      <Grid item xs={6}>
        <TextField fullWidth label="Last Name" />
      </Grid>
    </Grid>
  </Paper>
</Grid>

Please note that by adjusting the grid item size to xs={12}, the width will span in full accordance with your requirements.

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

Move images with drag-and-drop feature similar to Facebook cover using React Material UI

I am looking to incorporate Reposition image (Cover photo) feature similar to Facebook's cover functionality. Despite my efforts in searching for an npm package, I have been unsuccessful in finding one. I have the original image URL and need to save t ...

The process of updating a Material UI component from version 5.1.0 to 5.11.16

Currently, our project is using version 5.1.0 of the Material UI React component. We are looking to upgrade it to the latest version which is 5.11.16 as of now. I attempted to update the version numbers in the specified lines within the project's pac ...

What steps can I take to have the text extend beyond the background at both the top and bottom edges?

I am attempting to replicate this design using CSS: So far, this is what I have achieved: .container{ height: 30px; margin-bottom: 10px; } .redLine{ background-color: red; opacity: 0.5; height: 20px; border-radius: 5px; text-align: ce ...

Insert a gap between the two columns to create some breathing room

Does anyone know how to create spacing between columns in an HTML table without affecting the rows? In my table, each column has a border around it: <table> <tr> <td style="padding:0 15px 0 15px;">hello</td> <td style=" ...

Is there a way for me to have a background image behave like an img element with img-fluid class as shown in the

Showing 2 rows The issue lies with the first row, as it is not displaying properly and does not resize responsively like the second row. I have tried everything but nothing seems to work. I am attempting to do this because the text and elements in the ...

The full-width header is generating a horizontal scrollbar

Why am I seeing horizontal scroll bars on Safari and Chrome when the header width is set to 100%? Here is the code snippet: window.onscroll = function() { scrollFunction(); }; function scrollFunction() { if (document.body.scrollTop > 400 || doc ...

Eliminating blank spaces below or above images when displayed horizontally

Displayed below is the image depicting my problem: I have discovered a cumbersome method to eliminate this unwanted whitespace by treating it as four separate lists and utilizing a complex for-loop to add elements. However, this approach limits the number ...

Utilizing React Hooks efficiently with JSDoc annotations and destructuring arrays

Seeking guidance on how to properly JSDoc a React Typescript component that uses hooks, specifically when working with declared destructured arrays. Despite my efforts, I have not been able to find a solution that allows JSDoc to work seamlessly with destr ...

Introducing a variety of sub-menu fonts within an elegantly designed jQuery vertical accordion menu

I am struggling to achieve different font sizes, colors, and weights for top and sub-menu level links in my simple jQuery vertical accordion. Despite changing the CSS, I can't seem to get it right. What am I missing? Is there an easy way to accomplish ...

Learn how to render a single element with multiple child elements within separate `<td>` tags in a table row using React

I'm just starting out with React and I have a code snippet that I'm using to render an HTML table in a component. Is there a more optimized way to achieve this? bodyItems = sorted.map((data) => [ data.employerName, data.sectors.map((sector ...

Compass - substitute a single value for an alternate attribute

Can I utilize a value from one class to customize another? Consider the following class: .sourceClass { color: red; } And now, let's say we have this class: .destinationClass { border-color: ###needs to match the color in .sourceClass => re ...

The feature for choosing rows is not functioning properly when using materializecss in Tabulator

While working on my project, I encountered an issue with selecting rows. After some debugging, it was revealed that the culprit behind this behavior is the tabulator_materialize.min.css file. Interestingly, when I don't use this CSS, everything functi ...

Header on top of table that stays in place with scrolling

I'm facing an issue with a table that contains user-generated data. We are utilizing a plugin known as Clusterize, which allows us to smoothly scroll through large amounts of rows. However, the specific markup required for this plugin seems to be caus ...

Should you construct a fresh element using JavaScript, or opt to reveal a concealed one using CSS?

What are the benefits of using the following approach: var target = document.getElementById( "target" ); var newElement = document.createElement( "div" ); $( target ).after( newElement ); compared to just hiding the newElement div with CSS and showing ...

Horizontal Accordion Design for Cascading Style Sheets (CSS) Books

I am currently working on developing a page that features a book layout. This page will include tabs that users can expand individually. If you would like to see a working example, you can check out this link: https://codesandbox.io/s/book-layout-l28gh?fi ...

Display the gridview using <div> elements rather than tables

Does anyone have a code snippet that can display a gridview within a <div> element instead of a table? I would greatly appreciate any assistance with this. I am trying to create a graph similar to the one I have included and would like some functi ...

Guide to setting up a TreeView with default expansion

When using a @mui TreeView, all nodes are initially collapsed by default. I am trying to figure out how to have all nodes expanded by default, but haven't been successful so far. I attempted to create a method called handleExpandAll, but it doesn&ap ...

Stylish CSS round link positioned on a half-circle

As a beginner in web design, I'm struggling with CSS. However, I have been given the task of creating a button like the one shown below. I am unsure of how to create a circular link like this. Any assistance would be greatly appreciated. ...

Communication via socket between an external server, our server, and a client built using React

Currently, I am operating a server using express. Within this server, I have established a connection with an external socket server which uses "wss://..." as the protocol. While I am able to receive data from this external websocket, my primary inquiry ...

Determining the screen width of mobile devices across the board

While using the Google Chrome inspector to simulate the Galaxy S5 (360px) screen, I am encountering issues with detecting the correct screen width. The CSS for 360px is being overridden by the 768px CSS instead. Is there a more effective solution to this ...