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

              
                <div class="clearfix calcBody"> 
  <div class="clearfix row display">
    <div><span class="log"></span>
      <span class="current"></span>
    </div>
  </div>
  <div class="row">
    <h1>Javascript Calculator</h1>
  </div>
  <div class="clearfix row clearButtons">
    <div class="clearAll bttn"><p>C</p></div>
    <div class="clearEntry bttn"><p>CE</p></div>
    <div class="eq bttn"><p>=</p></div>
  </div>
  <div class="clearfix row operators">
    <div class="plus bttn op"><p>&#43</p></div>
    <div class="minus bttn op"><p>&#8722</p></div>
    <div class="multiply bttn op"><p>&#215</p></div>
    <div class="divide bttn op"><p>&#247</p></div>
  </div>
  <div class="clearfix row">
    <div class="1 bttn digit"><p>1</p></div>
    <div class="2 bttn digit"><p>2</p></div>
    <div class="3 bttn digit"><p>3</p></div>
  </div>
  <div class="clearfix row">
    <div class="4 bttn digit"><p>4</p></div>
    <div class="5 bttn digit"><p>5</p></div>
    <div class="6 bttn digit"><p>6</p></div>
  </div>
  <div class="clearfix row">
    <div class="7 bttn digit"><p>7</p></div>
    <div class="8 bttn digit"><p>8</p></div>
    <div class="9 bttn digit"><p>9</p></div>
  </div>
  <div class="clearfix row">
    <div class="0 bttn digit"><p>0</p></div>
    <div class="period bttn digit"><p>.</p></div>
  </div>
</div>

              
            
!

CSS

              
                .clearfix:after {
     visibility: hidden;
     display: block;
     font-size: 0;
     content: " ";
     clear: both;
     height: 0;
     }
.clearfix { display: inline-block; }
/* start commented backslash hack \*/
* html .clearfix { height: 1%; }
.clearfix { display: block; }

div{
  border: 1px solid black;
}

.row{
  border: none;
}

p,h1{
  font-family: Georgia;
  font-weight: 600;
}

h1{
  color: #555;
  font-size: 1.2em;
  margin: 1.4%;
}

p{
  text-align: center;
}

div div div{
  float: left;
  width: 30%;
  margin: 1.4%;
  border-radius: 3px;
  box-shadow: 0px 1px 1px 0px rgb(0, 0, 0),
		 0px 2px 1px 1px rgb(141, 132, 138),
		 0px 3px 1px 1px rgb(131, 122, 128),inset 2px 2px 1px 1px #666;
  color: white;
  background: #999;
  color-shift: -10;
  depth: 10;
  direction: up;
}

div div div:hover{
  transition: .5s;
  background: rgb(150,246,131); 
  box-shadow: 0px 1px 1px 0px rgb(0, 0, 0),
		 0px 2px 1px 1px rgb(141, 132, 138),
		 0px 3px 1px 1px rgb(131, 122, 128),inset 2px 2px 1px 1px rgb(5,225,119), inset 3px 3px 1px 1px rgb(19,227,87);
}

.calcBody{
  width: 40%;
  margin: 0 auto;
  border-radius:  10px 10px 200px 200px / 10px 10px 50px 50px;
  box-shadow: -2px 2px 2px 2px #777,
    -4px 4px 1px 1px #666,
    -6px 6px 2px 1px #999,
    inset 2px 4px 1px 1px rgb(200,200,200), inset 0px -5px 3px 3px rgb(200,200,200), inset 1px -9px 1px 4px rgb(220,220,220);
  background: rgb(245,250,245);
  padding: 1em 1.5em 3em 1.5em;
}

.operators div{
  width: 21.73%;
  margin:1.4%;
}

.clearButtons div{
  width: 30%;
  margin: 1.4%;
  background: rgb(190,100,100);
  box-shadow: 0px 1px 1px 0px rgb(0, 0, 0),
		 0px 2px 1px 1px rgb(141, 132, 138),
		 0px 3px 1px 1px rgb(131, 122, 128),inset 2px 2px 1px 2px rgb(210,120,100);
}

.clearButtons div:hover{
  background: rgb(170,90,90); 
  box-shadow: 0px 1px 1px 0px rgb(0, 0, 0),
		 0px 2px 1px 1px rgb(141, 132, 138),
		 0px 3px 1px 1px rgb(131, 122, 128),inset 2px 2px 1px 1px rgb(160,85,85), inset 3px 3px 1px 1px rgb(155,83,82);
}

