Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Automobiles (12)
    • Cars (7)
  • Blog (103)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (51)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones
Close

Search

Sand Timer Animation Using HTML, CSS, and JavaScript
HTMLCSSJavaScript

How to Create a Sand Timer Animation Using HTML, CSS, and JavaScript

By Admin
November 13, 2024 3 Min Read
0

Ever wanted to create a visually engaging sand timer animation using only HTML, CSS, and JavaScript? Here’s a tutorial on how to build a simple and elegant sand timer animation. We’ll use CSS to handle all the animations, so no JavaScript is needed for this version. Let’s dive into the code and see how it all comes together.

Follow this video for complete guidance :

https://www.youtube.com/watch?v=z7H7OHs9xt4

Step 1: Setting Up the HTML Structure

The HTML structure is straightforward, consisting of a main container .sand-timer, which houses two glass sections (top and bottom) and a sand flow element. Here’s the code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sand Timer Animation</title>
  <style>
    /* CSS Code */
  </style>
</head>
<body>
  <div class="sand-timer">
    <div class="glass top">
      <div class="sand"></div>
    </div>
    <div class="glass bottom">
      <div class="sand-fill"></div>
    </div>
    <div class="sand-flow"></div>
  </div>
</body>
</html>

In the HTML:

  • The .sand-timer div is the container for the animation.
  • .glass.top and .glass.bottom are the top and bottom sections of the hourglass.
  • .sand represents the sand in the top glass, .sand-fill represents sand filling the bottom, and .sand-flow simulates sand pouring down the hourglass.

Step 2: Adding CSS for Styling and Animation

Now, let’s dive into the CSS that styles each element and adds the animations.

body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
  background-color: #2d3436;
}

.sand-timer {
  width: 80px;
  height: 200px;
  position: relative;
}
.glass {
  width: 80px;
  height: 100px;
  position: absolute;
  overflow: hidden;
  border: 4px solid #dfe6e9;
}

.glass.top {
  top: 0;
  border-radius: 50% 50% 0 0;
}

.glass.bottom {
  top: 100px;
  transform: rotate(180deg);
  border-radius: 0 0 50% 50%;
}
.sand {
  width: 100%;
  height: 100%;
  background-color: #f39c12;
  animation: sand-drop 4s linear infinite;
}
.sand-fill {
  width: 100%;
  height: 0;
  background-color: #f39c12;
  animation: sand-fill 4s linear infinite;
}
.sand-flow {
  width: 8px;
  height: 80px;
  background-color: #f39c12;
  position: absolute;
  top: 60px;
  left: 36px;
  clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
  animation: sand-flow 4s linear infinite;
}
@keyframes sand-drop {
  0% { height: 100%; }
  100% { height: 0%; }
}
@keyframes sand-fill {
  0% { height: 0%; }
  100% { height: 100%; }
}
@keyframes sand-flow {
  0%, 100% { height: 0; }
  50% { height: 80px; }
}

Final Output

With the CSS styles applied to each element and animations set up, we have a functional sand timer animation. Here’s the complete code in a single file:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sand Timer Animation</title>
  <style>
    body {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
      background-color: #2d3436;
    }

    .sand-timer {
      width: 80px;
      height: 200px;
      position: relative;
    }

    .glass {
      width: 80px;
      height: 100px;
      position: absolute;
      overflow: hidden;
      border: 4px solid #dfe6e9;
    }

    .glass.top {
      top: 0;
      border-radius: 50% 50% 0 0;
    }

    .glass.bottom {
      top: 100px;
      transform: rotate(180deg);
      border-radius: 0 0 50% 50%;
    }

    .sand {
      width: 100%;
      height: 100%;
      background-color: #f39c12;
      animation: sand-drop 4s linear infinite;
    }

    .sand-fill {
      width: 100%;
      height: 0;
      background-color: #f39c12;
      animation: sand-fill 4s linear infinite;
    }

    .sand-flow {
      width: 8px;
      height: 80px;
      background-color: #f39c12;
      position: absolute;
      top: 60px;
      left: 36px;
      clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
      animation: sand-flow 4s linear infinite;
    }

    @keyframes sand-drop {
      0% { height: 100%; }
      100% { height: 0%; }
    }

    @keyframes sand-fill {
      0% { height: 0%; }
      100% { height: 100%; }
    }

    @keyframes sand-flow {
      0%, 100% { height: 0; }
      50% { height: 80px; }
    }
  </style>
</head>
<body>
  <div class="sand-timer">
    <div class="glass top">
      <div class="sand"></div>
    </div>
    <div class="glass bottom">
      <div class="sand-fill"></div>
    </div>
    <div class="sand-flow"></div>
  </div>
</body>
</html>

Using CSS animations, we’ve created a visually appealing sand timer. This code can be customized further by adjusting animation durations, colors, or even adding additional effects. Have fun experimenting and creating your unique animations!

Tags:

animationsand timer
Author

Admin

Follow Me
Other Articles
Everything you need to know for running a YouTube Channel
Previous

Everything you need to know to run a YouTube Channel

3d maze game using javascript
Next

3D Maze Game Using HTML and JavaScript

No Comment! Be the first one.

Leave a Reply

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

FIFA World Cup 2026 Predict and Win by SportsGuff

Recent Posts

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation
  • How to Design a Student Marksheet Using HTML and CSS

Tags

adsense ai animate animation animation using HTML and CSS API blog calculator chatgpt Cryptocurrency CSS css animation design Email Facebook featured filemanager file manager free template google htaccess HTML image Instagram interview javascript JQuery jquery ui NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App PWA QR random react reactjs Rotate travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You Admin
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide Admin
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme