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.

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

              
                <body>  
  <div class="container-fluid text-center">
       
    <div class="row">
      <h1> Calculator </h1>
      <h3> Math is Fun </h3>
    </div> 
    
    <div class="row"> 
      <!-- Start calculator -->
      <div id="calculator">
      
        <div class="top">
          <span class="clear">C</span>
          <div class="screen"></div>
        </div>
        
        <div class="keys">
        <!-- operators and other keys -->
          <span>7</span>
          <span>8</span>
          <span>9</span>
          <span class="operator">+</span>
          <span>4</span>
          <span>5</span>
          <span>6</span>
          <span class="operator">-</span>
          <span>1</span>
          <span>2</span>
          <span>3</span>
          <span class="operator">÷</span>
          <span>0</span>
          <span>.</span>
          <span class="eval">=</span>
          <span class="operator">x</span>
        </div>
      
      </div>
      <!-- End calculator -->
    </div> 
    
    <div class="row text-center"> <!-- for footer -->
      <h4>Video tutorial <a href="https://youtu.be/8divWTgRJm8" target="_blank">here</a></h4>
      <h5><a href="http://www.eleftheriabatsou.com" target="_blank">Eleftheria </a> | <a href="https://codepen.io/EleftheriaBatsou" target="_blank">Projects</a> | <a href="https://www.youtube.com/channel/UCC-WwYv3DEW7Nkm_IP6VeQQ" target="_blank"> Coding videos </a></h5>
    </div> <!-- end footer -->
    
  </div> <!-- end class container-fluid --> 
</body>
              
            
!

CSS

              
                body {
  background-color: #B2127D; /* purple */
  color: #14C6CC;            /* blue */
}

/* yellow: #FFDF27
dark blue: #1BADB2 
almost white: #FFF0F5*/

#calculator{
  width: 325px;
  height: auto;
  margin: 5% auto;
  padding: 20px 20px 10px;
  
  background: #14C6CC;
  background: linear-gradient(#14C6CC, #FFDF27);
  border-radius: 50px;
}

.top span.clear {
  float: left;
}

.top .screen {
  height: 40px;
  width: 212px;
  float: right;
  padding: 0 10px;
  
  background: #1BADB2;
  border-radius: 50px;
  
  font-size: 20px;
  line-height: 40px;
  color: white;
  text-align: right;
  letter-spacing: 1px;
}

.keys, .top {overflow:hidden}

.keys span, .top span.clear{
  float:left;
  position: relative;
  top: 0px;
  cursor:pointer;
  
  width: 66px;
  height: 36px;
  
  background: white;
  border-radius: 20px;
  margin: 0 7px 11px 0;
  
  color:  #B2127D;
  line-height: 36px;
  text-align: center;
  user-select: none;
  transition: all 0.2s ease;
}

.keys span.operator {
  background: #FFF0F5;
  margin-right: 0;
}

.keys span.eval {
  background: #14C6CC;
  color: #B2127D;
}

.top span.clear {
  background: #FFDF27;
  color: #B2127D;
}

.keys span:hover {
  background: #14C6CC;
  color: white;
}

.keys span.eval:hover {
  background: #B2127D;
  color: white;
}

.keys span.clear:hover {
  background: #B2127D;
  color: white;
}
              
            
!

JS

              
                // Get all the keys from document
var keys = document.querySelectorAll('#calculator span');
var operators = ['+', '-', 'x', '÷'];
var demimalAdded = 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;
    
    if (btnVal == 'C') {
      input.innerHTML = '';
      demimalAdded = 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(/.$/, '');
      
      if (equation)
        input.innerHTML = eval(equation);
      
      demimalAdded = 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);
      }
      
      demimalAdded = false;
    } else if(btnVal == '.') {
      if(!demimalAdded) {
        input.innerHTML +=btnVal;
        demimalAdded = true;
      }
    }
    
    else {
      input.innerHTML += btnVal;
    }
    
    e.preventDefault();
  }
}
              
            
!
999px

Console