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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

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.

HTML

            
              <section></section>
            
          
!

CSS

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

html {
  font-family: sans-serif;
  line-height: 1.15;
  -webkit-text-size-adjust: 100%;
  -ms-text-size-adjust: 100%;
  -ms-overflow-style: scrollbar;
  -webkit-tap-highlight-color: transparent;
  font-size: 62.5%;
  height: 100%;
}

@-ms-viewport {
  width: device-width;
}

article, aside, dialog, figcaption, figure, footer, header, hgroup, main, nav, section {
  display: block;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  font-size: 1.4rem;
  font-weight: normal;
  line-height: 1.5;
  color: #212529;
  text-align: left;
  background-color: #2f98e4;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  overflow: auto;
}
section {
  display: flex;
  flex: 1;
  box-sizing: border-box;
  overflow: auto;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  min-height: 540px;
}

.calculator {
  width: 320px;
  height: 480px;
  display: inline-flex;
  flex-direction: column;
  box-shadow: 0px 10px 20px 0px rgba(0,0,0,0.5);
}

.calculator-display {
  color: white;
  background: #1c191c;
  line-height: 130px;
  font-size: 6em;
  position: relative;
  padding: 0 10px;
}

.auto-scaling-text {
  display: inline-block;
  padding: 0 30px;
  position: absolute;
  right: 0;
  transform-origin: right;
}

button {
  display: block;
  background: none;
  border: none;
  padding: 0;
  font-family: inherit;
  user-select: none;
  cursor: pointer;
  outline: none;

  -webkit-tap-highlight-color: rgba(0,0,0,0);
}

button:active {
  box-shadow: inset 0px 0px 80px 0px rgba(0,0,0,0.25);
}
.calculator-key {
  width: 80px;
  height: 70px;
  border-top: 1px solid #777;
  border-right: 1px solid #666;
  text-align: center;
  line-height: 70px;
}

.calculator .input-keys {
  width: 240px;
}

.calculator .function-keys {
  display: flex;
}

