cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <body ontouchstart="">

<div id="calc_box">
   
   <div class="screen"></div>
   
   <div id="buttons">
   <div class="button">C</div>
   
   <div class="button">&divide;</div>
   
   <div class="button">7</div>
   
   <div class="button">8</div>
   
   <div class="button">9</div>
   
   <div class="button">x</div>
   
   <div class="button">4</div>
   
   <div class="button">5</div>
   
   <div class="button">6</div>
   
   <div class="button">&minus;</div>
      
   <div class="button">1</div>
      
   <div class="button">2</div>
      
   <div class="button">3</div>
      
   <div class="button">&#43;</div>
      
   <div class="button">0</div>
      
      <div class="button">.</div>
      
   <div class="button">&#61;</div>
      
</div>
   
</div>
   
</body>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Roboto|Share+Tech+Mono);

$default_back:#222;
$default_text:#777;

body {
   margin:0;
   background-color:teal;
}

#calc_box {
   width:250px;
   height:350px;
   background-color:$default_back;
   margin:10vh auto;
   border-radius:3px;
   border-left:1px solid #555;
   border-top:1px solid #555;
   border-bottom:3px solid #555;
   border-right:3px solid #555;
   
   -webkit-box-shadow: 10px 10px 1px 1px #111;
   -moz-box-shadow: 10px 10px 1px 1px #111;
   box-shadow: 10px 10px 1px 1px #111;
}

.screen {
   background-color:$default_text;
   width:95%;
   height:15%;
   position:relative;
   margin:0 auto;
   color:$default_back;
   border-radius:3px;
   top:2%;
   border-top:1px solid #444;
   border-left:2px solid #444;
   font-family:'Share Tech Mono',monospace;
   text-align:right;
   font-size:3em;
   overflow:hidden;
}

#buttons {
   width:100%;
   height:82%;
   background-color:$default_back;
   position:relative;
   top:3%;
   border-radius:3px;
}

.button {
   box-sizing:border-box;
   display:block;
   float:left;
   width:22%;
   height:18%;
   border-right:2px solid $default_text;
   border-bottom:2px solid $default_text;
   border-left:1px solid #444;
   border-top:1px solid #444;
   font-weight:400;
   color:$default_text;
   font-family:'Roboto', sans-serif;
   text-align:center;
   font-size:2.5em;
   margin-left:2.5%;
   margin-bottom:2%;
   border-radius:3px;
   
   &:hover {
      cursor:pointer;
   }
   
   &:active {
      border:1px solid #444;
   }
}

.button:nth-child(1) {
   width:71%;
}

.button:nth-child(15) {
   width:46.5%;
}
            
          
!
            
              //Javascript source: http://thecodeplayer.com/walkthrough/javascript-css3-calculator

// Get all the keys from document
var keys = document.querySelectorAll('.button');
var operators = ['÷', 'x', '-', '+'];
var decimalAdded = false;

// Add onclick event to all the keys and perform operations
for(var i = 0; i < keys.length; i++) {
	keys[i].onclick = function(e) {
		// Get the input and button values
		var input = document.querySelector('.screen');
		var inputVal = input.innerHTML;
		var btnVal = this.innerHTML;
		
		// Now, just append the key values (btnValue) to the input string and finally use javascript's eval function to get the result
		// If clear key is pressed, erase everything
		if(btnVal == 'C') {
			input.innerHTML = '';
			decimalAdded = false;
		}
		
		// If eval key is pressed, calculate and display the result
		else if(btnVal == '=') {
			var equation = inputVal;
			var lastChar = equation[equation.length - 1];
			
			// Replace all instances of x and ÷ with * and / respectively. This can be done easily using regex and the 'g' tag which will replace all instances of the matched character/substring
			equation = equation.replace(/x/g, '*').replace(/÷/g, '/');
			
			// Final thing left to do is checking the last character of the equation. If it's an operator or a decimal, remove it
			if(operators.indexOf(lastChar) > -1 || lastChar == '.')
				equation = equation.replace(/.$/, '');
			
			if(equation)
				input.innerHTML = eval(equation);
				
			decimalAdded = false;
		}
		
		// Basic functionality of the calculator is complete. But there are some problems like 
		// 1. No two operators should be added consecutively.
		// 2. The equation shouldn't start from an operator except minus
		// 3. not more than 1 decimal should be there in a number
		
		// We'll fix these issues using some simple checks
		
		// indexOf works only in IE9+
		else if(operators.indexOf(btnVal) > -1) {
			// Operator is clicked
			// Get the last character from the equation
			var lastChar = inputVal[inputVal.length - 1];
			
			// Only add operator if input is not empty and there is no operator at the last
			if(inputVal != '' && operators.indexOf(lastChar) == -1) 
				input.innerHTML += btnVal;
			
			// Allow minus if the string is empty
			else if(inputVal == '' && btnVal == '-') 
				input.innerHTML += btnVal;
			
			// Replace the last operator (if exists) with the newly pressed operator
			if(operators.indexOf(lastChar) > -1 && inputVal.length > 1) {
				// Here, '.' matches any character while $ denotes the end of string, so anything (will be an operator in this case) at the end of string will get replaced by new operator
				input.innerHTML = inputVal.replace(/.$/, btnVal);
			}
			
			decimalAdded =false;
		}
		
		// Now only the decimal problem is left. We can solve it easily using a flag 'decimalAdded' which we'll set once the decimal is added and prevent more decimals to be added once it's set. It will be reset when an operator, eval or clear key is pressed.
		else if(btnVal == '.') {
			if(!decimalAdded) {
				input.innerHTML += btnVal;
				decimalAdded = true;
			}
		}
		
		// if any other key is pressed, just append it
		else {
			input.innerHTML += btnVal;
		}
		
		// prevent page jumps
		e.preventDefault();
	} 
}
            
          
!
999px
Loading ..................

Console