DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for How to make Floating animation in HTML CSS
Suyash Vashishtha
Suyash Vashishtha

Posted on

How to make Floating animation in HTML CSS

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

Top comments (7)

Collapse
 
shanda0409 profile image
Shanda Anny

Thanks a lot
it's really amazing and helpful

Collapse
 
fistelectro profile image
ElectroFist

Was looking for this, 🀧Schools Realllyyyy need to update their syllabus. Thanks Teacher

Collapse
 
suyashvash profile image
Suyash Vashishtha

β™₯️β™₯️ Glad you liked it bro.

Collapse
 
amircahyadi profile image
Amir-cahyadi

πŸ‘

Collapse
 
suyashvash profile image
Suyash Vashishtha

Glad you liked It.

Collapse
 
suyashvash profile image
Suyash Vashishtha

Thanks for such a wonderful comment. 😊β™₯️

Some comments have been hidden by the post's author - find out more

Super Useful CSS Resources

A collection of 70 hand-picked, web-based tools which are actually useful.
Each will generate pure CSS without the need for JS or any external libraries.