I'm working on a widget that will display up to 8 articles spread vertically across two columns. In the event of this widget showing less than 4 articles, I would like to avoid the extra space. I tried tons of things but without success and due to me not being very familiar with scripts, I would like to avoid them, if it can be done without them.
This is what I have so far :
article {
background: lightblue;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(4, 1fr);
grid-column-gap: 1em;
grid-auto-flow: column;
margin-bottom: 1em;
}
/* Styling Purpose */
div {
padding: 0.5em;
}
div:nth-child(odd) {
background: lightgray;
}
article.yes {
grid-template-rows: repeat(3, 1fr);
}
<h1>OK</h1>
<article>
<div>Article 1</div>
<div>Article 2</div>
<div>Article 3</div>
<div>Article 4</div>
<div>Article 5</div>
</article>
<h1>Not OK</h1>
<article>
<div>Article 1</div>
<div>Article 2</div>
<div>Article 3</div>
</article>
<h1>What I want</h1>
<article class="yes">
<div>Article 1</div>
<div>Article 2</div>
<div>Article 3</div>
</article>
https://codepen.io/HitArrowLegend/pen/bGBRvZR
If you know some css grid I don't, please let me know :D