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

              
                <div class="wrap">
  <div class="container">
    <div class="screen"><div id="display">0
      </div>
      <div id="sum"></div>
    </div>
    
    <div class="cell"></div>
    <div class="cell"><div class="button pink" data-num="clear">C</div></div>
    <div class="cell symbol"><div class="button" data-num="/">/</div></div>
    <div class="cell"><div class="button" data-num="*">x</div></div>
    
    <div class="cell"><div class="button" data-num="7" >7</div></div>
    <div class="cell"><div class="button" data-num="8">8</div></div>
    <div class="cell"><div class="button" data-num="9">9</div></div>
    <div class="cell symbol"><div class="button" data-num="-">-</div></div>
    
    <div class="cell"><div class="button" data-num="4">4</div></div>
    <div class="cell"><div class="button" data-num="5">5</div></div>
    <div class="cell"><div class="button" data-num="6">6</div></div>
    <div class="double-row symbol"><div class="button drow" data-num="+">+</div></div>
    
    <div class="cell"><div class="button" data-num="1">1</div></div>
    <div class="cell"><div class="button" data-num="2">2</div></div>
    <div class="cell"><div class="button" data-num="3">3</div></div>
    
    <div class="cell"><div class="button" data-num="0">0</div></div>
    <div class="cell symbol"><div class="button" data-num=".">.</div></div>
    <div class="double-col symbol"><div class="button pink" data-num="=" data-calc="true">=</div></div>
  </div>
</div>
<footer>Made with CSS Grid by <a href="https://twitter.com/nicki_hastings/">@Nicki_Hastings</a></footer>

              
            
!

CSS

              
                body{
   background-color: #e2e2e2;
  font-family:Orbitron;
  font-size: 1.25em;
  line-height:2.75em
}
.wrap{
  display: block;
  width: 400px;
  margin: 50px auto;
  background-color: #fff;
}
.container {
  display: grid;
  grid-template-columns: auto auto auto auto;
  border: 1px solid #555;
  text-align: center;
  background-color: #c8d7dc;
  -webkit-box-shadow: 1px 3px 8px 3px rgba(0,0,0,0.4);  
  -moz-box-shadow: 1px 3px 8px 3px rgba(0,0,0,0.4);  
  box-shadow: 1px 3px 8px 3px rgba(0,0,0,0.3);  
}

