Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <main class="main">
      <div class="calculator">
        <div class="screen">
          <div class="screen-last" id="lastOperationScreen"></div>
          <div class="screen-current" id="currentOperationScreen">0</div>
        </div>
        <div class="buttons-grid">
          <button
            class="btn btn-red span-2"
            id="clearBtn"
            onclick="this.blur();"
          >
            CLEAR
          </button>
          <button
            class="btn btn-blue span-2"
            id="deleteBtn"
            onclick="this.blur();"
          >
            DELETE
          </button>
          <button class="btn" onclick="this.blur();" data-number>7</button>
          <button class="btn" onclick="this.blur();" data-number>8</button>
          <button class="btn" onclick="this.blur();" data-number>9</button>
          <button class="btn" onclick="this.blur();" data-operator>÷</button>

          <button class="btn" onclick="this.blur();" data-number>4</button>
          <button class="btn" onclick="this.blur();" data-number>5</button>
          <button class="btn" onclick="this.blur();" data-number>6</button>
          <button class="btn" onclick="this.blur();" data-operator>×</button>

          <button class="btn" onclick="this.blur();" data-number>1</button>
          <button class="btn" onclick="this.blur();" data-number>2</button>
          <button class="btn" onclick="this.blur();" data-number>3</button>
          <button class="btn" onclick="this.blur();" data-operator>−</button>

          <button class="btn" onclick="this.blur();" id="pointBtn">.</button>
          <button class="btn" onclick="this.blur();" data-number>0</button>
          <button class="btn" onclick="this.blur();" id="equalsBtn">=</button>
          <button class="btn" onclick="this.blur();" data-operator>+</button>
        </div>
      </div>
    </main>
              
            
!

CSS

              
                :root {
  --background: #f6f6f6;
  --font: #222;
  --border: #333;
  --screen: #eee;
  --calculator: #aaa;
  --btn: #eee;
  --btn-hover: #ddd;
  --btn-active: #ccc;
  --btn-red: #faa;
  --btn-red-hover: #e99;
  --btn-red-active: #d88;
  --btn-blue: #aaf;
  --btn-blue-hover: #99e;
  --btn-blue-active: #88d;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

html {
  /* footer support */
  position: relative;
  min-height: 100%;
}

body {
  background-color: var(--background);
  color: var(--font);
  font-family: 'Roboto', sans-serif;
  line-height: 1.6;
  /* footer support */
  margin-bottom: 100px;
}

button {
  border: none;
  color: inherit;
  font-family: inherit;
  font-size: inherit;
  cursor: pointer;
  outline: none;
}

/* MAIN */

.main {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 75vh;
  min-height: 500px;
  padding: 10px;
  margin-top: 50px;
}

.calculator {
  width: 400px;
  border: 2px var(--border) solid;
  border-radius: 15px;
  padding: 20px;
  background-color: var(--calculator);
}

.screen {
  padding: 10px 20px;
  border: 2px var(--border) solid;
  border-radius: 5px;
  margin-bottom: 20px;
  background-color: var(--screen);
  text-align: right;
  word-wrap: break-word;
  word-break: break-all;
}

.screen-last {
  min-height: 32px;
  font-size: 20px;
}

.screen-current {
  min-height: 64px;
  font-size: 40px;
}

.buttons-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-gap: 10px;
}

.span-2 {
  grid-column: span 2;
}

.btn {
  padding: 20px;
  border: 2px var(--border) solid;
  border-radius: 5px;
  background-color: var(--btn);
  font-size: 20px;
}

.btn:hover {
  background-color: var(--btn-hover);
}

.btn:active {
  background-color: var(--btn-active);
}

.btn-red {
  background-color: var(--btn-red);
}

.btn-red:hover {
  background-color: var(--btn-red-hover);
}

.btn-red:active {
  background-color: var(--btn-red-active);
}

.btn-blue {
  background-color: var(--btn-blue);
}

.btn-blue:hover {
  background-color: var(--btn-blue-hover);
}

.btn-blue:active {
  background-color: var(--btn-blue-active);
}

