Search code examples
htmlcssclicktouch

CSS Touch Ripple Effect, Not Working Properly


My CSS touch ripple is not working properly. It is just seen like a bubble in the center when i click on the button. Please help in finding where am i doing the mistake. i am not much known to CSS animations. i would like to do this using just CSS only.

.ripple-con,
.button {
  display: inline-block;
  position: relative;
  overflow: hidden;
}

.button::after,
.ripple {
  content: ' ';
  background: rgba(0, 0, 0, 0.3);
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  display: inline-block;
  border-radius: 100%;
}

.button:active::after,
.ripple:active,
.button:active+.ripple {
  animation: ripple 2s;
}

@keyframes ripple {
  to {
    opacity: 0;
    margin: -250px;
    width: 500px;
    height: 500px;
  }
}

.button {
  line-height: 39px;
  border: 0;
  height: 42px;
  box-sizing: border-box;
  padding: 0 20px;
  background: #888;
  border-radius: 5px;
  cursor: default;
  vertical-align: top;
}

html {
  text-align: center
}

body {
  display: inline-block
}
<div class="ripple-con">
  <input class="button" type="submit" value="Submit Button : Button">
  <span class="ripple"></span>
</div>

<div class="ripple-con">
  <input class="button" type="button" value="Input Button : Button">
  <span class="ripple"></span>
</div>

<div class="button">Div : Button</div>

<button class="button">Button : Button</button>


Solution

  • Just place animation: ripple inside .button::after, .ripple style definition and place animation: none where it goes active as follows

    .ripple-con,
    .button {
      display: inline-block;
      position: relative;
      overflow: hidden;
    }
    
    .button::after,
    .ripple {
      content: ' ';
      background: rgba(0, 0, 0, 0.3);
      position: absolute;
      top: 50%;
      left: 50%;
      width: 0;
      height: 0;
      display: inline-block;
      border-radius: 100%;
      animation: ripple 2s;
    }
    
    .button:active::after,
    .ripple:active,
    .button:active + .ripple {
      animation: none;
    }
    
    @keyframes ripple {
      to {
        opacity: 0;
        margin: -250px;
        width: 500px;
        height: 500px;
      }
    }
    
    .button {
      line-height: 39px;
      border: 0;
      height: 42px;
      box-sizing: border-box;
      padding: 0 20px;
      background: #888;
      border-radius: 5px;
      cursor: default;
      vertical-align: top;
    }
    
    html {
      text-align: center
    }
    
    body {
      display: inline-block
    }
    <div class="ripple-con">
      <input class="button" type="submit" value="Submit Button : Button">
      <span class="ripple"></span>
    </div>
    
    <div class="ripple-con">
      <input class="button" type="button" value="Input Button : Button">
      <span class="ripple"></span>
    </div>
    
    <div class="button">Div : Button</div>
    
    <button class="button">Button : Button</button>

    Here is the fiddle