.screen{
  grid-column-start: 1;
  grid-column-end: 5;
  text-align: center;
  padding: 8px;
  font-family: Cute Font;
  font-size: 3.5em;
  background: #c3d0d8;
  background: -moz-linear-gradient(top, #c3d0d8 0%, #70818e 100%);
  background: -webkit-linear-gradient(top, #c3d0d8 0%,#70818e 100%);
  background: linear-gradient(to bottom, #c3d0d8 0%,#70818e 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#c3d0d8', endColorstr='#70818e',GradientType=0 );
  position: relative;
}

#display{
  padding: 5px 10px 5px 5px;
  background: #f2f5f6;
  background: -moz-linear-gradient(top, #f2f5f6 0%, #e3eaed 37%, #c8d7dc 100%);
  background: -webkit-linear-gradient(top, #f2f5f6 0%,#e3eaed 37%,#c8d7dc 100%);
  background: linear-gradient(to bottom, #f2f5f6 0%,#e3eaed 37%,#c8d7dc 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f2f5f6', endColorstr='#c8d7dc',GradientType=0 );
  text-align: right;
  height: 60px;
}

#sum{
  position: absolute;
  top: 18%;
  left:0;
  text-align: left;
  font-size: 0.5em;
  margin-left: 0.5em;
}

.cell{
  /*border: 1px solid #888;*/
  padding: 10px;
}
.button{
  display: block;
  border: 1px solid #70818e;
  background: #c3d0d8;
background: -moz-linear-gradient(top, #c3d0d8 0%, #70818e 100%);
background: -webkit-linear-gradient(top, #c3d0d8 0%,#70818e 100%);
background: linear-gradient(to bottom, #c3d0d8 0%,#70818e 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#c3d0d8', endColorstr='#70818e',GradientType=0 );
  -webkit-box-shadow: 1px 3px 6px 2px rgba(0,0,0,0.4);  
  -moz-box-shadow: 1px 3px 6px 2px rgba(0,0,0,0.4);  
  box-shadow: 1px 3px 6px 2px rgba(0,0,0,0.3); 
}
.button:hover{
  cursor: pointer;
}
.button:active{
  background: -moz-linear-gradient(top, #70818e 0%, #c3d0d8 100%);
  background: -webkit-linear-gradient(top, #70818e 0%,#c3d0d8 100%);
  background: linear-gradient(to bottom, #70818e 0%,#c3d0d8 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#70818e', endColorstr='#c3d0d8',GradientType=0 );
  -webkit-box-shadow: 1px 3px 6px 2px rgba(0,0,0,0.4);  
  -moz-box-shadow: 1px 3px 6px 2px rgba(0,0,0,0.4);  
  box-shadow: 1px 1px 1px 1px rgba(0,0,0,0.3);
}

.button.pink{
  border: 1px solid #fe5377;
  background: #fcaeae;
background: -moz-linear-gradient(top, #fcaeae 0%, #fe5377 100%);
background: -webkit-linear-gradient(top, #fcaeae 0%,#fe5377 100%);
background: linear-gradient(to bottom, #fcaeae 0%,#fe5377 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcaeae', endColorstr='#fe5377',GradientType=0 );
}
.button.pink:active{
  cursor:pointer;
  background: -moz-linear-gradient(top, #fe5377 0%, #fcaeae 100%);
background: -webkit-linear-gradient(top, #fe5377 0%,#fcaeae 100%);
background: linear-gradient(to bottom, #fe5377 0%,#fcaeae 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fe5377', endColorstr='#fcaeae',GradientType=0 );
}
.button.drow{
  height: 100%;
  vertical-align: center;
  margin: auto 0;
  line-height: 5em;
}
.double-row{
  /*row start, col start, row end, col end*/
  grid-area: 4 / 4 / 6 / 5;
  align-self: stretch;
  padding: 10px 10px;
}
.double-col{
  grid-area: 6/3/7/5;
  align-self: center;
  padding: 10px;
}

.symbol{
  font-size: 1.3em;
}
footer{
  text-align: center;
  font-size: 60%;
}
              
            
!

JS

              
                $(document).ready(function(){
  var calculation = 0;  //variable to hold the calculation
  var lastDigit = 'num'; //whether button pressed is number or symbol
  var digitNum = 0; //counts digits for screen display
  var decPoint = false; //decimal point can only be used once.
  
  function resetCalc(btnType){
    //if the clear button is pressed reset the calculation
    document.getElementById("display").innerHTML='0';
    document.getElementById("sum").innerHTML='';
    calculation = 0;
    lastDigit = 'num';
    digitNum = 0;
    decPoint = false;
  }
  
  //carry out the calculation and show the answer
  function doCalculation(){
    var result = eval(calculation);
    lastDigit = '=';
    result = result.toString();
    calculation = result;
    if(calculation.indexOf(".") !== -1){ //if the decimal point is there
      //stops the decimal point being used twice
      decPoint = true;
    }
    else{
      decPoint = false;
    }
    
    if(result.length > 11){
      document.getElementById("display").innerHTML=result.slice(0, 11);
      document.getElementById("sum").innerHTML='E';
    }
    else{
      document.getElementById("display").innerHTML=result;
      document.getElementById("sum").innerHTML='=';
    }
  }
  
  function decimalPoint(btnType){
    switch(decPoint){
      case true:
        break;
        
      case false:
        if(digitNum >= 11){
          //add the error symbol and freeze screen
          document.getElementById("sum").innerHTML='E';
        }
        //if the last digit was a symbol, clear the screen for the next number
        else if(lastDigit === 'symbol' || lastDigit === "="){
          document.getElementById("display").innerHTML="0.";
          if(lastDigit==="="){
            document.getElementById("sum").innerHTML='';
          }
          decPoint = true;
          calculation += "0.";
          lastDigit = 'num';
          digitNum = 2;
        }
        //otherwise append the point to the screen
        else{
          document.getElementById("display").innerHTML+=btnType;
          calculation += btnType;
          lastDigit = 'num';
          digitNum ++;
          decPoint = true;
        }
        break;
    }
  }
  
  function updateCalc(btnType){
    //if its a symbol update the symbol field and calculation
      document.getElementById("sum").innerHTML=btnType;
      //if the last digit was a number add the symbol.
      if(lastDigit === 'num' || lastDigit === "="){
        calculation += btnType;
        lastDigit = 'symbol';
        decPoint = false;
      }
      else{
        //otherwise, if the last digit was a symbol
        //remove the last symbol from the calculation
        var saveCalc = calculation.slice(0, -1);
        //then add the new one
        calculation = saveCalc + btnType;
        lastDigit = 'symbol';
        decPoint = false;
      }
  }
  
  function updateScreen(btnType){
    //display the number on the screen
    //if there are too many digits to display on the screen:
    if(digitNum >= 11){
      //add the error symbol and freeze screen
      document.getElementById("sum").innerHTML='E';
    }
    //if the number is currently zero, replace it unless it's a point.
    else if(calculation === 0 || calculation === '0' || lastDigit === "="){
      document.getElementById("display").innerHTML=btnType;
      document.getElementById("sum").innerHTML='';
      calculation = btnType;
      lastDigit = 'num';
      digitNum = 1;
    }
    //if the last digit was a symbol, clear the screen for the next number
    else if(lastDigit === 'symbol'){
      decPoint = false;
      document.getElementById("display").innerHTML=btnType;
      calculation += btnType;
      lastDigit = 'num';
      digitNum = 1;
    }
    //otherwise append the number to the screen
    else{
      document.getElementById("display").innerHTML+=btnType;
      calculation += btnType;
      lastDigit = 'num';
      digitNum ++;
    }
    
  }
  
  function captureBtn(num){
    //get the data-num attribute for the button
    var btnType = this.dataset.num;

    switch(btnType){
      
      case 'clear':
        resetCalc(btnType);
        break;
      
      case '=':
        doCalculation();
        break;
      
      case '*':
      case '-':
      case '+':
      case '/':
        updateCalc(btnType);
        break;
        
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
      case '0':
        updateScreen(btnType);
        break;
      
      case '.':
        decimalPoint(btnType);
        break;
    }       
    
  }
  
  var theButtons = document.getElementsByClassName("button");
  for(var i = 0; i < theButtons.length; i++){
    theButtons[i].addEventListener("click", captureBtn);
  }

});
              
            
!
999px

Console