The Wayback Machine - https://web.archive.org/web/20240905190732/https://www.geeksforgeeks.org/how-to-make-smooth-bounce-animation-using-css/
Open In App

How to make smooth bounce animation using CSS ?

Last Updated : 29 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The smooth bounce animation can be done with the help of HTML and CSS. It will generate fun and desired outputs.

For this project, a simple div with class ball is needed in HTML page:

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

We will switch to CSS for animation programming. Now, Flexbox is used to have the ball at the middle of the page and make the ball 70px by 70px in size. It can be taken in any size of the user’s choice as it determines the size of the ball.

css
 body {
  display: flex;              
  justify-content: center;   
}
.ball {
  width: 100px;
  height: 100px;
  border-radius: 50%;         
  background-color: #FF5722;  
}

Here,

justify-content: center is used to center the ball horizontally.

border-radius : 50% turns the square into a circle.

background-color: #FF5722 turns the circle to orange colour. There are various code notations for colors to be remembered.

Keyframe creating:

Keyframes in CSS animations gives complete control over the animation. Simple use of the keyword @keyframes followed by the name of the animation, i.e, smooth bounce ball:

css
@keyframes smoothbounceball{
    statements
}

Within the keyframe, use keywords from and to to make a start and end point for the animation.

css
@keyframes smoothbounceball{
    from {/*starting*/}
    to {/*ending*/}
}

To our understanding, we can add starting and ending values to the animation. To create a bouncing effect, we need to transform the location of the ball. transform allows to modify co-ordinates of a given element. Hence the final keyframe:

css
@keyframes smoothbounceball{
    from { transform: translate3d(0, 0, 0);}
    to { transform: translate3d(0, 200px, 0);}
}

Here, translate3d functions takes three inputs, the change in 3-dimensional axis (x, y, z). It will translate the ball in 3-Dimensional axes. If the ball wants to move up and down, the ball needs to translate along the y-axis.

Running the keyframe: Since the @keyframe has been created, now it needs to run. In the above mentioned code of .ball{ }, a following a line has to be added:

css
.ball{
    Given statements...

    animation: bounce 0.5s;
    animation-direction: alternate;
    animation-iteration-count: infinite;
}

The understanding of the animation: The animation tells ball element to use the given keyframe rule bounce and sets the length of the animation of 0.5 seconds. Then at finishing, the animation direction alternates. Then runs the animation an infinite number of times.

But it does not like the ball bounces but moves back and forth, up and down.

Hence, it looks like:

That is because the timing of the animation is off. Animations are set to ease, by default. So, to look like the ball is bouncing, the animation needs to be slow at the start and speeding up in the middle and then finishing slowly.

Hence bezier curve is used to customize animation timings. Therefore the code:

css
.ball{
    Given statements..

    animation: bounce 0.5s cubic-bezier(0.5, 0.05, 1, 0.5);
}

After this, the ball shows the bouncing effect. Here is the final code:

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        body {
            display: flex;
            justify-content: center;
        }

        .ball {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background-color: #FF5722;
            animation: bounce 0.5s;
            animation-direction: alternate;
            animation-timing-function: cubic-bezier(.5, 0.05, 1, .5);
            animation-iteration-count: infinite;
        }

        @keyframes bounce {
            from {
                transform: translate3d(0, 0, 0);
            }

            to {
                transform: translate3d(0, 200px, 0);
            }
        }

        /* Prefix Support */

        ball {
            -webkit-animation-name: bounce;
            -webkit-animation-duration: 0.5s;
            -webkit-animation-direction: alternate;
            -webkit-animation-timing-function: cubic-bezier(.5, 0.05, 1, .5);
            -webkit-animation-iteration-count: infinite;
        }

        @-webkit-keyframes bounce {
            from {
                -webkit-transform: translate3d(0, 0, 0);
                transform: translate3d(0, 0, 0);
            }

            to {
                -webkit-transform: translate3d(0, 200px, 0);
                transform: translate3d(0, 200px, 0);
            }
        }
    </style>
</head>

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

</html>

Output:



Similar Reads

