Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a transition in height without knowing the height before?

I want to make a transition of height in CSS, but I noticed that I have to adjust the height statically so that it works.

Here is my code working with static height:

<!-- HTML --->
<div class="content show">
    <form method="post" action="">
        <label>Lastname
            <input type="text" name="lastname">
        </label>
        <label>Firstname
            <input type="text" name="firstname">
        </label>
    </form>
</div>

/* CSS */

.content{
    overflow:hidden;
}

.content.show{
    height:433px !important;       <<---------  //I would not define this height
    transition:0.5s;
    -webkit-transform:translateY(0);
}

.content.hidden{
    height:60px;
    transition:0.5s;
    -webkit-transform:translateY(0);
}

Here is the code I want to be with an automatic height:

<!-- HTML --->
<div class="content show">
    <form method="post" action="">
        <label>Lastname
            <input type="text" name="lastname">
        </label>
        <label>Firstname
            <input type="text" name="firstname">
        </label>
    </form>
</div>

/* CSS */

.content{
    overflow:hidden;
}

.content.show{
    height:auto !important;     <<<----------
    transition:0.5s;
    -webkit-transform:translateY(0);
}

.content.hidden{
    height:60px;
    transition:0.5s;
    -webkit-transform:translateY(0);
}

Someone would have an idea how to run this code? I specify that I do not want to use javascript.

like image 805
Florent Avatar asked Dec 04 '13 07:12

Florent


People also ask

Can you transition height?

We can't transition height , but we can transition max-height , since it has an explicit value. At any given moment, the actual height of the element will be the minimum of the height and the max-height .

How do you add transition to height in CSS?

For animate the "height" of element with CSS Transitions you need use "max-height". If use the "height: auto", the effect not works. Is necessary some value for the CSS create a CSS animate, and you can use "max-height" with a great value for emulate this effect.

How is height auto calculated?

When using the keyword auto , height is calculated based on the elements content area unless explicitly specified. This means a value based on a percentage is still that of the elements content area.

What is Max-height?

The max-height CSS property sets the maximum height of an element. It prevents the used value of the height property from becoming larger than the value specified for max-height .


1 Answers

If your height changes without your CSS knowing it, I am afraid that it is impossible. The animation has to know the height to use.

Here is the documentation of w3c : http://www.w3.org/TR/css-transforms-1/#two-d-transform-functions for the transitionY() which has to have a translation-value translateY() = translateX( <translation-value> ), and this translation-value must be known "statically" (so if you want it dynamic, you have to do it with Javascript. Here is the documentation of the transition-value http://www.w3.org/TR/css-transforms-1/#svg-transform-value

like image 100
Emilie Avatar answered Oct 30 '22 07:10

Emilie