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

              
                <form class="calc">
	<input class="calc__screen" type="text" name="output" maxlength="5" value="0" readonly>
	<div class="calc__btns">
		<button class="calc__btn" type="button" data-fn="7" ontouchstart="">7</button>
		<button class="calc__btn" type="button" data-fn="8" ontouchstart="">8</button>
		<button class="calc__btn" type="button" data-fn="9" ontouchstart="">9</button>
		<button class="calc__btn" type="button" data-fn="C" ontouchstart="">C</button>
		<button class="calc__btn" type="button" data-fn="/" ontouchstart="">&divide;</button>
		<button class="calc__btn" type="button" data-fn="*" ontouchstart="">&times;</button>
		<button class="calc__btn" type="button" data-fn="4" ontouchstart="">4</button>
		<button class="calc__btn" type="button" data-fn="5" ontouchstart="">5</button>
		<button class="calc__btn" type="button" data-fn="6" ontouchstart="">6</button>
		<button class="calc__btn" type="button" data-fn="." ontouchstart="">.</button>
		<button class="calc__btn" type="button" data-fn="-" ontouchstart="">-</button>
		<button class="calc__btn" type="button" data-fn="+" ontouchstart="">+</button>
		<button class="calc__btn" type="button" data-fn="1" ontouchstart="">1</button>
		<button class="calc__btn" type="button" data-fn="2" ontouchstart="">2</button>
		<button class="calc__btn" type="button" data-fn="3" ontouchstart="">3</button>
		<button class="calc__btn" type="button" data-fn="0" ontouchstart="">0</button>
		<button class="calc__btn calc__btn--wide" type="button" data-fn="=" ontouchstart="">=</button>
	</div>
</form>
              
            
!

CSS

              
                * {
	border: 0;
	box-sizing: border-box;
	margin: 0;
	padding: 0;
}
:root {
	font-size: calc(20px + (30 - 20) * (100vw - 320px) / (1280 - 320));
}
body, button, input {
	color: #000;
	font: 1em/1.5 "Press Start 2P", sans-serif;
}
body {
	background-color: #55abaa;
	display: grid;
	place-items: center;
	height: 100vh;
}
.calc {
	background-color: #188080;
	box-shadow:
		0 -0.4em 0 #188080,
		-0.4em 0 0 #188080,
		0 -0.4em 0 #120180 inset,
		-0.4em 0 0 #120180 inset,
		0 0.4em 0 #fff inset,
		0.4em 0 0 #fff inset,
		0 0.4em 0 #000,
		0.4em 0 0 #000;
	margin: 0.4em;
	padding: 0.4em 1.2em;
	position: relative;
	width: 11.6em;
	height: 7.6em;
}
.calc:before {
	background-color: inherit;
	box-shadow:
		0 -0.4em 0,
		0 -1.2em 0,
		0 -2.0em 0,
		0 -2.8em 0,
		0 -3.6em 0,
		0 -4.4em 0,
		0 -5.2em 0,
		0 -6.0em 0,
		0 -6.8em 0,
		0.4em -7.2em 0,
		1.2em -7.2em 0,
		2.0em -7.2em 0,
		2.8em -7.2em 0,
		3.6em -7.2em 0,
		4.4em -7.2em 0,
		5.2em -7.2em 0,
		6.0em -7.2em 0,
		6.8em -7.2em 0,
		7.6em -7.2em 0,
		8.4em -7.2em 0,
		9.2em -7.2em 0,
		10.0em -7.2em 0,
		10.8em -7.2em 0;
	color: #3afdff;
	content: "";
	display: block;
	position: absolute;
	bottom: 0;
	left: 0;
	width: 0.4em;
	height: 0.4em;
}
.calc__btns {
	display: grid;
	grid-template-columns: repeat(6,1fr);
	grid-template-rows: repeat(3,1fr);
	grid-gap: 0.4em;
}
.calc__btn {
	background-color: #c0c0c0;
	box-shadow:
		-0.4rem -0.4rem 0 #000 inset,
		0.4rem 0.4rem 0 #fff inset;
	cursor: pointer;
	font-size: 0.5em;
	padding: 0 0.4rem 0.4rem 0;
	width: 1.2rem;
	height: 1.2rem;
	-webkit-tap-highlight-color: #0000;
}
.calc__btn:active {
	box-shadow:
		0.4rem 0.4rem 0 #188080 inset,
		0.8rem 0.8rem 0 #fff inset;
	padding: 0.4rem 0 0 0.4rem;
}
.calc__btn--wide {
	grid-column: 5 / 7;
	width: 2.8rem;
}
.calc__screen {
	background-color: #fff;
	box-shadow:
		0.4em 0.4em 0 #000 inset,
		0 0.8em 0 #c0c0c0 inset,
		-0.4em -0.4em 0 #c0c0c0 inset;
	margin-bottom: 0.4em;
	padding: 0.4em 0 0 0.4em;
	width: 6em;
	height: 1.6em;
	text-align: right;
}
.calc__screen, .calc__btn {
	-webkit-appearance: none;
	appearance: none;
}
              
            
!

JS

              
                document.addEventListener("DOMContentLoaded",app);

