Create a Simple, Stylish Calculator Using HTML, CSS and JavaScript

Introduction

 
In this article, we will create a calculator step-by-step. We need to create a basic structure using HTML, style it using CSS and make it work using JavaScript. 
 
Step 1: HTML Document Structure
 
The following creates the div (.box) that represents the structure of the calculator. Inside it are two div tags, one for display (inside this, I added input type=" text" to display the result and set this to read-only) and one for Keys. There is also an onclick event to be explained later.
  1. <div class="box">  
  2.         <div class="display"><input type="text" readonly size="18" id="d"></div>  
  3.         <div class="keys">  
  4.                <p><input type="button" class="button gray" value="mrc" onclick='c("Created....................")'><input type="button" class="button gray" value="m-" onclick='c("...............by............")'><input type="button" class="button gray"  
  5. value="m+" onclick='c(".....................Anoop")'><input type="button" class="button pink" value="/" onclick='v("/")'></p>  
  6.                <p><input type="button" class="button black" value="7" onclick='v("7")'><input type="button" class="button black" value="8" onclick='v("8")'><input type="button" class="button black" value="9" onclick='v("9")'><input  
  7. type="button" class="button pink" value="*" onclick='v("*")'></p>  
  8.                <p><input type="button" class="button black" value="4" onclick='v("4")'><input type="button" class="button black" value="5" onclick='v("5")'><input type="button" class="button black" value="6" onclick='v("6")'><input  
  9. type="button" class="button pink" value="-" onclick='v("-")'></p>  
  10.                <p><input type="button" class="button black" value="1" onclick='v("1")'><input type="button" class="button black" value="2" onclick='v("2")'><input type="button" class="button black" value="3" onclick='v("3")'><input  
  11. type="button" class="button pink" value="+" onclick='v("+")'></p>  
  12.                <p><input type="button" class="button black" value="0" onclick='v("0")'><input type="button" class="button black" value="." onclick='v(".")'><input type="button" class="button black" value="C" onclick='c("")'><input  
  13. type="button" class="button orange" value="=" onclick='e()'></p>  
  14.         </div>  
  15. </div> 
Preview
 
Calculator1.jpg
 
CSS
  1. body  
  2. {  
  3. background-color:tan;  
  4. }  
  5. .box  
  6. {  
  7. background-color:#3d4543;  
  8. height:300px;  
  9. width:250px;  
  10. border-radius:10px;  
  11. position:relative;  
  12. top:80px;  
  13. left:40%;  
The following CSS sets the background color of the body and sets the height, width and border-radius of the class box (.box) for styling:
 
Preview
 
Calculator2.jpg
 
CSS
 
The following CSS sets display background-color, width, height and position. Inside it, we have an input box, I also adjust its height, width, color, background-color and position.
  1. .display  
  2. {  
  3. background-color:#222;  
  4. width:220px;  
  5. position:relative;  
  6. left:15px;  
  7. top:20px;  
  8. height:40px;  
  9. }  
  10. .display input  
  11. {  
  12. position:relative;  
  13. left:2px;  
  14. top:2px;  
  15. height:35px;  
  16. color:black;  
  17. background-color:#bccd95;  
  18. font-size:21px;  
  19. text-align:right;  
Preview
 
Calculator2.5.jpg
 
CSS
 
The following CSS adjusts the position of the div (.keys). I use a Double Class Selector (for example class=button gray). By the use of this, I set the .button width, height, border-radius, cursor, and so on. This helps me in making all my buttons of the same width, height, and so on. Then I set .button.gray color, background-color, and so on.
 
After that, I create: active (the: active pseudo selector will match when an element is being pressed down on by the mouse cursor) for buttons. The thing I have done here is to change the top border color to black and bottom color to be the same as the button color. This gives the button the feel of pressing down.
  1. .keys  
  2. {  
  3. position:relative;  
  4. top:15px;  
  5. }  
  6. .button  
  7. {  
  8. width:40px;  
  9. height:30px;  
  10. border:none;  
  11. border-radius:8px;  
  12. margin-left:17px;  
  13. cursor:pointer;  
  14. border-top:2px solid transparent;  
  15. }  
  16. .button.gray  
  17. {  
  18. color:white;  
  19. background-color:#6f6f6f;  
  20. border-bottom:black 2px solid;  
  21. border-top:2px #6f6f6f solid;  
  22. }  
  23. .button.pink  
  24. {  
  25. color:black;  
  26. background-color:#ff4561;  
  27. border-bottom:black 2px solid;  
  28. }  
  29. .button.black  
  30. {  
  31. color:white;  
  32. background-color:303030;  
  33. border-bottom:black 2px solid;  
  34. border-top:2px 303030 solid;  
  35. }  
  36. .button.orange  
  37. {  
  38. color:black;  
  39. background-color:FF9933;  
  40. border-bottom:black 2px solid;  
  41. border-top:2px FF9933 solid;  
  42. }  
  43. .gray:active  
  44. {  
  45. border-top:black 2px solid;  
  46. border-bottom:2px #6f6f6f solid;  
  47. }  
  48. .pink:active  
  49. {  
  50. border-top:black 2px solid;  
  51. border-bottom:#ff4561 2px solid;  
  52. }  
  53. .black:active  
  54. {  
  55. border-top:black 2px solid;  
  56. border-bottom:#303030 2px solid;  
  57. }  
  58. .orange:active  
  59. {  
  60. border-top:black 2px solid;  
  61. border-bottom:FF9933 2px solid;  
  62. }  
  63. p  
  64. {  
  65. line-height:10px;  
Preview
 
Calculator3.jpg
 
Finally, we have created a calculator but it does not work. Now we need to do scripting for it (using JavaScript).
 
Create Script
 
The following is the script (using JavaScript).
 
Here, I created three functions. Function c(val) clears the data from the display. When we click on the "C" button, then the onclick='c("")' event runs and searches for the c(val) function and displays the value depending on the parameter ed to it (here, we have not ed any parameter so the input screen appears blank or clear).
 
Function v(val) is used for typing numbers as well as mathematical operators.
 
Function e() is used for evaluating, in other words when clicking the "=" button, the value inside the Id="d" is solved.
  1. function c(val)  
  2. {  
  3. document.getElementById("d").value=val;  
  4. }  
  5. function v(val)  
  6. {document.getElementById("d").value+=val;  
  7. }  
  8. function e()  
  9. {  
  10. try  
  11.     {  
  12.      c(eval(document.getElementById("d").value))  
  13.     }  
  14.     catch(e)  
  15.     {  
  16.      c('Error') }  
You can also download the source file for viewing.
 
Working Preview
 
calculator working.gif
 
That's All. Hope you like it.