DEV Community

SEENUVASAN P
SEENUVASAN P

Posted on

Day:4 Building a Simple Counter App with HTML, CSS, and JavaScript

Today, I created a small but useful application Counter App using basic HTML, CSS, and JavaScript. Itโ€™s a great little project for beginners and a handy way to reinforce fundamental web development skills.

Let me walk you through how I built it and what it does.

What the App Does

This counter app displays a number (starting from 0) and provides three buttons:

โž• Increase: Adds 1 to the counter

โž– Decrease: Subtracts 1 from the counter

๐Ÿ” Reset: Resets the counter back to 0
Enter fullscreen mode Exit fullscreen mode

Itโ€™s interactive, responsive, and runs right in the browserโ€”no frameworks required!

The Code

Hereโ€™s the complete HTML code that includes the structure and the JavaScript logic:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Counter App</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <h1 id="result">0</h1>
        <div class="btn">
            <button onclick="increase()">+</button>
            <button onclick="decrease()">-</button>
            <button onclick="reset()">Reset</button>
        </div>
    </div>

    <script>
        let count = 0;
        let elem = document.getElementById("result");

        function increase() {
            count = count + 1;
            elem.innerText = count;
        }

        function decrease() {
            count = count - 1;
            elem.innerText = count;
        }

        function reset() {
            count = 0;
            elem.innerText = count;
        }
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

CSS code

body {
    font-family: Arial, sans-serif;
    background-color: #f5f5f5;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
}

.container {
    text-align: center;
    background-color: white;
    padding: 30px;
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

h1 {
    font-size: 4em;
    margin-bottom: 20px;
}

.btn button {
    font-size: 1.5em;
    padding: 10px 20px;
    margin: 5px;
    border: none;
    border-radius: 5px;
    background-color: #007bff;
    color: white;
    cursor: pointer;
}

.btn button:hover {
    background-color: #0056b3;
}
Enter fullscreen mode Exit fullscreen mode

Outpu

Image description

What I Learned

While making this app, I practiced:

Using onclick to trigger JavaScript functions

Updating the DOM with innerText

Writing clean, reusable functions

Keeping logic and presentation separate
Enter fullscreen mode Exit fullscreen mode

Top comments (0)