.calculator .digit-keys {
  background: #e0e0e7;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

.calculator-keypad {
  height: 350px;
  display: flex;
}

.calculator .function-keys .calculator-key {
  font-size: 2em;
}

.calculator .function-keys .key-multiply {
  line-height: 50px;
}

.calculator .digit-keys .calculator-key {
  font-size: 2.25em;
}

.calculator .digit-keys .key-0 {
  width: 160px;
  text-align: left;
  padding-left: 32px;
}

.calculator .digit-keys .key-dot {
  padding-top: 1em;
  font-size: 0.75em;
}

.calculator .operator-keys .calculator-key {
  color: white;
  border-right: 0;
  font-size: 3em;
}

.calculator .function-keys {
  background: linear-gradient(to bottom, rgba(202,202,204,1) 0%, rgba(196,194,204,1) 100%);
}

.calculator .operator-keys {
  background:  linear-gradient(to bottom, rgba(252,156,23,1) 0%, rgba(247,126,27,1) 100%);
}

            
          
!

JS

            
              /** @jsx h */
const { h, Component} = composi

const DigitKeys = [7, 8, 9, 4, 5, 6, 1, 2, 3, 0]

const CalculatorOperations = [
  {
    key: '/',
    name: 'divide',
    symbol: '÷',
    func: (prevValue, nextValue) => prevValue / nextValue
  },
  {
    key: '*',
    name: 'multiply',
    symbol: '×',
    func: (prevValue, nextValue) => prevValue * nextValue
  },
  {
    key: '-',
    name: 'subtract',
    symbol: '−',
    func: (prevValue, nextValue) => prevValue - nextValue
  },
  {
    key: '+',
    name: 'add',
    symbol: '+',
    func: (prevValue, nextValue) => prevValue + nextValue
  },
  {
    key: '=',
    name: 'equals',
    symbol: '=',
    func: (prevValue, nextValue) => nextValue
  }
]

const getFormattedValue = value => {
  const language = navigator.language || 'en-US'

  let formattedValue = parseFloat(value).toLocaleString(language, {
    useGrouping: true,
    maximumFractionDigits: 6
  })

  const match = value.match(/\.\d*?(0*)$/)

  if (match) { formattedValue += (/[1-9]/).test(match[0]) ? match[1] : match[0] }

  return formattedValue.length >= 14 ? String(parseFloat(value).toExponential()) : formattedValue
}


function CalculatorKey({ onClick, className, keyValue}) {
  return (
    <button onclick={onClick} class={`calculator-key ${className}`}>{keyValue}</button>
  )
}

function CalculatorDisplay({ value }) {
  return (
    <div class="calculator-display">
      {getFormattedValue(value)}
    </div>
  )
}

class Calculator extends Component {
  state = {
    value: null,
    displayValue: '0',
    operator: null,
    scale: 1,
    waitingForOperand: false
  }
  container = 'section'

  clearAll () {
    this.setState({
      value: null,
      displayValue: '0',
      operator: null,
      waitingForOperand: false
    })
  }

  clearDisplay () {
    this.setState({
      displayValue: '0'
    })
  }

  clearLastChar () {
    const { displayValue } = this.state

    this.setState({
      displayValue: displayValue.substring(0, displayValue.length - 1) || '0'
    })
  }

  toggleSign () {
    const { displayValue } = this.state
    const newValue = parseFloat(displayValue) * -1

    this.setState({
      displayValue: String(newValue)
    })
  }

  inputPercent () {
    const { displayValue } = this.state
    const currentValue = parseFloat(displayValue)

    if (currentValue === 0) { return }

    const fixedDigits = displayValue.replace(/^-?\d*\.?/, '')
    const newValue = parseFloat(displayValue) / 100

    this.setState({
      displayValue: String(newValue.toFixed(fixedDigits.length + 2))
    })
  }

  performOperation (nextOperator) {
    const { value, displayValue, operator } = this.state
    const inputValue = parseFloat(displayValue)

    if (value == null) {
      this.setState({
        value: inputValue
      })
    } else if (operator) {
      const currentValue = value || 0
      const operation = CalculatorOperations.find(item => operator == item.key)
      const newValue = operation.func(currentValue, inputValue)

      this.setState({
        value: newValue,
        displayValue: String(newValue)
      })
    }

    this.setState({
      waitingForOperand: true,
      operator: nextOperator
    })
  }

  inputDigit = digit => {
    const { displayValue, waitingForOperand } = this.state

    if (waitingForOperand) {
      this.setState({
        displayValue: String(digit),
        waitingForOperand: false
      })
    } else {
      this.setState({
        displayValue: displayValue === '0' ? String(digit) : displayValue + digit
      })
    }
  }

  inputDot = () => {
    const { displayValue, waitingForOperand } = this.state
    if (waitingForOperand) {
      this.setState({
        displayValue: '0.',
        waitingForOperand: false
      })
    } else if (!(/\./).test(displayValue)) {
      this.setState({
        displayValue: displayValue + '.',
        waitingForOperand: false
      })
    }
  }

  handleKeyDown = event => {
    let { key } = event

    if (key === 'Enter') { key = '=' }

    if ((/\d/).test(key)) {
      event.preventDefault()
      this.inputDigit(parseInt(key, 10))
    } else if (key in CalculatorOperations) {
      event.preventDefault()
      this.performOperation(key)
    } else if (key === '.') {
      event.preventDefault()
      this.inputDot()
    } else if (key === '%') {
      event.preventDefault()
      this.inputPercent()
    } else if (key === 'Backspace') {
      event.preventDefault()
      this.clearLastChar()
    } else if (key === 'Clear') {
      event.preventDefault()

      if (this.state.displayValue !== '0') {
        this.clearDisplay()
      } else {
        this.clearAll()
      }
    }
  }

  componentDidMount () {
    document.addEventListener('keydown', this.handleKeyDown)
  }

  componentWillUnmount () {
    document.removeEventListener('keydown', this.handleKeyDown)
  }

  render () {
    const { displayValue } = this.state
    const clearDisplay = displayValue !== '0'
    const clearText = clearDisplay ? 'C' : 'AC'

    return (
      <div class="calculator">
        <CalculatorDisplay value={displayValue} />
        <div class="calculator-keypad">
          <div class="input-keys">
            <div class="function-keys">
              <CalculatorKey
                className="key-clear"
                onClick={() => clearDisplay ? this.clearDisplay() : this.clearAll()}
                keyValue={ clearText }
              >
                
              </CalculatorKey>
              <CalculatorKey
                className="key-sign"
                onClick={() => this.toggleSign()}
                keyValue='±'>
                
              </CalculatorKey>
              <CalculatorKey
                className="key-percent"
                onClick={() => this.inputPercent()}
                keyValue='%'
              >
                
              </CalculatorKey>
            </div>
            <div className="digit-keys">
              {
                DigitKeys.map(el =>
                  <CalculatorKey
                    key={`key-${el}`}
                    className={`key-${el}`}
                    onClick={() => this.inputDigit(el)}
                    keyValue={el}
                  >
                  </CalculatorKey>
                )
              }
              <CalculatorKey
                className="key-dot"
                onClick={this.inputDot}
                keyValue='●'
              >
                
              </CalculatorKey>
            </div>
          </div>
          <div className="operator-keys">
            {
              CalculatorOperations.map(item => (
                <CalculatorKey 
                key={item.name}
                className={`key-${item.name}`}
                onClick={() => this.performOperation(item.key)}
                keyValue={item.symbol}
                >
                </CalculatorKey>
              ))
            }
          </div>
        </div>
      </div>
    )
  }
}


const calculator = new Calculator({
  container: 'section'
})

            
          
!
999px

Console