I am working with a basic css grid that contains X number of grid items. These grid items can vary in size and I need to implement an expander feature. The goal is to initially display only a certain number of rows, but upon clicking a button, the grid will expand to show all columns.
My initial approach was to set a transition for grid-auto-rows with a value of 0fr. However, it seems that this property is not animatable like grid-template-rows.
const button = document.querySelector('button');
const grid = document.querySelector('.grid');
button.addEventListener('click', function(){
grid.classList.add('expanded');
});
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: 1fr;
grid-auto-rows: 0fr;
gap: 1rem;
transition: all 0.5s ease;
}
.grid.expanded {
grid-auto-rows: 1fr;
}
.grid.expanded + button {
display: none;
}
.grid-item {
overflow: hidden;
}
<div class="grid">
<div class="grid-item">
<img src="https://place-hold.it/100x100">
</div>
<div class="grid-item">
<img src="https://place-hold.it/100x100">
</div>
<div class="grid-item">
<img src="https://place-hold.it/100x100">
</div>
<div class="grid-item">
<img src="https://place-hold.it/100x100">
</div>
<div class="grid-item">
<img src="https://place-hold.it/100x100">
</div>
</div>
<button>expand</button>