Simple Stopwatch using HTML, CSS and JavaScript (Source Code) – Coding Torque

Share your love

Welcome to Coding Torque, fellow coders! Are you ready to take your skills to the next level? In this tutorial, we will be creating a Simple Stopwatch using HTML, CSS, and JavaScript. Not only will this project help you improve your coding skills, but it will also come in handy as a practical tool in your daily life. Let’s get started!

Before we start, here are some JavaScript Games you might like to create:

1.Ā Snake Game using JavaScript

2.Ā 2D Bouncing Ball Game using JavaScript

3.Ā Rock Paper Scissor Game using JavaScript

4.Ā Tic Tac Toe Game using JavaScript

5. Whack a Mole Game using JavaScript


I would recommend you don’t just copy and paste the code, just look at the code and type by understanding it.



Starter Template

<!doctype html>
<html lang="en">

    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSS -->
    <link rel="stylesheet" href="style.css">

    <title>Simple Stopwatch using JavaScript - @code.scientist x @codingtorque</title>

    <!-- Further code here -->

    <script src="script.js"></script>



Paste the below code in your <body> tag

<section class="container">
    <div class="watch">
        <h2 id="watch">00:00:00:00</h2>
    <div class="buttons">
        <button id="start">Start</button>
        <button id="pause">Pause</button>
        <button id="reset">Reset</button>

Output Till Now

CSS CodeĀ 

Create a fileĀ style.css and paste the code below.

@import url(",700&display=swap");
:root {
  --primary-color: rgb(149, 0, 255);
  --primary-color-darker: rgb(85, 112, 84);

* {
  box-sizing: border-box;
  outline: 0;
  padding: 0;
  margin: 0;

body {
  margin: 0;
  padding: 0;
  background: var(--primary-color);
  font-family: "Poppins", sans-serif;
  font-size: 1.3em;
  line-height: 1.5em;

.container {
  max-width: 640px;
  margin: 50px auto;
  padding: 20px;
  border-radius: 10px;

.watch {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  background: white;
  height: 10rem;
  border-radius: 20px;

h2 {
  font-size: 3em;
  font-weight: 400;
  height: 50px;
  width: 335px;

h2.paused {
  color: red;

.buttons {
  display: flex;
  flex-direction: row;
  justify-content: center;
  margin: 10px 0;

.buttons button {
  margin: 10px;
  margin-top: 20px;
  border: none;
  padding: 15px 30px;
  font-weight: bold;
  text-transform: uppercase;
  letter-spacing: 4px;
  border-radius: 5px;
  cursor: pointer;


Output Till Now

Simple Stopwatch using HTML, CSS and JavaScript with Source Code

JavaScript CodeĀ 

Create a fileĀ script.jsĀ and paste the code below.
const watch = document.querySelector('#watch');
let milliseconds = 0;
let timer;

function startWatch() {
    timer = setInterval(() => {
        milliseconds += 10;
        let dateTimer = new Date(milliseconds);
        watch.innerHTML =
            ('0' + dateTimer.getUTCHours()).slice(-2) + ':' +
            ('0' + dateTimer.getUTCMinutes()).slice(-2) + ':' +
            ('0' + dateTimer.getUTCSeconds()).slice(-2) + ':' +
            ('0' + dateTimer.getUTCMilliseconds()).slice(-3, -1);
    }, 10);

function pauseWatch() {

function resetWatch() {
    milliseconds = 0;
    watch.innerHTML = '00:00:00:00';

document.addEventListener('click', (e) => {
    const el =;
    if ( === 'start') startWatch();
    if ( === 'pause') pauseWatch();
    if ( === 'reset') resetWatch();


Written by: Piyush Patil
Code Credits:Ā @henriquelothammer
If you have any doubts or any project ideas feel free to Contact Us
Hope you find this post helpfulšŸ’–
Share your love