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
  • Janma Kundali
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Artificial Intelligence (1)
  • Automobiles (12)
    • Cars (7)
  • Blog (107)
    • 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 (396)
  • 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
  • Janma Kundali
  • 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 *

Recent Posts

  • What REALLY Happens When You Type a URL and Press Enter?
  • Balen Shah UN Speech 2026: Full Text & Audio UN General Assembly
  • inDrive Provides Safe Driving Training and Certificates to 1,500+ Driver Partners in Nepal
  • NEPSE’s Data Center Faces Technical Issue: Today’s Share Trading Suspended
  • KIST Teaching Hospital Ranks First in Public Health Office’s Evaluation

Tags

adsense ai animation animation using HTML and CSS API blog calculator chatgpt clock Cryptocurrency CSS css animation custom file manager Cyber Security design Email Facebook featured filemanager free template google htaccess HTML image Instagram interview javascript JQuery NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App QR random react reactjs Rotate seo 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

  • What REALLY Happens When You Type a URL and Press Enter? Admin
  • Balen Shah UN Speech 2026: Full Text & Audio UN General Assembly Admin
  • inDrive Provides Safe Driving Training and Certificates to 1,500+ Driver Partners in Nepal Admin

Quick Links

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