'White space below MUI Grid container (which is not a padding or margin)

How can I avoid the extra white space below a nested MUI Grid container with dynamic and fixed size items (in a clean way) ?

I am interested in the "correct" MUI way of doing this kind of layout.

Apparently the content is considered "not fitting into the available width", but if I reduce the width of any content or spacing, then the content doesn't fill the whole width anymore (of course).

The code:

import { Grid } from '@mui/material';

export default function App(){
    return <div>
        <Grid container direction={ 'column' }>
            <Grid item style={{ border: 'solid 1px #f00' }}>

                <Grid container style={{ border: 'solid 1px #0f0' }}>
                    <Grid item xs={ true /* or false or 'auto' or 7 */ }>
                        <div style={{ height: '100px', width: '1px', backgroundColor: '#399' }} />
                    </Grid>
                    <Grid item xs={ 3 }>
                        <div style={{ height: '130px', width: '100%', backgroundColor: '#00f' }} />
                    </Grid>
                </Grid>

                {/* (here some mysterious space is displayed, if no other content follows) */ }

            </Grid>
        </Grid>
    </div>;
};

Apparently this happens if these conditions are met:

  • nested Grid containers
  • (inner) Grid item sizes include a specified size (a number 1-12) and a not specified size ('auto', true, false)
  • there is none or only small conent following the inner Grid container

Apparently the size of the extra space is exactly the space that would be needed to fit the last Grid item.

screenshot / illustration



Solution 1:[1]

To summarize again:

The two boxes are 50% and auto, so one box takes half of the screen, and the other takes the space needed for its content, e.g. 100px;

Of course, if the window is smaller than 2 x 100px, then the 50%-box can not be 50% anymore (because this 50% needs to be at least 100px, because of the other box). Therefore one box gets placed below the other, instead of next to it.

This is all fine, and there should be no problem if the window is larger than 2 x 100px. But this is where the extra space is added in this case of nested Grid containers.

Analysis:

Now I don't know how or in which order exactly the browser will determine or apply all the different sizes, but I guess that the browser might do something like this:

  1. Assume no specific window size, and apply some abstract "half size" for the 50%-box and 0 for the auto-box.
  2. Find the 100px content, which doesn't fit into the 0 box, and place the 100px-box below the 50%-box.
  3. Adjust the height of the parent container
  4. Find that the window width is large enough to fit everything inside it, and place the boxes next to each other again.
  5. Forget to adjust the height of the parent container again (note that the browser also needs to be careful not to infinitely adjust the parent according to the child, and the child according to the parent)

This will surely not be what the browser actually does, but it might be something like it.

So my basic idea was to tell the browser "as soon as possible" how much space will be needed (e.g. declare percentages instead of auto for outer elements, and let the auto sizing happen at the inner elements.), and this lead me to some solutions, which should work in different situations:

Solutions

1. Set the width of the outer item

<Grid container direction={ 'column' }>
    <Grid item style={{ width: '100%' }}>          <---- set width here 
        <Grid container>
            <Grid item xs={ 6 }>{/* ... */}</Grid>
            <Grid item xs={ true }>{/* ... */}</Grid>
        </Grid>
    </Grid>
</Grid>

Contra:
This changes the behavior of an ancestor, which is never a good approach, because the ancestor might have other descendants (now or in the future) that require the previous behavior.

2. Set min-width of the 50 % container

Set the min-width: 100px for the 50%-box (i.e. calculate what 50% should be at least).

This way the browser knows in advance that 50% + 100px needs to be at least 200px.

...
<Grid item xs={ 6 } style={{ minWidth: '150px' }}>{/* ... */}</Grid>
<Grid item xs={ true }>{/* ... */}</Grid>
...

Contra:

  • The 50 % container can not shrink smaller anymore if the window size gets smaller than 200px
  • If the 150px value or the percentage needs to be changed, it has to be changed in two places (unwanted relation / coupling)

3. Set nowrap for the outer Grid container

Set flex-wrap: nowrap for the outer Grid container.

This way the browser will overflow the items instead of placing them below each other, no extra height is added in an intermediate step, and no height needs to be corrected after the width is corrected.

<Grid container direction={ 'column' } style={{ flexWrap: 'nowrap' }}>
    <Grid item>
        <Grid container>
            <Grid item xs={ 6 }>{/* ... */}</Grid>
            <Grid item xs={ true }>{/* ... */}</Grid>
        </Grid>
    </Grid>
</Grid>

Contra:

  • This changes the behavior of an ancestor (same problem as solution #1)
  • We declare "don't wrap around if the window gets too small", but that's not what we want. We just want "fit everything inside the container, IF the window is large enough".

4. max-width instead of width

Set the max-width instead of the width of the 100px-box.

This way the browser never has a problem to fit the 150px-box next to the 50%-box, because it is allowed to shrink.

Contra:
The 100px-box will shrink below 100px if the screen gets too small, which might not be the desired behavior.

5. Another Grid container inside the auto Grid item

Instead of having 2 boxes with relative and absolute values mixed (50% and 100px), use 2 boxes with 50% each, and add another container inside the second 50% container, with the relatively sized item inside it.

E.g.:

<Grid container direction={ 'column' }>     // outer container
    <Grid item>
        <Grid container>                    // inner container

            <Grid item xs={ 6 }>               // left 50 % box 
            </Grid>

            <Grid container item xs={ 6 }>     // right 50 % box <--- another container
            
                <Grid item xs={ true }>           // (optional space or other content)
                </Grid>
                
                <Grid item xs={ 'auto' }>         // fixed pixel width 
                    <div style={{ height: '50px', width: '150px' }} />
                </Grid>
                
            </Grid>            
        </Grid>
    </Grid>
</Grid>

Solution 2:[2]

In this case use overflow: hidden; It worked for meenter image description here

when I used overflow: hidden; both the columns became equal.enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kca
Solution 2 haroon kiani