Semantic-UI Static Bounce Animation
Semantic UI is an open-source framework that uses CSS and jQuery to build great user interfaces. It is the same as a bootstrap for use and has great different elements to use to make your website look more amazing. If we wish to transform web content in and out of the viewport, we use animations. Static animations are used to draw the user's attent
2 min read
How to create a bounce in and out on scroll effect using CSS ?
Creating a bounce in and out on the scroll effect can add an exciting and engaging touch to your web design. This effect creates a bouncing animation when the user scrolls down the page, and then bounces back when the user scrolls up. In this article, we will discuss the syntax and approaches to create this effect, followed by at least two examples
3 min read
How to create a Smooth Transition on hover using CSS ?
Creating a smooth transition on hover in CSS involves using the transition property to define the property being animated and the duration of the transition. This effect is commonly used to make changes to an element's appearance smooth and visually appealing when a user hovers over it. Syntax:/* Example of creating a smooth transition on hover */.
1 min read
Design a Progress Bar with Smooth Animations using Tailwind CSS
A progress bar is a type of component that tracks the percentage completion of a particular task and makes it visible to the users to enhance user interactivity. Steps to create the projectStep 1: Create a directory Progress Bar with files: index.html, style.css, and script.js for HTML, CSS, and JavaScript respectively. Step 2: Add the below CDN li
3 min read
How to Implement Smooth Scrolling using Tailwind CSS ?
Smooth scrolling refers to the scrolling of a section of the webpage when the respective link is clicked. By default when we click a link it skips directly to that element using smooth scrolling the UX improves a lot. Tailwind CSS provides a utility class named scroll-smooth which applies the CSS property of scroll-behaviour with a value of smooth.
2 min read
How to create a smooth scrolling effect using CSS ?
Smooth scrolling is employed on web pages to enhance the user experience by providing a visually appealing and seamless transition between different sections of the page. It improves readability and enhances navigation. Using the CSS property scroll-behaviour we can achieve the smooth scrolling effect. PreviewApproachCreate the basic structure of t
2 min read
jQuery UI Bounce Effect
In this article, we are going to show the effect of bounce using jQuery UI. All the content will show the bounce effect when the action button is clicked which actually triggers the bounce effect script. Syntax : $( ".selector" ).effect( selectedEffect, options, time(in ms), callback ); Parameters: selectedEffect: Select any effect given in jQuery
2 min read
How to Build a Bounce Ball with HTML and JavaScript ?
A bouncing ball can be created by using HTML, CSS, and JavaScript and performing some bouncing operations on that ball. You can see a related article on how to make smooth bounce animation using CSS. ApproachCanvas Setup:Create a canvas element and set its width and height to match the window dimensions.Context Initialization:Get the 2D rendering c
2 min read
Foundation CSS Smooth Scroll
Foundation CSS is an open-source &amp; responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails that look amazing &amp; can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is
5 min read
Foundation CSS Smooth Scroll Installation
Foundation CSS is an open-source and responsive front-end framework built by the ZURB foundation in September 2011, that makes it easy to layout stunning responsive websites, apps, and emails that appear amazing and can be accessible to any device. For setup/installation of the Smooth Scroll in our code just add the attribute data-smooth-scroll to
3 min read
How to Create a Smooth Scrolling Effect with CSS?
Smooth scrolling is a popular web design technique because it helps users navigate through any section of the webpage without disturbing the current flow. It enhances user experience and provides a more visually appealing interface CSS. Preview Image ApproachFirst, setting up a simple HTML document with the necessary elements, including sections or
5 min read
How to Draw Smooth Curve Through Multiple Points using JavaScript ?
In this article, we will Draw a Smooth Curve Through Multiple Points using JavaScript. Approach: Drawing a smooth curve with multiple points is a challenging task. There are many algorithms that can help us to draw a curve using particular points. Bezier Curve is one of the curve drawing techniques. There is always lots of discussion around how to
3 min read
How to set smooth scrolling to stop at a specific position from the top using jQuery ?
The scrollTop() method in jQuery is used to scroll to a particular portion of the page. Animating this method with the available inbuilt animations can make the scroll smoother. And, subtracting the specified value from it will make the scrolling to stop from the top. Approach: The hash portion of the anchor link is first extracted using the hash p
2 min read
How to set smooth scroll after clicking the link using JavaScript ?
Smooth scroll provides a seamless user experience as it provides an effective way to enhance website navigation. By implementing smooth scroll functionality, one can ensure that users transition fluidly between different sections of your webpage when they click on internal links. There are several methods to implement smooth scroll after clicking t
4 min read
How to Implement Smooth Scrolling in Action Using React-bootstrap
Smooth scrolling in React Bootstrap ensures a seamless transition between different sections of a webpage. It allows users to smoothly navigate to a specific component when they click on a particular button or tag. PrerequisiteReact JSreact-bootstrapNPM (Node Package Manager)ApproachUsing React Bootstrap's Container, Row, Col, and Button components
3 min read
How to make border popup animation for buttons using CSS ?
In this article, we will learn how to make border pop animation for buttons for your website. To create the border popup animation, we use CSS Pseudo Elements. A CSS Pseudo elements is a keyword added to a selector that lets you style to a specific part of the selected element. In order to create an animation of such kind, we need to create our web
2 min read
How to Make Vertical Card Sliding Animation using only HTML & CSS ?
The vertical card slider serves as an engaging way to showcase content, such as profiles, products, or services, in a modern web design. In this tutorial, we'll see the creation of a visually appealing vertical card slider with smooth animation effects using HTML and CSS. Approach :Firstly use the HTML to structure the cards and their content.Now a
3 min read
How to make a morph spinner animation using CSS ?
Morphing is the rotating animation of certain objects changing their shape. The changing of shape is called morphing. In this article, we will make a morph spinner that will act as a loading animation by using CSS. This method can also be used to make eye-catching card backgrounds, banners, buttons, and demo purposes of certain functions on the web
3 min read
p5.js smooth() Function
The smooth() function is an inbuilt function in p5.js library. This function is used to get smooth Amplitude analysis by averaging with the last analysis frame by default it is off. Syntax: smooth(channel) Note: All the sound-related functions only work when the sound library is included in the head section of the index.html file. Parameter: This f
1 min read
Next.js Built-in Loader Feature for Smooth Page Transitions
Next.js offers a built-in loader feature that enhances user experience by providing smooth page transitions. This feature helps to inform users that the application is loading, reducing perceived waiting times and improving overall usability. In this article, we'll explore the Next.js Loader feature by creating a "loading.js" component that will be
3 min read
How to make Card Flipping Animation in HTML & CSS ?
This animation makes web pages more fun and interesting. It's great for showing off things you want to sell or making web pages more exciting for people to use. This animation can help retain users for more time due to interaction. Here we will create a card-flipping animation on the hover of the mouse, the card will be flipped 180 degrees after th
3 min read
How to make a CTA Animation with Tailwind CSS ?
To enhance the engagement level of your website's Call-to-Action (CTA) elements we use Tailwind CSS utility classes. By directly customizing transition effects with Tailwind CSS, you can effortlessly introduce captivating animation effects to your CTAs, improving user interaction and overall user experience. ApproachIn the HTML5 document, integrate
2 min read
Underline Hover Animation Effect using Tailwind CSS
The Tailwind CSS underline animation is a visual effect that can be added to any text or heading or a specific word in the web page using the Tailwind CSS utility framework. The basic function of this effect is this creates an underline animation from left to right with a smooth transition, in simpler words it created underline animation whenever w
3 min read
Create Effect of Particle Animation using CSS
At least once, you must have seen a website with particles appearing and disappearing in its background. This cool particle animation may seem complicated but it is actually quite easy to make. In this article, we will set an image as the background of our body, and on it, we will have particles moving in zig-zag motion appearing and disappearing r
6 min read
How to create X and Y axis flip animation using HTML and CSS ?
The flip animation is the kind of loading animation in which we use a square flip effect to give the feel of loading animation. These kinds of animations are useful in times when the content of the website is taking too long to load. This animation can keep visitors engage and prevent them from leaving your web page without seeing the content. The
3 min read
How to Create Text Changing Animation Effect using CSS ?
Almost any website that we visit today uses some kind of text animation to engage with its users. Generally, all kinds of text-animations are made using plain or vanilla JavaScript or by using some third-party JavaScript libraries. But there are some animations that can be made using only CSS. One of them is changing the word text animation. In thi
3 min read
How to create text-fill animation using CSS ?
Text-fill animation on hover is a type of text-animation of modern web design concepts. In this animation, the text is filled with a color different from that of the original text-color in a particular direction i.e. left to right, right to left, top to bottom, or bottom to top. This type of animations is not limited to only text. You can use the s
3 min read
How to Create Animation Loading Bar using CSS ?
Loading Bar with animation can be created using HTML and CSS. We will create a Loader that is the part of an operating system that is responsible for loading programs and libraries. The progress bar is a graphical control element used to visualize the progression of an extended computer operation, so here we use the progress bar as an animation in
3 min read
How to Create a Dot loading Animation using HTML and CSS?
The Dot Loading animation can be used to enhance the user interface of a website, it can be added while the website loads. This animation can be easily created using HTML and CSS. HTML Code: In this section we will create the basic structure of the dot loader using a div tag which will have some span tags inside of it. &lt;!DOCTYPE html&gt; &lt;htm
2 min read
Bonfire Animation Effect using CSS
In this article, we will create a Bonfire Animation effect using CSS. Approach: Create wood logs: We can use the clip-path property and clip a polygon and apply some rotation such that it looks like one of its end is inside fire.Create flames: For flames, we are going to create 5 flames i.e. Red, Orange, Yellow, blue, and black. These flames can be
3 min read