Pen Settings

HTML

CSS

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.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="calculator">
  <!-- Screen and clear key -->
  <div class="top">
    <span class="clear" data-value="C">C</span>
    <div class="input"></div>
    <div class="result"></div>
  </div>

  <div class="keys">
    <!-- operators and other keys -->
    <span data-value="7">7</span>
    <span data-value="8">8</span>
    <span data-value="9">9</span>
    <span class="operator" data-value="/">/</span>
    <span data-value="4">4</span>
    <span data-value="5">5</span>
    <span data-value="6">6</span>
    <span class="operator" data-value="*">x</span>
    <span data-value="1">1</span>
    <span data-value="2">2</span>
    <span data-value="3">3</span>
    <span class="operator" data-value="-">-</span>
    <span data-value="0">0</span>
    <span data-value=".">.</span>
    <span class="equals" data-value="=">=</span>
    <span class="operator" data-value="+">+</span>
  </div>
</div>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Poiret+One);
@import url(https://fonts.googleapis.com/css?family=Cutive);
* {
  box-sizing: border-box;
  font-family: 'Open Sans', Helvetica, Arial, sans-serif;
  font-size: 24px;
  font-size: 4vw;
  font-weight: 300;
  margin: 0;
  padding: 0;
}

html {
  height: 100%;
  background-color: pink;
  background-size: cover;
}

#calculator {
  width: 12.5em;
  height: auto;
  background: #ED5565;
  margin: 10vh auto 1em;
  padding: 30px;
  border-right: 5px solid #DA4453;
  border-bottom: 5px solid #DA4453;
  box-shadow: 10px 10px 0px -4px rgba(0, 0, 0, 0.2), inset -1px -1px 0 1px rgba(255, 255, 255, 0.1), inset -5px -5px 15px 10px rgba(255, 255, 255, 0.1), inset 5px 10px 10px 12px rgba(0, 0, 0, 0.05);
}

.top span.clear {
  float: left;
  box-shadow: 0 4px #ff7c87;
  color: #FFF;
}

.top .input {
  height: 2.8em;
  width: 7.5em;
  padding-right: 1em;
  float: right;
  color: rgba(0, 0, 0, 0.7);
  line-height: 3em;
  text-align: right;
  letter-spacing: 1px;
  background-image: url(http://api.thumbr.it/whitenoise-361x370.png?background=f5f3edff&noise=96917b&density=58&opacity=11);
  border: 4px solid #DA4453;
  margin: 0 0 5px 0;
  box-shadow: inset 1px 1px 0 1px rgba(0, 0, 0, 0.1), inset -5px -5px 15px 10px rgba(255, 255, 255, 0.2), inset 5px 10px 10px 12px rgba(0, 0, 0, 0.05);
  font-family: "cutive";
}

.keys,
.top {
  overflow: hidden;
}

.keys span,
.top span.clear {
  float: left;
  cursor: pointer;
  width: 3em;
  height: 3em;
  line-height: 2.9em;
  color: #fff;
  text-align: center;
  user-select: none;
  transition: background-color .2s ease;
  border: 3px solid #ddd;
  border-radius: 2em;
  background: #222;
  margin: 0.25em;
  box-shadow: 10px 10px 0px -4px rgba(0, 0, 0, 0.2), 0 0 0 2px #aaa, 0 0 0 3px #888, inset 2px 1px 0 2px rgba(0, 0, 0, 0.5), inset -1px -1px 0 5px rgba(255, 255, 255, 0.2), inset -5px -5px 15px 10px rgba(255, 255, 255, 0.1), inset 5px 10px 10px 12px rgba(0, 0, 0, 0.13);
  position: relative;
  font-family: "poiret one";
  font-size: 0.75em;
}

.top span.clear {
  background: #EB9532;
}

.keys span.operator {
  background: #bf2b3d;
  color: #fff;
}

.keys span.operator:hover {
  background: #a0202f;
}

.keys span:hover {
  background: #111;
  color: #FFF;
  top: 2px;
}

.top span.clear:hover {
  background: #E87E04;
  color: #FFF;
  top: 2px;
}

span:active {
  top: 5px;
}

@media (max-width: 810px) {
  #calculator {
    width: 13em;
  }
}

@media (max-width: 700px) {
  #calculator {
    width: 13.2em;
  }
}

@media (max-width: 590px) {
  #calculator {
    width: 14em;
  }
}

@media (max-width: 500px) {
  #calculator {
    width: 14.8em;
  }
  .keys span,
  .top span.clear {
    margin: 0.25em;
    box-shadow: 8px 8px 0px -4px rgba(0, 0, 0, 0.2), 0 0 0 1px #aaa, 0 0 0 1px #888, inset 2px 1px 0 2px rgba(0, 0, 0, 0.5), inset -1px -1px 0 2px rgba(255, 255, 255, 0.2), inset -5px -5px 15px 5px rgba(255, 255, 255, 0.1), inset 5px 10px 10px 5px rgba(0, 0, 0, 0.13);
    position: relative;
    font-family: "poiret one";
    font-size: 0.75em;
    border: 1px solid #ddd;
  }
  @media (max-width: 400px) {
    #calculator {
      width: 15.5em;
    }
  }
              
            
!

JS

              
                // Get all the keys from document
var keys = document.querySelectorAll('#calculator span');
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('.input');
		var inputVal = input.innerHTML;
		var btnVal = this.getAttribute("data-value");
		
		// 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

Console