Fynydd Blog

Company and tech industry news, featured projects, open source code, tech tips, and more.
Post Categories

Animate CSS Auto Height Without Javascript

Published on Sunday, August 4, 2024

Web developers rejoice! There's an easy way to animate the height of an HTML element even if the height is dynamic, determined by its content, with only CSS. This is typically used for navigation menus and the like, and now it's much easier to code and maintain.

The strategy is to actually animate the grid-template-rows not the height. For example, take the following HTML markup:

<div class="menu">
    <div class="inner-wrapper">
        <p>Here is some content.</p>
        <p>Here is some content.</p>
        <p>Here is some content.</p>
    </div>
</div>

The CSS for this markup would be:

.menu {
    display: grid;
    grid-template-rows: 0fr;
    transition: grid-template-rows 100ms;
}

.menu.active {
    grid-template-rows: 1fr;
}

.menu .inner-wrapper {
    overflow: hidden;
}

Initially the outer div will be hidden since it has no overflow and the grid template rows are zero. When you add active to the outer div element's class list, the browser will animate the transition from zero row height to 1fr, which essentially means the height it needs for its content to render.

How can we help?

Whether your organization is big or small, book a conference call or request a project estimate and find out how Fynydd can help with your next web development or mobile app project!

Powered by Umbraco CMS with Sfumato™ and Tolnedra™

© 2024, Fynydd LLC

Privacy Policy

By using this website you accept our privacy policy. Choose the browser data you consent to allow:

Only Required
Accept and Close