Select Page

CSS & HTML water animation

by | May 16, 2023

In this blog post, we explore the fascinating world CSS & HTML water animation. We’ll dive into the code and break down how to create an engaging water animation effect using CSS. By combining key CSS properties and animation techniques, we’ll bring a sense of fluidity and movement to the screen. Whether you’re a beginner or an experienced developer, this tutorial will provide you with the knowledge and inspiration to add captivating weather animations to your web projects. Join us as we uncover the secrets behind this mesmerizing water animation and learn how to apply it to your own HTML and CSS creations.

Create water animation using CSS & HTML

<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="index.css">
</head>

<body>
    <div class="content">
        <h2>Water</h2>
        <h2>Water</h2>
    </div>
</body>

</html>

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: sans-serif;
}

body{
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #212121;
}

.content{
    position: relative;
}

.content h2{
    position: absolute;
    color: lightsteelblue;
    transform: translate(-50%, -50%);
    font-size: 8em;
}

.content h2:nth-child(1){
    color:transparent;
    -webkit-text-stroke: 2px lightsteelblue;
}

.content h2:nth-child(2){
    color:lightsteelblue;
    animation: animate 3s ease-in-out infinite;
}

@keyframes animate {
    0%, 100% {
        clip-path: polygon(0% 45%, 15% 44%, 32% 50%, 54% 60%, 70% 61%, 84% 59%, 100% 52%, 100% 100%, 0% 100%);
    }
    50% {
        clip-path: polygon(0% 60%, 16% 65%, 34% 66%, 51% 62%, 67% 50%, 84% 45%, 100% 46%, 100% 100%, 0% 100%);
    }
}

CSS code explained

Here’s a brief explanation of the purpose and significance of each key CSS property used in the animation:

  1. *{ margin: 0; padding: 0; box-sizing: border-box; font-family: sans-serif; }
    • Purpose: Sets global CSS rules for all elements on the page.
    • Significance:
      • margin: 0; padding: 0;: Resets margin and padding to remove default spacing.
      • box-sizing: border-box;: Ensures that the declared width and height of an element include padding and border.
      • font-family: sans-serif;: Sets the default font family to sans-serif for all elements.
  2. body{ display: flex; justify-content: center; align-items: center; background-color: #212121; }
    • Purpose: Styles the body element.
    • Significance:
      • display: flex;: Enables flexbox layout for the body.
      • justify-content: center; align-items: center;: Centers the content both horizontally and vertically.
      • background-color: #212121;: Sets the background color of the body to dark gray.
  3. .content{ position: relative; }
    • Purpose: Styles a container div with the class ‘content.’
    • Significance:
      • position: relative;: Positions the div relative to its normal position.
  4. .content h2{ position: absolute; color: lightsteelblue; transform: translate(-50%, -50%); font-size: 8em; }
    • Purpose: Styles the heading elements inside the ‘content’ div.
    • Significance:
      • position: absolute;: Positions the headings absolutely within the content div.
      • color: lightsteelblue;: Sets the color of the headings to lightsteelblue.
      • transform: translate(-50%, -50%);: Centers the headings both vertically and horizontally using a transform.
      • font-size: 8em;: Sets the font size of the headings to 8em (adjustable).
  5. .content h2:nth-child(1){ color: transparent; -webkit-text-stroke: 2px lightsteelblue; }
    • Purpose: Selects the first heading element inside ‘content.’
    • Significance:
      • color: transparent;: Makes the text transparent, creating an outline effect.
      • -webkit-text-stroke: 2px lightsteelblue;: Adds a 2-pixel lightsteelblue stroke to the text.
  6. .content h2:nth-child(2){ color: lightsteelblue; animation: animate 3s ease-in-out infinite; }
    • Purpose: Selects the second heading element inside ‘content.’
    • Significance:
      • color: lightsteelblue;: Sets the color of the second heading to lightsteelblue.
      • animation: animate 3s ease-in-out infinite;: Applies the ‘animate’ animation to the second heading with a duration of 3 seconds, ease-in-out timing function, and infinite repeat.
  7. @keyframes animate { ... }
    • Purpose: Defines the keyframes for the ‘animate’ animation.
    • Significance:
      • Keyframes specify how the animation evolves over time.
      • Two keyframes (0% and 50%) are defined to create a water-like animation using the clip-path property.
    • Additional Explanation for @keyframes animate:
      • 0%, 100% Keyframe:
        • Defines the initial and final state of the animation.
        • Uses clip-path to create a polygon shape outlining the water animation.
      • 50% Keyframe:
        • Defines the middle state of the animation.
        • Alters the clip-path property to create a different polygon shape, simulating a wave-like appearance.
      • Infinite Animation:
        • The animation is set to repeat infinitely, creating a continuous water animation effect.

These CSS properties collectively contribute to the creation of a visually appealing water animation on the web page. Adjustments to these properties can lead to variations in the animation’s appearance

Conclusion

When the HTML file is loaded in a browser and linked with the CSS file, the result is an animated water effect where the second heading appears to be flowing like water within a defined clip path. The first heading provides an outline stroke effect, enhancing the water-like appearance.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Looking For Something?

Follow Us

Related Articles

How to Open Links in a New Tab Using HTML and JavaScript

How to Open Links in a New Tab Using HTML and JavaScript

Introduction How to Open Links in a New Tab Using HTML and JavaScript Have you ever clicked on a link and wished it would open in a new tab instead of navigating away from the current page? Well, you're in luck! In this blog post, we'll guide you through the simple...

Namecheap: More Than Just a Domain Registrar

Namecheap: More Than Just a Domain Registrar

Introduction In the vast digital landscape of the internet, one thing is for certain: your online presence begins with a memorable domain name. When it comes to domain registration and hosting services, Namecheap stands out as a top choice for individuals and...

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!