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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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.

            
              <div id="calculator">
	<div class="top">
		<span class="clear">C</span>
		<div class="screen"></div>
	</div>
	
	<div class="keys">
		<span>7</span>
		<span>8</span>
		<span>9</span>
		<span class="operator">+</span>
		<span>4</span>
		<span>5</span>
		<span>6</span>
		<span class="operator">-</span>
		<span>1</span>
		<span>2</span>
		<span>3</span>
		<span class="operator">÷</span>
		<span>0</span>
		<span>.</span>
		<span class="eval">=</span>
		<span class="operator">x</span>
	</div>
</div>

            
          
!
            
              * {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
	font: normal 25px Arial, sans-serif;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

html {
	height: 100%;
	background: url(https://33.media.tumblr.com/d4a884274dff05760e01bd3bdcc6defb/tumblr_nb1ulnMaP91st5lhmo1_1280.jpg);
	background-size: cover;
}


#calculator {
	width: 325px;
	height: auto;
	overflow:hidden;
	margin: 100px auto;
	padding: 20px 20px 9px;
	position:relative;
	background: rgba(0,0,0,0.3);
	border-radius: 3px;
	box-shadow:  0px 10px 15px rgba(0, 0, 0, 0.2);
}


.top span.clear {
	position:absolute;
  z-index:6;
 
}


.top .screen {
	height: 80px;
	width: 100%;
	position:absolute;
  top:0; left:0;
	padding: 20px 10px;
	background: rgba(0, 0, 0, 0.2);
	font-size: 27px;
  font-weight:normal;
	line-height: 40px;
	color: white;
	text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
	text-align: right;
	letter-spacing: 1px;
}

.keys, .top {overflow: hidden;}


.keys span, .top span.clear {
	float: left;
	position: relative;
	top: 0;
	
	cursor: pointer;
	
	width: 66px;
	height: 56px;
	
	background: rgba(0,0,0,0.5);
	border-radius: 3px;
	box-shadow: 0px 4px rgba(0, 0, 0, 0.2);
	
	margin: 0 7px 11px 0;
	
	color: #ccc;
	line-height: 56px;
	text-align: center;
	
	user-select: none;
	

	transition: all 0.2s ease;
}


.keys span.operator {
	background: rgba(250,100,0,0.3);
	margin-right: 0;
}

.keys span.eval {

	color: #888e5f;
}

.top span.clear {
	background: transparent;
	box-shadow: none;
	color: #aaa;
  width:56px;
  left:5px
}


.keys span:hover {
	background: rgba(255,255,255,0.8);
	color: #333;
}

.keys span.eval:hover {
	background: #abb850;
	color: #ffffff;
}

.top span.clear:hover {
  border-radius:190px;
	background: rgba(0,0,0,0.1);
	color: white;
}

.keys span:active {
	box-shadow: 0px 0px rgba(0, 0, 0, 0.2);
	top: 4px;
}

.keys span.eval:active {
	box-shadow: 0px 0px #717a33;
	top: 4px;
}

.top span.clear:active {
	top: 4px;
	box-shadow: 0px 0px #d3545d;
}

            
          
!
            
              var keys = document.querySelectorAll('#calculator span');
var operators = ['+', '-', 'x', '÷'];
var decimalAdded = false;


for(var i = 0; i < keys.length; i++) {
	keys[i].onclick = function(e) {
		
		var input = document.querySelector('.screen');
		var inputVal = input.innerHTML;
		var btnVal = this.innerHTML;
		var total;
		if(btnVal == 'C') {
			input.innerHTML = '';
			decimalAdded = false;
		}
		
		else if(btnVal == '=') {
			var equation = inputVal;
			var lastChar = equation[equation.length - 1];
			
			equation = equation.replace(/x/g, '*').replace(/÷/g, '/');
			
			if(operators.indexOf(lastChar) > -1 || lastChar == '.')
				equation = equation.replace(/.$/, '');
			
			if(equation)
				{
             total = eval(equation);
              if(total.toString().indexOf('.') != -1)
                total= total.toFixed(2);
          
          input.innerHTML = total;
        }
				
			decimalAdded = false;
		}
		
		
		else if(operators.indexOf(btnVal) > -1) {
			
			var lastChar = inputVal[inputVal.length - 1];
			
			if(inputVal != '' && operators.indexOf(lastChar) == -1) 
				input.innerHTML += btnVal;
			
			else if(inputVal == '' && btnVal == '-') 
				input.innerHTML += btnVal;
			
			if(operators.indexOf(lastChar) > -1 && inputVal.length > 1) {
				input.innerHTML = inputVal.replace(/.$/, btnVal);
			}
			
			decimalAdded =false;
		}
		
		else if(btnVal == '.') {
			if(!decimalAdded) {
				input.innerHTML += btnVal;
				decimalAdded = true;
			}
		}
		
		else {
			input.innerHTML += btnVal;
		}
		
		e.preventDefault();
	} 
}
            
          
!
999px
Loading ..................

Console