Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add slide-up & slide-down animation to variable height div?

Tags:

html

css

I want to add slide-up and slide-down animation when I hover over a div card.

Initial card:

enter image description here

When I hover over the card:

The yellow part should slide-up and when I remove the hover it should slide-down till it's not visible. enter image description here

I'm able to show and hide yellow part on hover but I'm not able to animate it. I guess because of top: 182px; bottom: auto; (use to hide yellow part and position purple part at bottom) and top:auto;bottom:0; (to completely show yellow card irrespective of it's height)

Here's the code:

.card{
  margin-right:20px;
display: inline-block;
    padding: 0;
    border-radius: 19px;
    box-shadow: 0 6px 10px 0 rgba(0, 0, 0, 0.2);
    overflow: hidden;
    height: 200px;
    background-color:#2196f3;
    position:relative;
    text-align:center;
}
.image{
  padding:50px;
}
.content{
  border-radius: 0 0 19px 19px;
        background-color: #673AB7;
    position: absolute;
    width: 100%;
    top: 182px;
    bottom: auto;
}
.desc{
  background-color:#ffeb3b;
}

.card:hover .content{
  top: auto;
    transition: all 2s ease;
    bottom: 0px;
}
<div class='card'>
    fixed height card
    <div class='image'>
        fixed height image
    </div>
    <div class='content'>
        <div class='title'>
            fixed height title
        </div>
        <div class='desc'>
            =:variable height description:= Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
            labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud the end.
        </div>
    </div>
</div>

<div class='card'>
    fixed height card
    <div class='image'>
        fixed height image
    </div>
    <div class='content'>
        <div class='title'>
            fixed height title
        </div>
        <div class='desc'>
            =:variable height description:= Lorem ipsum dolor sit amet, consectecing elit, sed do eiusmod tempor incididunt ut the end.
        </div>
    </div>
</div>

Here's the JSFiddle to play around: http://jsfiddle.net/JerryGoyal/63c8hbr5/

I'm open to ideas as long as it can be done with CSS only!

like image 379
GorvGoyl Avatar asked Oct 25 '25 17:10

GorvGoyl


2 Answers

You are right inabout needing to use always the same position technique.

You need to stick to bottom, and then set a translateY

.card {
  margin-right: 20px;
  display: inline-block;
  padding: 0;
  border-radius: 19px;
  box-shadow: 0 6px 10px 0 rgba(0, 0, 0, 0.2);
  overflow: hidden;
  height: 200px;
  background-color: #2196f3;
  position: relative;
  text-align: center;
}

.image {
  padding: 50px;
}

.content {
  border-radius: 0 0 19px 19px;
  background-color: #673AB7;
  position: absolute;
  width: 100%;
  bottom: 18px;
  transform: translateY(100%);
  transition: all 2s ease;
}

.desc {
  background-color: #ffeb3b;
}

.card:hover .content {
  bottom: 0px;
  transform: translateY(0%);
}
<div class='card'>
  fixed height card
  <div class='image'>
    fixed height image
  </div>
  <div class='content'>
    <div class='title'>
      fixed height title
    </div>
    <div class='desc'>
      =:variable height description:= Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud the end.
    </div>
  </div>
</div>

<div class='card'>
  fixed height card
  <div class='image'>
    fixed height image
  </div>
  <div class='content'>
    <div class='title'>
      fixed height title
    </div>
    <div class='desc'>
      =:variable height description:= Lorem ipsum dolor sit amet, consectecing elit, sed do eiusmod tempor incididunt ut the end.
    </div>
  </div>
</div>
like image 90
vals Avatar answered Oct 28 '25 08:10

vals


Changed the transition to work on desc div instead of content. Also transitions don't work on auto property. Try using max-height property like i have shown. The max-height should be some very large height that your div can get.

Updated CSS

.card {
      margin-right: 20px;
      display: inline-block;
      padding: 0;
      border-radius: 19px;
      box-shadow: 0 6px 10px 0 rgba(0, 0, 0, 0.2);
      overflow: hidden;
      height: 200px;
      background-color: #2196f3;
      position: relative;
      text-align: center;
    }

    .image {
      padding: 50px;
    }

    .content {
      border-radius: 0 0 19px 19px;
      background-color: #673AB7;
      position: absolute;
      width: 100%;
      bottom: 0;
    }

    .desc {
      background-color: #ffeb3b;
      max-height: 0;
      transition: all 2s ease;
    }

    .card:hover .desc {
      max-height: 500px;
    }

.card {
  margin-right: 20px;
  display: inline-block;
  padding: 0;
  border-radius: 19px;
  box-shadow: 0 6px 10px 0 rgba(0, 0, 0, 0.2);
  overflow: hidden;
  height: 200px;
  background-color: #2196f3;
  position: relative;
  text-align: center;
}

.image {
  padding: 50px;
}

.content {
  border-radius: 0 0 19px 19px;
  background-color: #673AB7;
  position: absolute;
  width: 100%;
  bottom: 0;
}

.desc {
  background-color: #ffeb3b;
  max-height: 0;
  transition: all 2s ease;
}

.card:hover .desc {
  max-height: 500px;
}
<div class='card'>
  fixed height card
  <div class='image'>
    fixed height image
  </div>
  <div class='content'>
    <div class='title'>
      fixed height title
    </div>
    <div class='desc'>
      =:variable height description:= Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud the end.
    </div>
  </div>
</div>

<div class='card'>
  fixed height card
  <div class='image'>
    fixed height image
  </div>
  <div class='content'>
    <div class='title'>
      fixed height title
    </div>
    <div class='desc'>
      =:variable height description:= Lorem ipsum dolor sit amet, consectecing elit, sed do eiusmod tempor incididunt ut the end.
    </div>
  </div>
</div>
like image 20
Nandita Sharma Avatar answered Oct 28 '25 06:10

Nandita Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!