Create an index.html File:
- Open your text editor (e.g., VSCode, Sublime Text, or Notepad++).
- Create a new file and save it as index.html inside your project folder.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bouncing Ball Animation</title>
</head>
<style>
body{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.ball{
width: 50px;
height: 50px;
background-color: #ff5733;
border-radius: 50%;
animation: bounce 2s infinite
ease-in-out;
position: relative;
}
@keyframes bounce{
0%, 100%{
top: 0;
}
50%{
top: 300px;
}
}
</style>
<body>
<div class="ball"></div>
</body>
</html>HTML Structure:
Thank you for reading this post, don't forget to subscribe!- A simple HTML structure with a single
divelement that represents the ball.
CSS Styling:
- The
bodyelement is styled to center the ball both vertically and horizontally usingflexbox. - The
.ballclass is given specific dimensions, a background color, and rounded corners to make it a circle. - The
animationproperty is used to apply thebounceanimation to the ball. This animation will run indefinitely (infinite) with a duration of 2 seconds and anease-in-outtiming function. - The
@keyframesrule defines thebounceanimation, specifying how the element should change at different points during the animation cycle. At 0% and 100%, the ball is at the top (starting position), and at 50%, the ball is at the bottom (300px down).