We are going to build a countdown timer together in this series. We’ll start by counting down to the end of this month. Then, to the end of the year. Then, a few years down the road.
It looks like this:
Anatomy of the countdown timer
There are two parts in a countdown timer:
The end date
The countdown itself
We can write the HTML like this:
<div class="countdown">
<h1 class="countdown__target">Time to ...</h1>
<div class="countdown__timer">...</div>
</div>
The end date
People use countdown timers to count to an event, like the launch of a product, end of sales, and start of a new year. If you’re using a countdown timer for that purpose, it makes sense to include the event in the timer.
For this countdown timer, we’ll count to a specific date instead.
Since we’re counting down to a date, we want to use the <time> element to create the date. We also want to include a datetime attribute.
<!-- Setting end date to 1 July, 2019 -->
<h1 class="countdown__target">Time to <time datetime="2019-07-01">1 July 2019</time></h1>
The countdown timer
To count to the end of this month, you need four different units:
Days
Hours
Minutes
Seconds
Each unit should be wrapped in its own HTML element since they’re independent of other units.