Bouncing ball animation Html CSS

Hello, in this lesson we will learn how to create a Bouncing ball animation Html CSS. In the last post, we created a CSS waves effect and CSS animation. Let’s create bouncing balls.

Bouncing ball Html CSS with code:

These balls are bouncing with different bricks and it’s going infinite. These are different animations in Html and CSS and we will create them using HTML and CSS only.

How to create bouncing ball animation HTML CSS?

As you can see below image the bouncing and animated balls are shown. There is a multiple-step code line that is implemented for its development. First of all, we have taken the objects. Then provided a proper shape to all of these objects. These objects have different shapes and colors. Every object is different from another object. Then we provided the animations for these objects. These objects are constantly moving from one to another place. While moving they are coming across each other as well. In this way, they are bouncing back after coming in contact. 

To this effect, we have provided a solid dark background. This background is replaceable with gradient color, image, and video as well according to users’ requirements.

You might like it:

Bouncing Ball
Bouncing Ball

Source code of Html:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>FantacyDesigns | Bouncing Ball </title>
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
    <div class="fall">

    <div class="walls">
      <div class="wall"><div class="top"></div><div class="bottom"></div><div class="left"></div><div class="right"></div><div class="ceil"></div></div>
      <div class="wall"><div class="top"></div><div class="bottom"></div><div class="left"></div><div class="right"></div><div class="ceil"></div></div>
      <div class="wall"><div class="top"></div><div class="bottom"></div><div class="left"></div><div class="right"></div><div class="ceil"></div></div>
      <div class="wall"><div class="top"></div><div class="bottom"></div><div class="left"></div><div class="right"></div><div class="ceil"></div></div>
      <div class="wall"><div class="top"></div><div class="bottom"></div><div class="left"></div><div class="right"></div><div class="ceil"></div></div>
      <div class="wall"><div class="top"></div><div class="bottom"></div><div class="left"></div><div class="right"></div><div class="ceil"></div></div>
      <div class="wall"><div class="top"></div><div class="bottom"></div><div class="left"></div><div class="right"></div><div class="ceil"></div></div>
      <div class="wall"><div class="top"></div><div class="bottom"></div><div class="left"></div><div class="right"></div><div class="ceil"></div></div>
      <div class="wall"><div class="top"></div><div class="bottom"></div><div class="left"></div><div class="right"></div><div class="ceil"></div></div>
      <div class="wall"><div class="top"></div><div class="bottom"></div><div class="left"></div><div class="right"></div><div class="ceil"></div></div>
    </div>

    <div class="ballArm">
      <div class="ball"></div>
    </div>
  </div>

  </body>
</html>

Source code of CSS:

CSS is also a markup language and we use it to design and animate our project using CSS properties. We have created bricks and balls in Html.

Don’t forget to save HTML and CSS files with the extensions of .html and .css and must link them with each other. These extensions will help the browser run the files and read the files easily.

*, *::before, *::after {
  padding: 0;
  margin: 0 auto;
  box-sizing: border-box;
}

body {
  background-color: #111;
  color: #fff;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  perspective: 800px;
  overflow: hidden;
}

.fall {
  position: relative;
  transform-style: preserve-3d;
  -webkit-animation: fallRotate 42s infinite linear;
          animation: fallRotate 42s infinite linear;
}
@-webkit-keyframes fallRotate {
  to {
    transform: rotateY(360deg);
  }
}
@keyframes fallRotate {
  to {
    transform: rotateY(360deg);
  }
}

.walls {
  transform-style: preserve-3d;
}