/* Disables input spinners in calc screen */
input[type='number']::-webkit-inner-spin-button,
input[type='number']::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
              
            
!

JS

              
                let firstOperand = ''
let secondOperand = ''
let currentOperation = null
let shouldResetScreen = false

const numberButtons = document.querySelectorAll('[data-number]')
const operatorButtons = document.querySelectorAll('[data-operator]')
const equalsButton = document.getElementById('equalsBtn')
const clearButton = document.getElementById('clearBtn')
const deleteButton = document.getElementById('deleteBtn')
const pointButton = document.getElementById('pointBtn')
const lastOperationScreen = document.getElementById('lastOperationScreen')
const currentOperationScreen = document.getElementById('currentOperationScreen')

window.addEventListener('keydown', handleKeyboardInput)
equalsButton.addEventListener('click', evaluate)
clearButton.addEventListener('click', clear)
deleteButton.addEventListener('click', deleteNumber)
pointButton.addEventListener('click', appendPoint)

numberButtons.forEach((button) =>
  button.addEventListener('click', () => appendNumber(button.textContent))
)

operatorButtons.forEach((button) =>
  button.addEventListener('click', () => setOperation(button.textContent))
)

function appendNumber(number) {
  if (currentOperationScreen.textContent === '0' || shouldResetScreen)
    resetScreen()
  currentOperationScreen.textContent += number
}

function resetScreen() {
  currentOperationScreen.textContent = ''
  shouldResetScreen = false
}

function clear() {
  currentOperationScreen.textContent = '0'
  lastOperationScreen.textContent = ''
  firstOperand = ''
  secondOperand = ''
  currentOperation = null
}

function appendPoint() {
  if (shouldResetScreen) resetScreen()
  if (currentOperationScreen.textContent === '')
    currentOperationScreen.textContent = '0'
  if (currentOperationScreen.textContent.includes('.')) return
  currentOperationScreen.textContent += '.'
}

function deleteNumber() {
  currentOperationScreen.textContent = currentOperationScreen.textContent
    .toString()
    .slice(0, -1)
}

function setOperation(operator) {
  if (currentOperation !== null) evaluate()
  firstOperand = currentOperationScreen.textContent
  currentOperation = operator
  lastOperationScreen.textContent = `${firstOperand} ${currentOperation}`
  shouldResetScreen = true
}

function evaluate() {
  if (currentOperation === null || shouldResetScreen) return
  if (currentOperation === '÷' && currentOperationScreen.textContent === '0') {
    alert("You can't divide by 0!")
    return
  }
  secondOperand = currentOperationScreen.textContent
  currentOperationScreen.textContent = roundResult(
    operate(currentOperation, firstOperand, secondOperand)
  )
  lastOperationScreen.textContent = `${firstOperand} ${currentOperation} ${secondOperand} =`
  currentOperation = null
}

function roundResult(number) {
  return Math.round(number * 1000) / 1000
}

function handleKeyboardInput(e) {
  if (e.key >= 0 && e.key <= 9) appendNumber(e.key)
  if (e.key === '.') appendPoint()
  if (e.key === '=' || e.key === 'Enter') evaluate()
  if (e.key === 'Backspace') deleteNumber()
  if (e.key === 'Escape') clear()
  if (e.key === '+' || e.key === '-' || e.key === '*' || e.key === '/')
    setOperation(convertOperator(e.key))
}

function convertOperator(keyboardOperator) {
  if (keyboardOperator === '/') return '÷'
  if (keyboardOperator === '*') return '×'
  if (keyboardOperator === '-') return '−'
  if (keyboardOperator === '+') return '+'
}

function add(a, b) {
  return a + b
}

function substract(a, b) {
  return a - b
}

function multiply(a, b) {
  return a * b
}

function divide(a, b) {
  return a / b
}

function operate(operator, a, b) {
  a = Number(a)
  b = Number(b)
  switch (operator) {
    case '+':
      return add(a, b)
    case '−':
      return substract(a, b)
    case '×':
      return multiply(a, b)
    case '÷':
      if (b === 0) return null
      else return divide(a, b)
    default:
      return null
  }
}
              
            
!
999px

Console