How To Build a Simple Calculator Using HTML, CSS & Javascript

Have you ever wanted to design a simple calculator using HTML, CSS and most of all Javascript to handle the calculations? In this tutorial I’ll guide you through designing yours with the best design you might have imagined.
Before we continue with this tutorial, I’d assume you have a basic knowledge of HTML, CSS and Javascript. Anyways, if you are new to any of these, you can as well download the full script at the end of this post and simply upload to your server to have a working and stunning looking calculator webpage.
Here’s what we’d be having at the end of this tutorial.
Calculator with JS, CSS and HTML
You can also check out the live demo by following the link below.
Let’s start with the HTML code. You can create a file in your favorite text editor (Notepad, Notepad++, Sublime Text, Visual Studio code etc.). Then copy the below HTML code and save as “index.html”. The below code contains the buttons and inputs that houses the structure of the calculator.
<!DOCTYPE html>
 <title>A Simple Calculator With HTML, CSS & Javascript - Oscarmini Calculator</title>
 <meta name="description" content="Here's a demo of a simple calculator with a stunning interface built from scratch with CSS, Javascript and of course HTML"/>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="script.js"></script>

<div class="container">
 <div class="calc-container">

  <h1>OM Calc</h1>

  <input id="box" value="" type="text" readonly placeholder="0"><br/>

  <div class="btn-container">
  <div class="row">
    <button class="btn" onclick="shownum('1')"><strong>1</strong></button>
    <button class="btn" onclick="shownum('2')"><strong>2</strong></button>
    <button class="btn" onclick="shownum('3')"><strong>3</strong></button>
    <button class="btn cbtn" onclick="delback()"><strong></strong></button>
  <div class="row">
    <button class="btn" onclick="shownum('4')"><strong>4</strong></button>
    <button class="btn" onclick="shownum('5')"><strong>5</strong></button>
    <button class="btn" onclick="shownum('6')"><strong>6</strong></button>
    <button class="btn" onclick="shownum('+')"><strong>+</strong></button>
  <div class="row">
    <button class="btn" onclick="shownum('7')"><strong>7</strong></button>
    <button class="btn" onclick="shownum('8')"><strong>8</strong></button>
    <button class="btn" onclick="shownum('9')"><strong>9</strong></button>
    <button class="btn" onclick="shownum('-')"><strong>-</strong></button>
  <div class="row">
    <button class="btn" onclick="shownum('.')"><strong>.</strong></button>
    <button class="btn" onclick="shownum('0')"><strong>0</strong></button>
    <button class="btn" onclick="shownum('00')"><strong>00</strong></button>
    <button class="btn" onclick="shownum('*')"><strong>*</strong></button>
  <div class="row">
    <button class="btn cbtn" onclick="clearme()"><strong>C</strong></button>
    <button class="btn solvebtn" onclick="solve()"><strong>=</strong></button>
    <button class="btn" onclick="shownum('/')"><strong>/</strong></button>


 <footer id="footer">Designed by <a href="" target="_blank" style="color: #ffbb4f">Oscarmini</a></footer>
HTML for Simple JS Calc
After you must have saved the above code in your choice directory or folder, it’s time to write the CSS to style our calculator into a beauty. This css file as you can see from the above code is already linked.
h1 {color: #fff;}
        margin: 0 auto;
.calc-container {
  padding: 20px;
  background: #191717;
  width: 250px;
  border-radius: 20px;
  box-shadow: 10px 10px 5px #888888;    
  -moz-box-shadow: 10px 10px 5px #888888;
  -webkit-box-shadow: 10px 10px 5px #888888;
  margin: 10px auto;
#box {
        height: 40px; 
        border:1px solid #000; 
        background: #ddded9; 
        font-size: 30px; 
        text-align: right;
        color: #f00;
  margin: 0 auto;
.btn-container {
  margin: 10px 5px;
  background: #ffbb4f;

.btn {
  height: 40px;
  width: 40px;
        padding: 10px; 
        background: #291616; 
        color: #fff; 
  border: none;
        margin: 8px; 
        font-size: 15px;    
  box-shadow: 1px 1px 1px #000;
     -moz-box-shadow: 1px 1px 1px #000;
     -webkit-box-shadow: 1px 1px 1px #000;

.btn:hover {background-color: #000}

.btn:active {
  background-color: #000;
  box-shadow: 0 5px #666;
  transform: translateY(2px);
.solvebtn {
  background: #085d08;
.solvebtn:hover {background-color: #013e01;}

.solvebtn:active {
  background-color: #013e01;
  box-shadow: 0 5px #666;
  transform: translateY(2px);
.cbtn {
  background: #f00; 
.cbtn:hover {background-color: #b70505}

.cbtn:active {
  background-color: #b70505;
  box-shadow: 0 5px #666;
  transform: translateY(2px);
#footer {
  text-align: center;
  margin: 0px auto;
  padding: 10px;
  color: #fff;
#footer a:link {color: #fff;text-decoration: none;}
CSS for simple JS calculator
You have to save this codes as “style.css” and in the same directory as the “index.html” you saved earlier.
Now this is where the magic happens. We are going to use javascript to handle all the calculations. Below is the javascript code you need. Make sure it is saved as “script.js” in the exact same folder as the files above.
// Get the Variable from the input box
function screen(val)

// Concatenating Values
function shownum(val)   {

// Performing the Calcuulation
function solve() { 
    try     { 
    catch(e) {
// Clear the Input Screen
function clearme() {
                document.getElementById("box").value = "";
// Backspace Function
function delback() {
                var valueNeeded = document.getElementById("box").value;
                var finalValueNeeded = valueNeeded.substr(0, valueNeeded.length-1); 
JavaScript for simple JS calculator
Once you have all these files saved in the same folder, it’s time to test out your work. Launch the “index.html” file in your favorite browser and test your stunning looking JS, HTML and CSS written calculator.
Wanna save yourself the stress of having to write and save these codes yourself? Well, I’ve got you covered. You can download the full script with the link below and simple upload to see how it works.
Download the full script below
Do let me know what you’d like to see in the future on this blog as regards, HTML, CSS and Javascript.