Light/Dark Theme Calculator Javascript | Calculator Html Css Js

Light/Dark Theme Calculator Javascript | Calculator Html Css Js

Welcome🎉 to Code With Random blog. In this blog, we learn how we create a Light/Dark Theme Calculator. We use HTML, Css, and javascript for this Light/Dark Theme Calculator. I hope you enjoy our blog so let’s start with a basic HTML structure for the Light/Dark Theme Calculator.


Table of Contents

 <!DOCTYPE html>  
<html lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Black & White Calculator</title>
<div class="container">
<div class="calculator dark">
<div class="theme-toggler active">
<i class="toggler-icon"></i>
<div class="display-screen">
<div id="display"></div>
<div class="buttons">
<td><button class="btn-operator" id="clear">C</button></td>
<td><button class="btn-operator" id="/">&divide;</button></td>
<td><button class="btn-operator" id="*">&times;</button></td>
<td><button class="btn-operator" id="backspace"><</button></td>
<td><button class="btn-number" id="7">7</button></td>
<td><button class="btn-number" id="8">8</button></td>
<td><button class="btn-number" id="9">9</button></td>
<td><button class="btn-operator" id="-">-</button></td>
<td><button class="btn-number" id="4">4</button></td>
<td><button class="btn-number" id="5">5</button></td>
<td><button class="btn-number" id="6">6</button></td>
<td><button class="btn-operator" id="+">+</button></td>
<td><button class="btn-number" id="1">1</button></td>
<td><button class="btn-number" id="2">2</button></td>
<td><button class="btn-number" id="3">3</button></td>
<td rowspan="2"><button class="btn-equal" id="equal">=</button></td>
<td><button class="btn-operator" id="(">(</button></td>
<td><button class="btn-number" id="0">0</button></td>
<td><button class="btn-operator" id=")">)</button></td>
<script src="script.js"></script>

There is all the HTML code for the Light/Dark Theme Calculator Javascript. Now, you can see an output with Light/Dark Theme Calculator Javascript then we write javascript for Light/Dark Theme Calculator Javascript.


CSS code

padding: 0;
margin: 0;
box-sizing: border-box;
outline: 0;
transition: all 0.5s ease;
font-family: sans-serif;
text-decoration: none;
color: #fff;
background-image: linear-gradient(to bottom right, rgba(189,13,34,1),rgba(2,0,184,1), rgba(0,0,0,1));
height: 100vh;
width: 100vw;
display: grid;
place-items: center;
position: relative;
height: auto;
width: auto;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 30px #000;
position: absolute;
top: 30px;
right: 30px;
color: #fff;
cursor: pointer;
z-index: 1;
color: #333;
background-color: #fff;
content: '';
height: 30px;
width: 30px;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
border-radius: 50%;
background-color: #333;
z-index: -1;
margin: 0 10px;
height: 150px;
width: auto;
max-width: 270px;
display: flex;
align-items: flex-end;
justify-content: flex-end;
font-size: 30px;
margin-bottom: 20px;
overflow-x: scroll;
display: block;
height: 3px;
height: 60px;
width: 60px;
border: 0;
border-radius: 30px;
margin: 5px;
font-size: 20px;
cursor: pointer;
transition: all 200ms ease;
transform: scale(1.1);
height: 130px;
/* light theme */
background-color: #fff;
.calculator #display{
color: #0a1e23;
.calculator button#clear{
background-color: #ffd5d8;
color: #fc4552;
.calculator button.btn-number{
background-color: #c3eaff;
color: #000;
.calculator button.btn-operator{
background-color: #ffd0fd;
color: #f967f3;
.calculator button.btn-equal{
background-color: #adf9e7;
color: #000;
/* dark theme */
background-color: #071115;
.calculator.dark #display{
color: #f8fafd;
.calculator.dark button#clear{
background-color: #2d191e;
color: #bd3740;
.calculator.dark button.btn-number{
background-color: #1b2f38;
color: #f8fafb;
.calculator.dark button.btn-operator{
background-color: #2e1f39;
color: #aa00a4;
.calculator.dark button.btn-equal{
background-color: #223323;

Css Updated output

Javascript code

 const display = document.querySelector('#display');  
const buttons = document.querySelectorAll('button');
buttons.forEach((item) => {
item.onclick = () => {
if ( == 'clear') {
display.innerText = '';
} else if ( == 'backspace') {
let string = display.innerText.toString();
display.innerText = string.substr(0, string.length - 1);
}else if (display.innerText != '' && == 'equal') {
display.innerText = eval(display.innerText);
}else if (display.innerText == '' && == 'equal') {
display.innerText = 'Empty!';
setTimeout(() => (display.innerText = ''), 2000);
}else {
display.innerText +=;
const themeToggleBtn = document.querySelector('.theme-toggler');
const calculator = document.querySelector('.dark');
const toggleIcon = document.querySelector('.toggler-icon');
let isDark = true;
themeToggleBtn.onclick = () => {
isDark = !isDark;

Final output

Now that we have completed our javascript section,  Here is our updated output with javascriptHope you like the Light/Dark Theme Calculator. you can see the output video and project screenshots. See our other blogs and gain knowledge in front-end development. Thank you 🙏💕!

In this post, we learn how to create a Light/Dark Theme Calculator using simple HTML & CSS, and javascript. If we made a mistake or any confusion, please drop a comment to reply or help you in easy learning. 

Written by – Code With Random/Anki 

Code by – Richard Rembert

Check out more…..

This Post Has 0 Comments

  1. Anonymous

    Where is the style.css and js file

Leave a Reply