Search code examples
htmlcssexpand

How to expand a div and its contents on hover?


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


Solution

  • 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>