CountdownTutorials

how to make a countdown timer in html code

how to make a countdown timer in html code

how to make a countdown timer in html code

Before We Start

how to make a countdown timer in html code. Websites have timers which show the accurate time that can be used for any special event and coming soon things by using a countdown.

If there is a countdown in a website, it takes more attention and it highlights to the users the exact time the event is on and gives them a better experience.It has the remaining dates so the users can get a perfect idea than showing just the date.

how to make a countdown timer in html code. So if you are a person who wishes to develop a website, it will be so wonderful if you know how to create a countdown too. Do you know how to create a countdown perfectly?

We are here to solve your problem.

However in this article you are going to learn, how to develop a countdown completely.

Before we start please read the below articles which easier you to understand the process, if you are new to development.

Step 1 – Add HTML

It’s too easy and simple. Just copy and paste below code in your HTML editor between <body> </body> tag.

<body>
<div class="container">
  <h1 id="headline">Countdown to my birthday:</h1>
  <div id="countdown">
    <ul>
      <li><span id="days"></span>days</li>
      <li><span id="hours"></span>Hours</li>
      <li><span id="minutes"></span>Minutes</li>
      <li><span id="seconds"></span>Seconds</li>
    </ul>
  </div>
  <div class="message">
    <div id="content">
      <span class="emoji">🥳</span>
      <span class="emoji">🎉</span>
      <span class="emoji">🎂</span>
    </div>
  </div>
</div>
</body>

Step 2 – Add CSS

Copy and paste below code in your HTML editor between <style></style> tag.

<style>
/* general styling */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  align-items: center;
  background-color: #ffd54f;
  display: flex;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
}

.container {
  color: #333;
  margin: 0 auto;
  text-align: center;
}

h1 {
  font-weight: normal;
  letter-spacing: 0.125rem;
  text-transform: uppercase;
}

li {
  display: inline-block;
  font-size: 1.5em;
  list-style-type: none;
  padding: 1em;
  text-transform: uppercase;
}

li span {
  display: block;
  font-size: 4.5rem;
}

.message {
  font-size: 4rem;
}

#content {
  display: none;
  padding: 1rem;
}

.emoji {
  padding: 0 0.25rem;
}

@media all and (max-width: 768px) {
  h1 {
    font-size: 1.5rem;
  }

  li {
    font-size: 1.125rem;
    padding: 0.75rem;
  }

  li span {
    font-size: 3.375rem;
  }
}

</style>

Step 2 – Add JavaScript

Copy and paste below code in your HTML editor between <script></script> tag.

<script>
(function () {
  const second = 1000,
    minute = second * 60,
    hour = minute * 60,
    day = hour * 24;

  let birthday = "Sep 30, 2021 00:00:00",
    countDown = new Date(birthday).getTime(),
    x = setInterval(function () {
      let now = new Date().getTime(),
        distance = countDown - now;

      (document.getElementById("days").innerText = Math.floor(distance / day)),
        (document.getElementById("hours").innerText = Math.floor(
          (distance % day) / hour
        )),
        (document.getElementById("minutes").innerText = Math.floor(
          (distance % hour) / minute
        )),
        (document.getElementById("seconds").innerText = Math.floor(
          (distance % minute) / second
        ));

      //do something later when date is reached
      if (distance < 0) {
        let headline = document.getElementById("headline"),
          countdown = document.getElementById("countdown"),
          content = document.getElementById("content");

        headline.innerText = "It's my birthday!";
        countdown.style.display = "none";
        content.style.display = "block";

        clearInterval(x);
      }
      //seconds
    }, 0);
})();

</script>

how to make a countdown timer in html code

At the end we will have something like this. You can change fonts, colors, backgrounds and all things that you want.Enjoy it.

How to create a Countdown Timer in HTML

Video Tutorial

Please watch this video to better understand this tutorial and don’t forget to subscribe to our channel.


More articles

https://w3schoolweb.com/how-to-create-image-gallery-in-html/

https://w3schoolweb.com/how-to-create-simple-image-slider/

https://w3schoolweb.com/how-to-create-simple-image-gallery/

https://w3schoolweb.com/how-to-create-bootstrap-gallery/


Help others to find out about this article on Social Media sites. If you have any doubt or any problem, don’t hesitate to contact us. Thereafter we will be able to help you and also make sure you bookmark our site on your browser.

Related Articles

Leave a Reply

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

Back to top button