How to make Floating animation in HTML CSS

Suyash Vashishtha - Dec 26 '21 - - Dev Community

Animations are one of the most attractive things when it comes to Websites. Even having some little minimalistic animations can make your website much more soothing and attractive.

In this blog, we will see how to we can make a little floating animation in HTML CSS only.

Example-
Floating image demo

This gif is from my React JS OTT webapp Link here :)

Looks cool enough for loading screens right ? You can also use it for some on page element animations.
``

Let's jump into Development part !

- HTML

Put a simple image inside of a div. This image will be the one we are going to make a float.

- CSS

Design your parent div with a flex (recommended way but totally optional).
Then make a @keyframe with an animation -

@keyframes floater {
  0%{transform: translateY(-10%);transition: ease 0.5s;}
  50%{transform: translateY(10%);transition: ease 0.5s;}
}

Now connect your animation with you're image class. And put default translateY to -10% and transition ease 0.5s for smooth animation

.float-area{
  height:100vh;
  display: flex;
  flex-direction:column;
  align-items: center;
  justify-content: center;
}

.floating-img{
  transform: translateY(-10%);
  animation: floater 1.5s infinite;
  transition: ease 0.5s;
  width: 80px;
  height:80px;
}

Source code -

And Poof ! You're image is floating like a charm !

Now go on and play with the values and make it more interesting according to your needs :)

Thanks for reading my blog! Please drop a Like and Comment if you found it useful

. . . . . . . . . . . . .
Terabox Video Player