I'm trying to create a 2x2 CSS grid (possibly extended to 3x2 in the future) that only pushes items onto the first row when there are three children in the grid, and I'm not sure if this is possible with grid.
The scenarios based on the number of items would be:
2 Items (Minimum)
<div class="grid">
<div class="player">Player 1</div>
<div class="player">Player 2</div>
</div>
2 Player Grid
3 Items
<div class="grid">
<div class="player">Player 1</div>
<div class="player">Player 2</div>
<div class="player">Player 3</div>
</div>
3 Player Grid
4 Items
<div class="grid">
<div class="player">Player 1</div>
<div class="player">Player 2</div>
<div class="player">Player 3</div>
<div class="player">Player 4</div>
</div>
4 Player Grid
So far what I've tried is the following:
.grid {
height: 100%;
display: grid;
grid-template-columns: repeat(2, minmax(150px, 2fr));
grid-template-rows: repeat(2, minmax(150px, 2fr));
border: blue 5px solid;
}
.player {
border: red 5px solid;
display: flex;
justify-content: center;
align-items: center;
}
Changing the number of columns/rows to autofit/autofill also does not produce the desired result, and instead simply takes up the full width regardless. I've attached a live example of what I've tried below:
https://codesandbox.io/s/misty-smoke-bwvtt
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…