COUNTDOWN TIMER USING HTML, CSS AND JAVASCRIPT
Tags: javascript, webdev, beginners, tutorial
Published: 2022-02-21 06:13:34

Full Blog Post With Source Code Here -> 
[COUNTDOWN TIMER USING HTML, CSS AND JAVASCRIPT](https://codelikeadev.com/blog/countdown-timer-html-css-javascript)


[![Subscribe to Sameer Saini Youtube channel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0lq3y8extdkpnjfkfjz.png)](https://bit.ly/2FAgXzS)


<p>If you are just starting our learning HTML, CSS and Javascript, mini projects can be a great way to learn the language and put it to use.</p>
<p>There are so many mini-projects that you can make using these languages one of which is a countdown timer.</p>
<h2>&nbsp;</h2>
<h2>Why Create A Countdown Timer?&nbsp;</h2>
<p>A countdown timer as we have seen in many places including one in our mobile phones is a timer that ticks backward from a given time to zero.</p>
<p>A countdown timer has several benefits in real life too. This can be used as an alarm to see if food is properly cooked or not, to track activity, and numerous other things.</p>
<p>Although we have a countdown timer in our mobile phone, creating a countdown timer from scratch using HTML, CSS and Javascript can be so much fun.&nbsp;</p>
<p>This can be a mini project that you can add to your portfolio.</p>
<p>So let's jump on to the blog post and create a fancy countdown timer using HTML, CSS, and Javascript step by step, and at the end, I will also provide the source code for it.</p>
<h2>&nbsp;</h2>
<h2>Coding A Countdown Timer - Explained [Step By Step]</h2>
<p>To begin with, we will create an HTML barebone structure and will link our CSS and Script file to it.</p>
<p>Our barebone HTML looks like this:</p>
<p><a href="https://blogger.googleusercontent.com/img/a/AVvXsEjINmL38p-9Yms2F9gShfTaSiRaVSZ70fEEAUOMFruZFd2J2FVA42AUGS8x4SKaLhbQ4UX7QprlGhEa9k4Td4wfCilfNd9dZXlIQ7eRb2rwhNlwRpkC5fZqE3QqxfFOzcG8yz0gTF6qPrFEPNTbysESBH-zp5tj_yPnXnTemT5a3tjJGDzelQTR5fJI"><img alt="" data-original-height="629" data-original-width="1162" src="https://blogger.googleusercontent.com/img/a/AVvXsEjINmL38p-9Yms2F9gShfTaSiRaVSZ70fEEAUOMFruZFd2J2FVA42AUGS8x4SKaLhbQ4UX7QprlGhEa9k4Td4wfCilfNd9dZXlIQ7eRb2rwhNlwRpkC5fZqE3QqxfFOzcG8yz0gTF6qPrFEPNTbysESBH-zp5tj_yPnXnTemT5a3tjJGDzelQTR5fJI=s16000" /></a></p>
<p><br />See how we have linked our CSS in the head section and the script right at the end of the body tag.</p>
<p>Any elements that we add to this HTML will go above the script file reference.</p>
<p>Now, we will add the HTML structure for the countdown timer.</p>
<p><a href="https://blogger.googleusercontent.com/img/a/AVvXsEgXR59oH8JwhxecluhvmyoT9eeE1f1BLkI3VdCMGBHnU9RCxw_0HmfLdUwbSMyS65B-xyQ_akcsQoCoKgmTRdp_xXkiSHkLzKvVNmG4Q97n2SSvfsIEp_nLpIK63DlqNPhu9QbceUaQrRUdYOb_gBG_DhUC6SXq6I4NrRmXcpC1bZ82dCAvU2AvWC3B"><img alt="" data-original-height="493" data-original-width="1102" src="https://blogger.googleusercontent.com/img/a/AVvXsEgXR59oH8JwhxecluhvmyoT9eeE1f1BLkI3VdCMGBHnU9RCxw_0HmfLdUwbSMyS65B-xyQ_akcsQoCoKgmTRdp_xXkiSHkLzKvVNmG4Q97n2SSvfsIEp_nLpIK63DlqNPhu9QbceUaQrRUdYOb_gBG_DhUC6SXq6I4NrRmXcpC1bZ82dCAvU2AvWC3B=s16000" /></a></p>
<p><br />Now we will style this countdown timer and add some CSS code for these HTML elements.</p>
<p><a href="https://blogger.googleusercontent.com/img/a/AVvXsEgZFsi0dmGBMS_tcMlwqSZA3BuJZRnf2r1XEsadAn8myP3Y1vWEHxt328WoTPugbKybz2-FTpO08u0so881IVDqxdXC7rTeBZE7bhfftkH_c0LE1sKLLJCncMMltQUl7uw6DhskuCnNpWPvkOq4sdj4FJJXqyNtWWE1K4cjynbBwKzHSIfyYgLSod_q"><img alt="" data-original-height="883" data-original-width="1051" src="https://blogger.googleusercontent.com/img/a/AVvXsEgZFsi0dmGBMS_tcMlwqSZA3BuJZRnf2r1XEsadAn8myP3Y1vWEHxt328WoTPugbKybz2-FTpO08u0so881IVDqxdXC7rTeBZE7bhfftkH_c0LE1sKLLJCncMMltQUl7uw6DhskuCnNpWPvkOq4sdj4FJJXqyNtWWE1K4cjynbBwKzHSIfyYgLSod_q=s16000" /></a></p>
<p><br />The result of this HTML and CSS is this:</p>
<p><a href="https://blogger.googleusercontent.com/img/a/AVvXsEgk8uGvuuwtPjEO3fBguPbGuHytCYvAJZRaFrkPxiSGxd8QdIE9FKQ9ljo3q8Wr3-bPpTzu5UGBB1Zb3KNWswviKOYkDX6pm70EMkchFd-q4hIkkum5BDlp4g3xBO9G3v9OAUqOZ4M1VVeQ8VVR4TF6Hh9zlmI1OIjXdRF_hMLTVh-nzIKgIiCtX9dn"><img alt="" data-original-height="880" data-original-width="1915" src="https://blogger.googleusercontent.com/img/a/AVvXsEgk8uGvuuwtPjEO3fBguPbGuHytCYvAJZRaFrkPxiSGxd8QdIE9FKQ9ljo3q8Wr3-bPpTzu5UGBB1Zb3KNWswviKOYkDX6pm70EMkchFd-q4hIkkum5BDlp4g3xBO9G3v9OAUqOZ4M1VVeQ8VVR4TF6Hh9zlmI1OIjXdRF_hMLTVh-nzIKgIiCtX9dn=s16000" /></a></p>
<p><br />Now, we will write some javascript to make this countdown timer functional.</p>
<p>We will first use a date in the future to which our countdown timer counts from today's date and time.</p>
<p><a href="https://blogger.googleusercontent.com/img/a/AVvXsEimi1K-lDB8aYW-YFjyqUolAioD8ONsyrr7Op1D3rCJehK-z4sy645rlYqSa4V1sOrVpeomjwG-rso7p7cPeSqJZK8KpRWQepEoWO0FA6fQ2vSD99Zv_1cDnnmR8Qpm6Pg-XII-opFqX-IwxsMkLZY9ft9tTSRBf9enZLWh932m6cUfZcOG7mbCsiB5"><img alt="" data-original-height="760" data-original-width="1081" src="https://blogger.googleusercontent.com/img/a/AVvXsEimi1K-lDB8aYW-YFjyqUolAioD8ONsyrr7Op1D3rCJehK-z4sy645rlYqSa4V1sOrVpeomjwG-rso7p7cPeSqJZK8KpRWQepEoWO0FA6fQ2vSD99Zv_1cDnnmR8Qpm6Pg-XII-opFqX-IwxsMkLZY9ft9tTSRBf9enZLWh932m6cUfZcOG7mbCsiB5=s16000" /></a></p>
<p><br />Let's break down the Javascript code for this countdown timer.</p>
<p>On the top, we have a const value of the date and time we are counting down from.</p>
<p>In our example, we are counting down the date and time from next year.</p>
<p>Then we have a few query selectors for the various values of days, hours, minutes, and seconds.</p>
<p>Then comes the main bit, we are using the "setInterval" function that Javascript provides and we will run a function every 1000 milliseconds or 1 second.</p>
<p>In this function, we will calculate the difference of time between now and the date provided in the constant we are counting down from.</p>
<p>Then, we will assign these values to the selectors.</p>
<p>This will result in the following output and our countdown timer is ready.</p>
<div><a href="https://blogger.googleusercontent.com/img/a/AVvXsEiywyZ8I7LA1U167fQ3GkaAit-Tb9dc_fOsGzwsHhsWddsi7488FtriPUxUX2cXUpkmrATft8HuiG6y5M5LWfJGRZzvGMUeXPU5YjO7H9Z3UGjAU97TXHUgV62MflbtQUYpdKCKlSGhKVJ_ANz7fooZqiGbybrDtyzGzZrB-_IJ2uGzzuqejoH9K9lP"><img alt="" data-original-height="764" data-original-width="1915" src="https://blogger.googleusercontent.com/img/a/AVvXsEiywyZ8I7LA1U167fQ3GkaAit-Tb9dc_fOsGzwsHhsWddsi7488FtriPUxUX2cXUpkmrATft8HuiG6y5M5LWfJGRZzvGMUeXPU5YjO7H9Z3UGjAU97TXHUgV62MflbtQUYpdKCKlSGhKVJ_ANz7fooZqiGbybrDtyzGzZrB-_IJ2uGzzuqejoH9K9lP=s16000" /></a></div>
<p><br /><br /></p>