.clearButtons .eq{
  background: rgb(10,180,220);
  box-shadow: 0px 1px 1px 0px rgb(0, 0, 0),
		 0px 2px 1px 1px rgb(131, 122, 128),
		 0px 3px 1px 1px rgb(121, 112, 118), inset 2px 2px 1px 2px rgb(40,200,240);
}

.clearButtons .eq:hover{
  background: rgb(20,170,200);
  box-shadow: 0px 1px 1px 0px rgb(0, 0, 0),
		 0px 2px 1px 1px rgb(131, 122, 128),
		 0px 3px 1px 1px rgb(121, 112, 118), inset 2px 2px 1px 2px rgb(30,190,220),inset 3px 3px 1px 2px rgb(20,175,220);
}

.display{
  border-radius: 10px 10px 0 0;
}

.display span{
  text-align: right;
  color: #555;
  font-family: sans-serif;
  display: block;
  margin: 5px;
  height: 1em;
}
.current{
  font-size: 3em;
}

.display div{
  float: none;
  height: 5em;
  width: 92%;
  margin: 1% auto;
  background: rgb(207,206,186);
  box-shadow: inset 3px 3px #777;
}

.display div:hover{
  background: rgb(207,206,186);
  box-shadow: inset 3px 3px #777;
}

.bttnPress.bttn{
  background: rgb(5,225,119);
}


              
            
!

JS

              
                $(document).ready(function(){
  //when a button is clicked, print it in display and tracker
  var val="",num="",lastClicked,currentWidth=$(".display").css("width"); //val tracks the overall string, num tracks the the current input, lastclicked tracks last button clicked, currentWidth keeps track of width of "display" div on different screen sizes
  
  //clear function resets everything
  function clear (){
    val="",num="",lastClicked="";
    $(".current").empty();
    $(".log").empty();
  }
  
  //digit button click handler
  $(".digit").click(function(){
     //button press animation
      $(this).addClass('bttnPress')
        .delay(200).queue(function() {
  $('.digit').removeClass('bttnPress').dequeue()
      });
      //if user just clicked "=", call clear to avoid treating answer as an input
      if (lastClicked=="eq"){
        clear();
      }
      ///don't print more numbers than can fit in the display 
      //only display if number of digits < display width/font-size -1
      if (num.length<parseInt(currentWidth)/Math.floor(parseInt($(".current").css("font-size")))-1){
        num+=$(this).children().text();
        $(".current").text(num);
      }
      lastClicked="dig";
  });
  //+,-,*,/ button handler
  $(".op").click(function(){
      //button press animation 
      $(this).addClass('bttnPress')
        .delay(200).queue(function() {
  $('.bttn').removeClass('bttnPress').dequeue()
      });
      if (!num){
        return "";
      }
      val+=" "+num; //adds previous number input to string to be evaluated
      num="";//resets number input
      var operator = $(this).attr("class");
      switch (operator){//add "+,-.*,/" to val depending on class of the button pressed
        case "plus bttn op bttnPress":
          val+=" +"
          break;
        case "minus bttn op bttnPress":
          val+=" -"
          break;
        case "multiply bttn op bttnPress":
          val+=" *"
          break;
         case "divide bttn op bttnPress":
          val+=" /"
          break;
      }
      lastClicked="op";
      $(".log").text(val);//display the whole val expression so far in display box
  });
  
  //equals button handler
  $(".eq").click(function(){
    lastClicked="eq";
    val+=" "+num;//add final number input to val string
    var exp=val.split(" ");
    exp=Math.round(eval(exp.join(""))*100)/100;//evaluate and round val string
    $(".current").text(exp);//show answer in display
    $(".log").empty();//clear out the log 
    num=exp;//set the answer as the current input in case user wants to do another calculation based on it
    val="";//reset the string to be evaluated
  });
  //clear entry button 
  $(".clearEntry").click(function(){
    //deletes most recent number and operation inputted
    if (!$(".log").text()){
      clear();
    }
    val+=" "+num;
    var exp = val.split(" ");
    exp.reverse();
    exp=exp.slice(0,exp.length-1);
    var index = ""; 
    for (var i=0;i<exp.length;i++){
      if (exp[i]=="+"||exp[i]=="*"||exp[i]=="-"||exp[i]=="/"){
        index = exp.indexOf(exp[i]);
        break;
      }else if (i==exp.length-1){
        index = exp.length-1;
      }
    }
    exp=exp.slice(index+1)
    var current=exp[0];
    exp.push("");
    exp.reverse();
    val=exp.join(" ");
    $(".log").text(val);
    $(".current").text(current);
  });
  //clear button handler
  $(".clearAll").click(function(){
   clear();
  });
  //if button is clicked after an answer is found
});
              
            
!
999px

Console