function app() {
	let calc = document.forms[0],
		isCalcing = false,
		isError = false,
		val = 0,
		op = "",
		exp = [],
		lastPart = "",
		lastOperand = 0,
		action = e => {
			let kbdClick = e.keyCode == 9 || e.keyCode == 32;
			
			if (!kbdClick) {
				let tar = e.target || e,
					fn = tar.getAttribute("data-fn") || fnByKey(e.keyCode,e.shiftKey,e.altKey),
					output = calc.output;

				if (fn && output) {
					let maxLen = +output.maxLength,
						signs = /[\/\*\-\+]/,
						fnIsNum = !isNaN(fn),
						fnIsDec = fn == ".",
						fnIsSign = fn == "+-",
						fnIsOp = "/*-+".indexOf(fn) > -1,
						fnIsEquals = fn == "=",
						fnIsClear = fn == "C";

					if (!isError) {
						if (exp.length)
							lastPart = exp[exp.length - 1];

						if (fnIsNum || fnIsDec || fnIsSign || fnIsOp) {
							// start a new expression if `=` was previously pressed
							if (!isCalcing) {
								isCalcing = true;
								if (fnIsNum || fnIsDec) {
									op = "";
									exp = ["0"];
									lastPart = exp[0];
									lastOperand = 0;
								}
							}
							if (!exp.length)
								exp.push("0");
						}
						// number
						if (fnIsNum) {
							if (isNaN(lastPart)) {
								exp.push(fn);
							} else if (lastPart.length < maxLen) {
								let numToAdd = lastPart + fn;
								if (numToAdd[0] == "0" && numToAdd[1] != ".")
									numToAdd = numToAdd.substr(1);

								exp[exp.length - 1] = numToAdd;
							}
						// decimal point
						} else if (fnIsDec) {
							if (lastPart.indexOf(".") == -1) {
								if (isNaN(lastPart))
									exp.push("0.");
								else if (lastPart.length < maxLen - 1)
									exp[exp.length - 1] += fn;
							}
						// toggle sign
						} else if (fnIsSign) {
							if (exp.length) {
								if  (!isNaN(lastPart))
									exp[exp.length - 1] = String(-exp[exp.length - 1]);
								else
									exp[0] = String(-exp[0]);
							}
						// operation
						} else if (fnIsOp) {
							op = fn;
							// switch operator
							if (isNaN(lastPart)) {
								exp[exp.length - 1] = op;
							// calculate the current expression before using the operator
							} else {
								let curExp = exp.join(" ");
								if (signs.test(curExp)) {
									exp = [`${solve(exp[0],exp[1],exp[2])}`];
									exp[0] = nearest1000th(exp[0]);
									lastPart = exp[0];
									val = lastPart;
								}
								exp.push(op);
							}
						// equals
						} else if (fnIsEquals) {
							isCalcing = false;

							if (op && exp.indexOf(op) > -1)
								lastOperand = lastPart;
							// equal again
							if (lastOperand && exp.indexOf(op) == -1) {
								exp = [`${solve(val,op,lastOperand)}`];
							// normal equals
							} else if (!isNaN(lastOperand)) {
								exp = [`${solve(exp[0],op,lastOperand)}`];
							// equals without second operand
							} else {
								lastOperand = exp[0];
								exp = [`${solve(val,op,val)}`];
							}
							exp[0] = nearest1000th(exp[0]);
						}
						if (fn != "C") {
							lastPart = exp[exp.length - 1];
							if (!isNaN(lastPart) || lastPart == "NaN")
								val = lastPart;
							// deal with integer overflow and infinity, or 0 / 0
							if (val <= -1e4 || val >= 1e5 || val == "NaN")
								isError = true;
							// update the display
							let outputVal = isError ? "ERROR" : String(val);
							if (!isError) {
								// fit the value into the screen without ending with a decimal point
								if ((val >= -1e3 && val < -1e2) || (val >= 1e3 && val < 1e4))
									outputVal = outputVal.substr(0,maxLen - 1);
								else
									outputVal = outputVal.substr(0,maxLen);
							}
							output.value = outputVal;
						}
					}
					// clear
					if (fnIsClear) {
						isCalcing = false;
						isError = false;
						val = 0;
						op = "";
						exp = [];
						lastPart = "";
						lastOperand = 0;
						output.value = val;
					}
					//console.log(exp);
				}
			}
		},
		fnByKey = (kc,isShift,isAlt) => {
			let fn = "";
			switch (kc) {
				case 12:
				case 27:
				case 67:
					fn = "C";
					break;
				case 48:
				case 96:
					fn = "0";
					break;
				case 49:
				case 97:
					fn = "1";
					break;
				case 50:
				case 98:
					fn = "2";
					break;
				case 51:
				case 99:
					fn = "3";
					break;
				case 52:
				case 100:
					fn = "4";
					break;
				case 53:
				case 101:
					fn = "5";
					break;
				case 54:
				case 102:
					fn = "6";
					break;
				case 55:
				case 103:
					fn = "7";
					break;
				case 56:
					fn = isShift ? "*" : "8";
					break;
				case 104:
					fn = "8";
					break;
				case 57:
				case 105:
					fn = "9";
					break;
				case 106:
					fn = "*";
					break;
				case 13:
					fn = "=";
					break;
				case 107:
					fn = "+";
					break;
				case 187:
					fn = isShift ? "+" : "=";
					break;
				case 109:
				case 189:
					fn = isAlt ? "+-" : "-";
					break;
				case 110:
				case 190:
					fn = ".";
					break;
				case 111:
				case 191:
					fn = "/";
					break;
				default:
					// do nothing
					break;
			}
			return fn;
		},
		nearest1000th = n => {
			let r = Math.round(n / 1e-3) * 1e-3;
			return String(+r.toFixed(4));
		},
		solve = (A,op,B) => {
			let r = 0,
				a = !isNaN(A) ? +A : r,
				b = !isNaN(B) ? +B : a;

			switch (op) {
				case "/":
					r = a / b;
					break;
				case "*":
					r = a * b;
					break;
				case "-":
					r = a - b;
					break;
				case "+":
					r = a + b;
					break;
				default:
					r = a;
					break;
			}
			return r;
		};

	calc.addEventListener("click",action);
	document.addEventListener("keydown",action);
}
              
            
!
999px

Console