While working on a grid I realised that setting margin: auto;
for grid items drastically changes their behaviour.
I created a simple example to illustrate what happens.
Without setting any margin
, width
or height
for grid items, each item appear of the same height and width, as I would expect.
Adding only margin: auto;
, each grid item seems to behave like an inline
element: the box takes the size of the content (plus the padding I added). But adding height: 100%;
and width: 100%;
, keeping the margin: auto;
fixes it.
So, I'm not saying that this is wrong, I'm guessing it's the expected behaviour and I just don't understand it.
Can someone explain what's happening and possibly link to where it's documented?
* {
box-sizing: border-box;
}
.grid {
display: grid;
grid-gap: 1.5rem;
grid-template-columns: repeat(4, minmax(0, 1fr));
}
.grid > div {
border: 1px solid;
padding: 1.5rem;
/*
margin: auto;
height: 100%;
width: 100%;
*/
}
<div class="grid">
<div>1</div>
<div><h1>text</h1></div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
</div>