I understand that fr
is calculated based on available space in the grid container. I have a situation where I have a grid container that I want to split into 5 columns. The children however, are dynamically generated and depending on the situation, it could be 3 children or 4 or 5. I still want to keep the 5-column grid intact with the specified grid-column-gap
, but I want the grid to start populating the elements from the right. Please see my code below: https://codepen.io/skepticacid/pen/dyGxaJb
<html>
<body>
<div class = "grid-container">
<div class = "grid-child">1</div>
<div class = "grid-child">2</div>
<div class = "grid-child">3</div>
<div class = "grid-child">4</div>
<div class = "grid-child">5</div>
</div>
<div class = "grid-container">
<div class = "grid-child">1</div>
<div class = "grid-child">2</div>
<div class = "grid-child">3</div>
<div class = "grid-child">4</div>
</div>
<div class = "grid-container">
<div class = "grid-child">1</div>
<div class = "grid-child">2</div>
<div class = "grid-child">3</div>
</div>
</body>
</html>
html{
font-size: 16px;
}
.grid-container {
background-color: coral;
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-column-gap: 1rem;
justify-content: end;
align-items: center;
padding: 1rem;
margin-bottom: 2rem;
}
.grid-child{
background-color: saddlebrown;
color: white;
padding: 1rem;
}
5 elements is the happy path. However, when it comes down to 4 or 3 elements, I want them to be aligned similar to a justify-content
: end or flex-end
(so in the 4-column example, I want div number 4 to align with div number 5 above). Also, I also want to retain the width of the column to match the ones in the 5-column width.
Is this possible through CSS grid? Apologies, if I'm missing something glaringly obvious.