Css - Expand Button Width Smoothly and Display Text on Hover

I was trying to make a button animation on hover states where initial state the button will show with an icon only and after hover on it will expand the width and then display the text inside of it.

I can't add predefined max-width or width or width value as I don't know what would be the length of the text as is rendered from a back end service

I had added some CSS code on it but it's not working properly. The flow what I need like -

  1. Initial state display button with icon
  2. After hovering the width of the button will expand up to the text length
  3. Then show the text inside the button
.sti_container{
  position: relative;
}

.btn{
  position: relative;
  display: inline-block;
  padding: 15px;
  background-color: blue;
  cursor: pointer;
  outline: none;
  border: 0;
  vertical-align: middle;
  text-decoration: none;
  color: #fff;
  border-radius: 25px;
  -webkit-transition: width 0.5s;
  transition: width 0.5s;
}
.btn .btn-text{
  width: 0;
  display: inline-block;
  -webkit-transition: color 2s;
  transition: color 2s;
  vertical-align: top;
  white-space: nowrap;
  overflow: hidden;
  color: blue;
}
.btn:hover .btn-text{
  width: auto;
  color: white;
}
<link href="" rel="stylesheet"/>
<div class="sti_container">
  <button class="btn">
    <span class="btn-icon"><i class="fa fa-shopping-bag" aria-hidden="true"></i></span>
    <span class="btn-text">Shop the image here to click and open</span>
  </button>
</div>

1 Answer

Width auto values can't be animated, so use max-width instead:

.sti_container {
  position: relative;
}

.btn {
  position: relative;
  display: inline-block;
  padding: 15px;
  background-color: blue;
  cursor: pointer;
  outline: none;
  border: 0;
  vertical-align: middle;
  text-decoration: none;
  color: #fff;
  border-radius: 25px;
  -webkit-transition: width 0.5s;
  transition: width 0.5s;
}

.btn .btn-text {
  max-width: 0;
  display: inline-block;
  -webkit-transition: color .25s 1.5s, max-width 2s;
  transition: color .25s 1.5s, max-width 2s;
  vertical-align: top;
  white-space: nowrap;
  overflow: hidden;
  color: blue;
}

.btn:hover .btn-text {
  max-width: 300px;
  color: white;
}
<link href="" rel="stylesheet" />
<div class="sti_container">
  <button class="btn">
    <span class="btn-icon"><i class="fa fa-shopping-bag" aria-hidden="true"></i></span>
    <span class="btn-text">Shop the image here to click and open</span>
  </button>
</div>

The other problem is that you animating only color, so you need to change your transition property to transition: color 2s, max-width 2s;

6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest