12

I'm trying to create a div that expands on hover, but cannot figure out how to make the content expand with the div. I've tried a few overflow options, but they didn't work.

.grow {
  padding: 5px 5px 5px 5px;
  border-radius:10px;
    height: 50px; 
    width: 22%; 
    margin: 5px 1% 5px 1%; 
    float: left; 
    position: relative; 
    transition:height 0.5s; 
    -webkit-transition:height 0.5s; 
    text-align: center;

}
.grow:hover {
    height: 115px; /* This is the height on hover */
}
<div class="grow" style="background-color: #2a75a9;">
    <h2>Title</h2> <br>
    Contrary to popular belief, Lorem Ipsum is not simply random text.  It has roots in a piece of classical Latin literature
</div>

Here is the JSFiddle

TylerH
  • 20,799
  • 66
  • 75
  • 101
Nicole
  • 381
  • 2
  • 3
  • 10

4 Answers4

30

Add overflow: hidden to the parent(.grow) container to hide the description. This will reveal the description on hover.

Additionally, instead of using the <br /> tag, wrap the text in a <p> tag.

.grow {
  padding: 5px 5px 5px 5px;
  border-radius: 10px;
  height: 49px;
  width: 22%;
  margin: 5px 1% 5px 1%;
  float: left;
  position: relative;
  transition: height 0.5s;
  -webkit-transition: height 0.5s;
  text-align: center;
  overflow: hidden;
}
.grow:hover {
  height: 145px;
}
<div class="grow" style="background-color: #2a75a9;">
  <h2>Title</h2> 
  <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature</p>
</div>
Weafs.py
  • 22,731
  • 9
  • 56
  • 78
3

just update;

.grow:hover {
    height: auto; /* This is the height on hover */
}
Alex Char
  • 32,879
  • 9
  • 49
  • 70
Aru
  • 1,840
  • 1
  • 12
  • 15
1

Check with the below link if it works with you.

https://jsfiddle.net/gknLstpt/9/

 .grow {
  padding: 5px 5px 5px 5px;
  border-radius: 10px;
  height: 49px;
  width: 15%;
  margin: 5px 1% 5px 1%;
  float: left;
  position: relative;
  transition: height 0.5s;
  -webkit-transition: height 0.5s;
  text-align: center;
  overflow: hidden;
}
.grow:hover {
  height: 145px;
    width: 145px;
}
Steevan
  • 826
  • 5
  • 8
  • The .grow class needs the following two lines to create a smooth transition for width as well: `transition: width 0.5s; -webkit-transition: width 0.5s;` I also changed .grow:hover's height to "auto" so it displays all the content it contains. Changing the height attribute made the height's transition less smooth, so I speed it up to 0.2s to compensate in this [demo](https://jsfiddle.net/gknLstpt/31/). This "fix" doesn't work well at all; is there anyway to maintain smooth transitions while handling the object's height dynamically? – plaidcorp Oct 11 '15 at 08:13
1

With CSS only, first key is "overflow:hidden", but if you fix height, all your elements expands same way, no mater containing text. If you do "height:auto", they expands right heights, but no animation supported. There is a way you do both with some little javascript, to use element.scrollHeight, that gives you correct height on all needed elements.

if (document.getElementsByClassName("auto")) {
  var autos = document.getElementsByClassName("auto");
  for (var i=0; i<autos.length; i++) {
    autos[i].addEventListener("mouseover", autoOver);
    autos[i].addEventListener("mouseout", autoOut);
  }
}

function autoOver() {
  this.style.height = this.scrollHeight + "px";
}

function autoOut() {
  this.style.height = "20px";
}
.auto {
  display: block;
  margin: 20px;
  width: 400px;
  height: 20px;
  overflow: hidden;
  transition: all .5s ease;
}
<div class="auto">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>

<div class="auto">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

<div class="auto">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
berge
  • 31
  • 2