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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <div id="calculator">
	<div class="top">
		<span class="clear">C</span>
		<div class="screen"></div>
	<div class="keys">
		<span class="operator">+</span>
		<span class="operator">-</span>
		<span class="operator">÷</span>
		<span class="eval">=</span>
		<span class="operator">x</span>

              * {
	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;
	margin: 100px auto;
	padding: 20px 20px 9px;
	background: rgba(0,0,0,0.3);
	border-radius: 3px;
	box-shadow:  0px 10px 15px rgba(0, 0, 0, 0.2);

.top span.clear {

.top .screen {
	height: 80px;
	width: 100%;
  top:0; left:0;
	padding: 20px 10px;
	background: rgba(0, 0, 0, 0.2);
	font-size: 27px;
	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;

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

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

.top span.clear:hover {
	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(/.$/, '');
             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;
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.