How to make transition height from 0 to auto using CSS?


Making a transition height from 0 to "auto" is a way to smoothly animate an element's height as it changes to fit its content. In web development, creating smooth and elegant transitions can make a website more attractive and provide a better user experience. However, creating a transition from a height of 0 to "auto" can be a bit tricky.

Syntax

transition: [property] [duration] [timing-function] [delay];

Here, property is the CSS property that we want to animate, duration is the length of time that we want the transition to last, timing-function specifies the timing curve or pace of the transition, which determines how the animation accelerates or decelerates over time and delay is an optional parameter that sets the amount of time to wait before starting the transition.

Transition Height

In CSS, a transition is a way to create smooth and dynamic animations between two states of an element. Specifically, a transition height refers to the animation effect that occurs when the height property of an element changes, that improves the user experience and makes the website more engaging.

For example, if we want to create a smooth transition when expanding or collapsing a div or when toggling the visibility of a dropdown menu. we can easily do this using the CSS transition property with the height property,

Animating Height from 0 to "Auto"

When we want to create a transition from a height of 0 to "auto", it is not as simple as just setting the height property to "auto". In fact, the "auto" value is not a valid value for CSS transitions. We can do this by using the following steps.

Step 1: Create the HTML structure

To create the transition, firstly we need an HTML element to apply it to. let's use a div with a class of "element".

For example -

<div class="element">
   <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
   <p>Nulla faucibus nisi nec ullamcorper finibus.</p>
</div>

Step 2: Add the CSS

After creating the HTML structure, we need to add some CSS to create the transition. We start by setting the initial height of the "element" class to 0, and the overflow to be hidden.

For Example -

.element {
   height: 0;
   overflow: hidden;
}

This will hide the content inside the element until it is expanded.

Step 3: Add the transition

Now, we need to add the transition property to the element. We use the height property and set a duration and timing function.

For Example -

.element {
   height: 0;
   overflow: hidden;
   transition: height 0.5s ease-in-out;
}

In this example, the transition will take 0.5 seconds and use an ease-out timing function, which means the transition will start fast and slow down at the end.

Step 4: Set the expanded state

Here, to set the expanded state of the element, we use the pseudo-class and set the height to auto.

input[type="checkbox"]:checked~.element {
   height: auto;
}

Example 1

Here is an example to make transition height from 0 to auto using CSS.

<!DOCTYPE html>
<html>
 <head>
   <style>
      body { text-align: center; }
      .element {
         height: 0;
         overflow: hidden;
         transition: height 0.5s ease-in-out;
      }
      input[type="checkbox"]:checked~.element {
         height: auto;
      }
      p { margin: 0;}
   </style>
</head>
   <body>
      <h3>Transitioning height 0 to auto using CSS</h3>
      <input type="checkbox" id="toggle">
      <label for="toggle">Toggle Element</label>
      <div class="element">
         <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
         <p>Nulla faucibus nisi nec ullamcorper finibus.</p>
      </div>
   </body>
</html>

Example 2

Here is one more example to make transition height from 0 to auto using CSS.

<!DOCTYPE html>
<html>
<head>
   <style>
      body{ text-align:center;}
      .box {
         max-height: 0;
         width:200px;
         margin:auto;
         transition: max-height 1.4s ease-out;
         overflow: hidden;
         background: #b2ceed;
      }
      .tab:hover .box {
         max-height: 500px;
         transition: max-height 1s ease-in;
      }
   </style>
</head>
   <body>
      <h2>Transition height 0 to auto using CSS </h2>
      <div class="tab"><b>Hover on me to increase the height.</b>
         <div class="box">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
      </div>
   </body>
</html>

Conclusion

Creating a transition from 0 to auto using CSS is a simple but effective way to add visual interest to the website. By following the steps outlined in this article, we can create this effect easily and enhance the user experience on the site.

Updated on: 16-Mar-2023

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements