countdown HTML CSS JS

countdown HTML CSS JS

countdown HTML CSS JS

Before We Start

countdown HTML CSS JS. 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.

countdown HTML CSS JS. 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.

  <div class="countdown" id="js-countdown">
    <div class="countdown__item countdown__item--large">
      <div class="countdown__timer js-countdown-days" aria-labelledby="day-countdown">


      <div class="countdown__label" id="day-countdown">Days</div>

    <div class="countdown__item">
      <div class="countdown__timer js-countdown-hours" aria-labelledby="hour-countdown">


      <div class="countdown__label" id="hour-countdown">Hours</div>

    <div class="countdown__item">
      <div class="countdown__timer js-countdown-minutes" aria-labelledby="minute-countdown">


      <div class="countdown__label" id="minute-countdown">Minutes</div>

    <div class="countdown__item">
      <div class="countdown__timer js-countdown-seconds" aria-labelledby="second-countdown">


      <div class="countdown__label" id="second-countdown">Seconds</div>

Step 2 – Add CSS

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

body {
  background-color: purple;
  display: -webkit-box;
  display: flex;
  -webkit-box-align: center;
          align-items: center;
  min-height: 100vh;
  text-align: center;
  font-family: helvetica;
  text-transform: uppercase;
  background-image: linear-gradient(165deg, rgba(194, 233, 221, 0.5) 3%, rgba(104, 119, 132, 0.5) 100%);

.countdown {
  display: -webkit-box;
  display: flex;
  flex-wrap: wrap;
  -webkit-box-pack: justify;
          justify-content: space-between;
  width: 75%;
  max-width: 20rem;
  margin: 0 auto;

.countdown__item {
  display: -webkit-box;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
          flex-direction: column;
  -webkit-box-flex: 0;
          flex: 0 1 auto;
  min-width: 31%;
  margin-bottom: 1rem;

.countdown__item--large {
  -webkit-box-flex: 1;
          flex: auto;
  width: 100%;
  font-size: 2.75em;

.countdown__timer {
  padding: 0.25em;
  background-color: white;
  border: 1px solid black;
  border-radius: 3px;
  font-weight: bold;
  font-size: 2em;

.countdown__label {
  font-size: 1em;
  padding-top: 0.4em;
.countdown__item--large .countdown__label:before, .countdown__item--large .countdown__label:after {
  content: "";
  display: block;
  height: 1px;
  background-image: -webkit-gradient(linear, left top, right top, from(transparent), color-stop(rgba(0, 0, 0, 0.4)), to(transparent));
  background-image: linear-gradient(left, transparent, rgba(0, 0, 0, 0.4), transparent);

Step 2 – Add JavaScript

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

const countdown = new Date("May 7, 2021");

function getRemainingTime(endtime) {
  const milliseconds = Date.parse(endtime) - Date.parse(new Date());
  const seconds = Math.floor(milliseconds / 1000 % 60);
  const minutes = Math.floor(milliseconds / 1000 / 60 % 60);
  const hours = Math.floor(milliseconds / (1000 * 60 * 60) % 24);
  const days = Math.floor(milliseconds / (1000 * 60 * 60 * 24));

  return {
    total: milliseconds,
    seconds: seconds,
    minutes: minutes,
    hours: hours,
    days: days };


function initClock(id, endtime) {
  const counter = document.getElementById(id);
  const daysItem = counter.querySelector(".js-countdown-days");
  const hoursItem = counter.querySelector(".js-countdown-hours");
  const minutesItem = counter.querySelector(".js-countdown-minutes");
  const secondsItem = counter.querySelector(".js-countdown-seconds");

  function updateClock() {
    const time = getRemainingTime(endtime);

    daysItem.innerHTML = time.days;
    hoursItem.innerHTML = ("0" + time.hours).slice(-2);
    minutesItem.innerHTML = ("0" + time.minutes).slice(-2);
    secondsItem.innerHTML = ("0" + time.seconds).slice(-2);

    if ( <= 0) {

  const timeinterval = setInterval(updateClock, 1000);
initClock("js-countdown", countdown);

countdown HTML CSS JS

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

countdown HTML CSS JS

Video Tutorial

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

More articles

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.

Back to top button