Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add CSS shaking animation to a button without hover

Tags:

html

css

I am trying to make the button shake without hovering on it. When I am removing the "hover" element, the button doesn't shake at all. What should I change in the code to make it work?

.first_button:hover {
  animation: shake 0.82s cubic-bezier(.36, .07, .19, .97) both;
  transform: translate3d(0, 0, 0);
  backface-visibility: hidden;
  perspective: 1000px;
}

@keyframes shake {
  10%,
  90% {
    transform: translate3d(-1px, 0, 0);
  }
  20%,
  80% {
    transform: translate3d(2px, 0, 0);
  }
  30%,
  50%,
  70% {
    transform: translate3d(-4px, 0, 0);
  }
  40%,
  60% {
    transform: translate3d(4px, 0, 0);
  }
}
<button class="first_button">Button</button>
like image 239
Lidor Amrani Avatar asked Oct 26 '25 03:10

Lidor Amrani


1 Answers

Use the infinite prop for your animation.

More info on animation syntax

.first_button {
  animation: shake 0.82s cubic-bezier(.36, .07, .19, .97) both infinite;
  transform: translate3d(0, 0, 0);
  backface-visibility: hidden;
  perspective: 1000px;
}

@keyframes shake {
  10%,
  90% {
    transform: translate3d(-1px, 0, 0);
  }
  20%,
  80% {
    transform: translate3d(2px, 0, 0);
  }
  30%,
  50%,
  70% {
    transform: translate3d(-4px, 0, 0);
  }
  40%,
  60% {
    transform: translate3d(4px, 0, 0);
  }
}
<button class="first_button">Button</button>
like image 124
Nidhin Joseph Avatar answered Oct 28 '25 18:10

Nidhin Joseph