.wall {
  position: absolute;
  width: 80px;
  height: 80px;
  transform-style: preserve-3d;
  background-image: linear-gradient(#0007, #000c);
  -webkit-animation: move 15s infinite linear;
          animation: move 15s infinite linear;
  bottom: -210px;
}
.wall:nth-child(odd) {
  left: -10px;
  transform: translate(-50%, 50%) rotateY(90deg) rotateX(45deg) translatez(-20px) translateY(-220px);
}
.wall:nth-child(even) {
  left: 10px;
  transform: translate(-50%, 50%) rotateY(-90deg) rotateX(45deg) translatez(-20px) translateY(-220px);
}
.wall:nth-child(1) {
  -webkit-animation-delay: -1.5s;
          animation-delay: -1.5s;
  background-color: #ef8f8f;
}
.wall:nth-child(2) {
  -webkit-animation-delay: -3s;
          animation-delay: -3s;
  background-color: #efc98f;
}
.wall:nth-child(3) {
  -webkit-animation-delay: -4.5s;
          animation-delay: -4.5s;
  background-color: #dcef8f;
}
.wall:nth-child(4) {
  -webkit-animation-delay: -6s;
          animation-delay: -6s;
  background-color: #a3ef8f;
}
.wall:nth-child(5) {
  -webkit-animation-delay: -7.5s;
          animation-delay: -7.5s;
  background-color: #8fefb6;
}
.wall:nth-child(6) {
  -webkit-animation-delay: -9s;
          animation-delay: -9s;
  background-color: #8fefef;
}
.wall:nth-child(7) {
  -webkit-animation-delay: -10.5s;
          animation-delay: -10.5s;
  background-color: #8fb6ef;
}
.wall:nth-child(8) {
  -webkit-animation-delay: -12s;
          animation-delay: -12s;
  background-color: #a38fef;
}
.wall:nth-child(9) {
  -webkit-animation-delay: -13.5s;
          animation-delay: -13.5s;
  background-color: #dc8fef;
}
.wall:nth-child(10) {
  -webkit-animation-delay: -15s;
          animation-delay: -15s;
  background-color: #ef8fc9;
}
@-webkit-keyframes move {
  from {
    bottom: -1210px;
  }
  to {
    bottom: 810px;
  }
}
@keyframes move {
  from {
    bottom: -1210px;
  }
  to {
    bottom: 810px;
  }
}
.wall > div {
  position: absolute;
  background-color: inherit;
}
.wall .ceil {
  width: 80px;
  height: 80px;
  background-image: linear-gradient(#fff7, #fff0);
  -webkit-animation: wallCeil 15s infinite linear;
          animation: wallCeil 15s infinite linear;
  -webkit-animation-delay: inherit;
          animation-delay: inherit;
  overflow: hidden;
}
.wall .ceil::after {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: radial-gradient(#000, #0000 50%);
  -webkit-animation: shadow 15s infinite linear;
          animation: shadow 15s infinite linear;
  -webkit-animation-delay: inherit;
          animation-delay: inherit;
}
@-webkit-keyframes shadow {
  0%, 48%, 53%, 100% {
    opacity: 0;
    transform: translateY(80px) scale(2);
  }
  50% {
    opacity: 0.25;
    transform: translateY(4px) scale(0.5);
  }
}
@keyframes shadow {
  0%, 48%, 53%, 100% {
    opacity: 0;
    transform: translateY(80px) scale(2);
  }
  50% {
    opacity: 0.25;
    transform: translateY(4px) scale(0.5);
  }
}
.wall .top {
  width: 80px;
  transform: rotateX(90deg);
  transform-origin: top;
  background-image: linear-gradient(#0007, #fff7);
  -webkit-animation: wallHeight 15s infinite linear;
          animation: wallHeight 15s infinite linear;
  -webkit-animation-delay: inherit;
          animation-delay: inherit;
}
.wall .bottom {
  bottom: 0;
  width: 80px;
  transform: rotateX(-90deg);
  transform-origin: bottom;
  background-image: linear-gradient(#fff0, #000c);
  -webkit-animation: wallHeight 15s infinite linear;
          animation: wallHeight 15s infinite linear;
  -webkit-animation-delay: inherit;
          animation-delay: inherit;
}
.wall .left {
  bottom: 0;
  height: 80px;
  transform: rotateY(-90deg);
  transform-origin: left;
  background-image: linear-gradient(to bottom left, #fff3, #000c);
  -webkit-animation: wallWidth 15s infinite linear;
          animation: wallWidth 15s infinite linear;
  -webkit-animation-delay: inherit;
          animation-delay: inherit;
}
.wall .right {
  bottom: 0;
  right: 0;
  height: 80px;
  transform: rotateY(90deg);
  transform-origin: right;
  background-image: linear-gradient(to bottom right, #fff3, #000c);
  -webkit-animation: wallWidth 15s infinite linear;
          animation: wallWidth 15s infinite linear;
  -webkit-animation-delay: inherit;
          animation-delay: inherit;
}
@-webkit-keyframes wallCeil {
  0%, 49.75%, 55%, 100% {
    transform: translateZ(20px);
  }
  50% {
    transform: translateZ(10px);
  }
}
@keyframes wallCeil {
  0%, 49.75%, 55%, 100% {
    transform: translateZ(20px);
  }
  50% {
    transform: translateZ(10px);
  }
}
@-webkit-keyframes wallHeight {
  0%, 49.75%, 55%, 100% {
    height: 20px;
  }
  50% {
    height: 10px;
  }
}
@keyframes wallHeight {
  0%, 49.75%, 55%, 100% {
    height: 20px;
  }
  50% {
    height: 10px;
  }
}
@-webkit-keyframes wallWidth {
  0%, 49.75%, 55%, 100% {
    width: 20px;
  }
  50% {
    width: 10px;
  }
}
@keyframes wallWidth {
  0%, 49.75%, 55%, 100% {
    width: 20px;
  }
  50% {
    width: 10px;
  }
}

.ballArm {
  position: absolute;
  bottom: -210px;
  width: 20px;
  height: 240px;
  transform-origin: bottom;
  transform-style: preserve-3d;
  -webkit-animation: armRotate 1.5s infinite linear alternate;
          animation: armRotate 1.5s infinite linear alternate;
}
@-webkit-keyframes armRotate {
  from {
    transform: translateX(-50%) rotate(-45deg);
  }
  to {
    transform: translateX(-50%) rotate(45deg);
  }
}
@keyframes armRotate {
  from {
    transform: translateX(-50%) rotate(-45deg);
  }
  to {
    transform: translateX(-50%) rotate(45deg);
  }
}

.ball {
  position: absolute;
  width: 20px;
  height: 20px;
  border-radius: 10%;
  transform-style: preserve-3d;
  -webkit-animation: ballRotateZ 1.5s infinite linear alternate;
          animation: ballRotateZ 1.5s infinite linear alternate;
}
@-webkit-keyframes ballRotateZ {
  from {
    transform: rotate(45deg);
  }
  to {
    transform: rotate(-45deg);
  }
}
@keyframes ballRotateZ {
  from {
    transform: rotate(45deg);
  }
  to {
    transform: rotate(-45deg);
  }
}
.ball::after {
  content: "";
  position: absolute;
  width: 30px;
  height: 30px;
  background-image: radial-gradient(circle at top, #fff, #222);
  border-radius: 50%;
  -webkit-animation: ballRotateY 42s infinite linear;
          animation: ballRotateY 42s infinite linear;
}
@-webkit-keyframes ballRotateY {
  from {
    transform: rotateY(0deg);
  }
  to {
    transform: rotateY(-360deg);
  }
}
@keyframes ballRotateY {
  from {
    transform: rotateY(0deg);
  }
  to {
    transform: rotateY(-360deg);
  }
}

Download source code:

We always provide the source code of every project and its source code is always present you can download it from the download button. It helps the newbies to read the code and understand it without creating the files of HTML and CSS manually and linking them. In this way, you can create this type of project easily.

Task:

You have learned how to create bouncing ball animation in HTML and CSS. Now modify these bouncing balls. Change the colors of each object. Then increase the speed of all these objects. Animation speed needs to be increased for learning purposes. Change the background color with an image using <img? Tag HTML and set it as background.

If you have any type of query then quote it in the comment section.

Thanks for reading this article.

[su_divider divider_color=”#1DBF73″ link_color=”#1DBF73″]

[su_button id=”download” url=”https://mega.nz/file/lhsWBRZD#4WfJmQ0Q93V9GkpguQS9bMIPpPtSKl8wWKi_libx630″ target=”blank” style=”3d” background=”#1DBF73″ size=”6″ radius=”5″ icon=”icon: download” desc=”Click to Download”]Download Source Code[/su_button]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top