Hello Devs!!
Today’s session was really productive! I got hands-on with a small but powerful frontend mini-project — a Simple Counter App using HTML, CSS, and JavaScript.
After finishing my simple counter app earlier today, I wanted to make the app feel more real— like it has a proper flow.
So, I decided to add a “Let’s Start” button on the landing screen. Only after clicking it, the user should see the counter and interact with the buttons.
It sounds simple, but this small addition taught me a lot of practical lessons.
What I Want To Do:
1.Show a welcome screen with a “Let’s Start” button
2.When the button is clicked, hide the welcome screen and reveal the counter app
3.Make the transition feel smooth and controlled using only HTML, CSS, and JavaScript(no page reload!)
Step 1: Create Two Screens:
<div class="container">
<!-- Start Screen -->
<div class="start-screen active" id="startScreen">
<h1>Welcome!</h1>
<button class="start-btn" onclick="startApp()">Let's Start</button>
</div>
<!-- Counter Screen -->
<div class="counter-screen" id="counterScreen">
<div class="counter-btns">
<button id="increase" onclick="increase()">Increase</button>
<button id="decrease" onclick="decrease()">Decrease</button>
<button id="reset" onclick="reset()">Reset</button>
</div>
<h1 id="result">0</h1>
</div>
</div>
Explanation:
1.We made two sections:
- Start screen → shows a welcome message + “Let’s Start” button.
- Counter screen → has increase, decrease, and reset buttons + displays the counter.
We give each an id so we can target it in JavaScript, and we use the Step 2: Add CSS for Styling and Layout: Explanation: Step 3: Write JavaScript: Explanation: Final check: Tips I Learned Challenges I Faced: 1.How to hide and show sections? At first, I thought about using 2.How to connect the start button to the counter screen? I needed to write a function At first, I forgot to connect the button with the JS function using 3.Managing state between screens The counter’s initial state was easy, but I had to make sure the counter only showed up after starting. So I wrapped the counter display in its own container and controlled its visibility properly. Here’s the approach I took: 1.In HTML, I created two divs: one for the start screen, one for the counter app. Keep Learn!! Happy Coding!!.active
class to control which one shows.
body {
background: linear-gradient(to right, #8702a1, #7b7dfe);
font-family: Arial, sans-serif;
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.start-screen, .counter-screen {
display: none;
text-align: center;
background-color: white;
padding: 40px;
border-radius: 20px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.start-screen.active, .counter-screen.active {
display: block;
}
button {
padding: 10px 20px;
margin: 10px;
font-size: 16px;
border: none;
border-radius: 8px;
cursor: pointer;
transition: background-color 0.3s;
}
.start-btn {
background-color: #ff9800;
color: white;
}
.counter-btns button {
background-color: #2196f3;
color: white;
}
.counter-btns button#increase {
background-color: #4caf50;
}
.counter-btns button#decrease {
background-color: #f44336;
}
h1 {
font-size: 48px;
margin-top: 20px;
}
.container
centers everything on the screen..start-screen
and .counter-screen
are hidden by default (display: none)
..active
is added, they become visible (display: block)
.
let count = 0; // starting counter value
const element = document.getElementById("result"); // where the number shows
const startScreen = document.getElementById("startScreen");
const counterScreen = document.getElementById("counterScreen");
function startApp() {
startScreen.classList.remove('active'); // hide start screen
counterScreen.classList.add('active'); // show counter screen
}
function increase() {
count += 1; // add 1
element.innerText = count; // update display
}
function decrease() {
count -= 1; // subtract 1
element.innerText = count; // update display
}
function reset() {
count = 0; // reset to 0
element.innerText = count; // update display
}
count
variable starting at 0.startScreen, counterScreen, result
) using getElementById.startApp()
switches screens by removing/adding the .active
class.
increase, decrease, reset
) change the count and update the text on screen.
(.active)
is cleaner than directly changing style.display.
onclick
.display: none
directly in JavaScript. But that felt messy. I realized it’s better to use CSS classes (.active)
to control visibility. That way, I just toggle the class in JS, and CSS handles the display.(startApp())
that:
.active
class from the start screen.active
class to the counter screenonclick,
so the button did nothing. Once I fixed that, it worked!
2.In CSS, I used .active
class to control which one is visible.
3.In JavaScript, I wrote a startApp()
function to switch the screens when the button is clicked.
Top comments (0)