fda3r
fda3r

Reputation: 13

Maintain CSS button size when hovering

I'm trying to make a button when hovered, an animation plays. Color scrolls up from the bottom and the word is changed from "Click Me" to "Go". I've tried it with a button, without a span, and so forth. I think the issue is in the position:absolute of the hover.

Currently, when you hover, the button shrinks and gets twitchy. I want the button to remain the same size throughout.

body {
  width: 100vw;
  height: 100vh;
  background-color: #5D5D5D;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

a {
  background-color: red;
  border: none;
  border-radius: 10px;
  color: white;
  padding: 16px 64px;
  font-size: 18px;
  cursor: pointer;
  text-decoration: none;
}

.hvr-sweep-to-top {
  display: inline-block;
  vertical-align: middle;
  transform: perspective(1px) translateZ(0);
  box-shadow: 0 0 1px rgba(0, 0, 0, 0);
  position: relative;
  transition-property: color;
  transition-duration: 0.3s;
}

.hvr-sweep-to-top:before {
  border-radius: 10px;
  content: "GO";
  text-align: center;
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: green;
  transform: scaleY(0);
  transform-origin: 50% 100%;
  transition-property: transform;
  transition-duration: 0.3s;
  transition-timing-function: ease-out;
}

.hvr-sweep-to-top:hover:before, .hvr-sweep-to-top:focus:before, .hvr-sweep-to-top:active:before {
  transform: scaleY(1);
}

a:hover span {
  display: none;
}
<a href="#" class="hvr-sweep-to-top">
  <span>Click Me</>
</a>

Upvotes: 0

Views: 1309

Answers (2)

jmrker
jmrker

Reputation: 508

Easiest change might be this one line difference:

.hvr-sweep-to-top {
  display: inline-block;
  vertical-align: middle;
  transform: perspective(1px) translateZ(0);
  box-shadow: 0 0 1px rgba(0, 0, 0, 0);
  position: relative;
  transition-property: color;
  transition-duration: 0.3s;
  height: 3em;  width: 5em;   /* modify to needs */
}

Upvotes: 0

Minal Chauhan
Minal Chauhan

Reputation: 6148

Remove a:hover span this css and add z-index:1 in :before

body {
  width: 100vw;
  height: 100vh;
  background-color: #5D5D5D;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

a {
  background-color: red;
  border: none;
  border-radius: 10px;
  color: white;
  padding: 16px 64px;
  font-size: 18px;
  cursor: pointer;
  text-decoration: none;
}

.hvr-sweep-to-top {
  display: inline-block;
  vertical-align: middle;
  transform: perspective(1px) translateZ(0);
  box-shadow: 0 0 1px rgba(0, 0, 0, 0);
  position: relative;
  transition-property: color;
  transition-duration: 0.3s;
}

.hvr-sweep-to-top:before {
  border-radius: 10px;
  content: "GO";
  text-align: center;
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: green;
  line-height: 53px;
  transform: scaleY(0);
  transform-origin: 50% 100%;
  transition-property: transform;
  transition-duration: 0.3s;
  transition-timing-function: ease-out;
}

.hvr-sweep-to-top:hover:before, .hvr-sweep-to-top:focus:before, .hvr-sweep-to-top:active:before {
  transform: scaleY(1);
}

/*a:hover span {
  display: none;
}*/
<a href="#" class="hvr-sweep-to-top">
    <span>Click Me</span>
</a>

Upvotes: 1

Related Questions