As far as I understand, something flexbox can do, css-grid should also be capable (usually in more detail).
But I can't figure out how to mimic a flexbox with an element repelling the rest 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>
Run codeHide resultSee how all cells have a size for their contents, and the latter li
repels the rest at the end?
How can I do this using css-grid without changing my html to add an 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>
Run codeHide resultThis is close, but all the lines have no size min-content
- I have no idea what they are designed for, but not min-content
. The closest I can do is add
grid-template-rows: repeat(3, min-content);
which works, but only if you know the amount li
ahead of time, which is not required for the flexbox version.