CSS Trick ★ Featured

Animate Height with Grid

Finally animate to auto height! Use grid-template-rows from 0fr to 1fr for smooth accordion animations.

#css #animation #grid #accordion

Browser Support

✓ Chrome 107+ ✓ Firefox 110+ ✓ Safari 16.4+ ✗ Edge

Animate Height with Grid

CSS can’t animate height: auto, but it can animate grid-template-rows from 0fr to 1fr.

.accordion {
display: grid;
grid-template-rows: 0fr;
transition: grid-template-rows 0.3s;
}
.accordion > div {
overflow: hidden;
}
.accordion.open {
grid-template-rows: 1fr;
}

Live Demo

Grid Height Animation
CLICK
0.3s
CSS Code
.accordion-btn {
width: 100%;
max-width: 300px;
padding: 0.875rem 1rem;
background: linear-gradient(135deg, #8b5cf6, #06b6d4);
color: white;
border: none;
border-radius: 0.5rem;
font-weight: 600;
cursor: pointer;
display: flex;
justify-content: space-between;
align-items: center;
}
.grid-accordion {
display: grid;
grid-template-rows: 0fr;
transition: grid-template-rows 0.3s ease;
max-width: 300px;
}
.grid-accordion.open {
grid-template-rows: 1fr;
}
.grid-accordion-inner {
overflow: hidden;
}
.grid-accordion-content {
padding: 1rem;
background: rgba(139, 92, 246, 0.1);
border-radius: 0 0 0.5rem 0.5rem;
border: 1px solid rgba(139, 92, 246, 0.2);
border-top: none;
font-size: 0.875rem;
}

Click the button to see the smooth height animation. This technique works for any content length.

Share this CSS trick

Comments