As far as I understand, anything flexbox can do, css-grid should also be able to do (usually more verbosely).
Yet I cannot figure out how to mimic a flexbox with an item pushing off the others with margin: auto
ul {
list-style-type: none;
padding: 0;
display: flex;
flex-direction: column;
outline: 1px solid red;
height: 200px;
background-color: lime;
}
li {
background-color: cornsilk;
}
li:last-of-type {
margin-top: auto;
}
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
See how all cells are sized to their content and the last li
pushes the others away to appear at the end?
How would I do this with css-grid without modifying my html to add element?
ul {
list-style-type: none;
padding: 0;
display: grid;
outline: 1px solid red;
height: 200px;
background-color: lime;
}
li {
background-color: cornsilk;
}
li:last-of-type {
margin-top: auto;
}
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
This is close, but all rows are not sized to min-content
- I have no idea what they are sized to but its not min-content
. The closest I can get is to add
grid-template-rows: repeat(3, min-content);
which works but only if you know the amount of li
s ahead of time which is not necessary for the flexbox version.