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:
Source code of Html:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<!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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
*, *::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.
Download Source CodeClick to Download