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 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

              
                <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Javascript Calculator</title>
    <link rel="stylesheet" href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
    <link rel="stylesheet" media="screen" href="https://fontlibrary.org/face/dseg14" type="text/css" />
</head>

<body>
    <div class="container">
        <div id="display">
            <div id="equation">
                <span id="cursor" class="blinker">I</span>
            </div>
        </div>
        <div class="row">
            <button id="bclr" class="bt black">C</button>
            <button id="beq" class="bt black">=</button>
            <div id="result" class=""></div>
        </div>
        <div class="row">
            <button id="b7" class="bt black">7</button>
            <button id="b8" class="bt black">8</button>
            <button id="b9" class="bt black">9</button>
            <button id="bl" class="bt grey">
                <i class="ion-arrow-left-c"></i>
            </button>
            <button id="br" class="bt grey">
                <i class="ion-arrow-right-c"></i>
            </button>
        </div>
        <div class="row">
            <button id="b4" class="bt black">4</button>
            <button id="b5" class="bt black">5</button>
            <button id="b6" class="bt black">6</button>
            <button id="bmult" class="bt blue">x</button>
            <button id="bdiv" class="bt blue">/</button>
        </div>
        <div class="row">
            <button id="b1" class="bt black">1</button>
            <button id="b2" class="bt black">2</button>
            <button id="b3" class="bt black">3</button>
            <button id="bsub" class="bt blue">-</button>
            <button id="badd" class="bt blue">+</button>
        </div>
        <div class="row">
            <button id="b0" class="bt black">0</button>
            <button id="bd" class="bt black">.</button>
            <button id="bdel" class="bt black">
                <i class="ion-backspace"></i>
            </button>
            <button id="bbrl" class="bt blue">(</button>
            <button id="bbrr" class="bt blue">)</button>
        </div>
    </div>

</body>
              
            
!

CSS

              
                body {
    background: #D1CED3;
}

.container {
    margin: 5% auto;
    display: block;
    width: 500px;
    height: 500px;
    border: black 3px solid;
    background: #000;
}

#display {
    display: block;
    height: 25%;
    color: #D1CED3;
    overflow-wrap: break-word;
    overflow-y: auto;
}

#equation {
    padding: 2%;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 1em;
}

#result {
    display: block;
    color: #D1CED3;
    padding: 2%;
    height: 73%;
    left: 45%;
    text-align: right;
    font-family: DSEG14ClassicItalic;
    font-size: 1.2em;
    overflow-x: auto;
}

.row {
    height: 15%;
    display: block;
}

.bt {
    border: black 1px solid;
    width: 20%;
    margin: 0px;
    float: left;
    padding: 0;
    height: 100%;
    font-weight: bold;
    font-size: 1.3em;
}

.black {
    background: #333333;
    color: #D1CED3;
}

.grey {
    background: #D1CED3;
}

.blue {
    background: #0095FF;
}

.bt:hover {
    background: #000;
    color: #0095FF;
}

.bt:active {
    background: #000;
    color: #D1CED3;
}

.bt:focus {
    outline: 0;
}

.hover {
    background: #000;
    color: #0095FF;
    transition-delay: 0,5s;
}

.error {
    color: #333333 !important;
}

#cursor {
    color: #0095FF;
    font-weight: bold;
}

.blinker {
    animation: blinker 1s linear infinite;
}

@keyframes blinker {
    50% {
        opacity: 0;
    }
}

::-webkit-scrollbar {
    width: 7px;
    height: 7px;
}

::-webkit-scrollbar-track {
    background: #000;
    border: black 1px solid;
}

::-webkit-scrollbar-thumb {
    background: #333333;
    border: black 1px solid;
}

::-webkit-scrollbar-thumb:hover {
    background: #D1CED3;
}
              
            
!

JS

              
                var totalString = ""; //string to be evaluated
var preCursor = ""; //text before cursor
var postCursor = ""; //text after cursor
var lastValid = 0; //last valid value

document.addEventListener('DOMContentLoaded', function () {

    function q(query) { //quick querySelector
        return document.querySelector(query);
    }

    update();

    //button event handles
    handle("#b0", "0", 48, 96);
    handle("#b1", "1", 49, 97);
    handle("#b2", "2", 50, 98);
    handle("#b3", "3", 51, 99);
    handle("#b4", "4", 52, 100);
    handle("#b5", "5", 53, 101);
    handle("#b6", "6", 54, 102);
    handle("#b7", "7", 55, 103);
    handle("#b8", "8", 56, 104);
    handle("#b9", "9", 57, 105);
    handle("#bd", ".", 190, 110);
    handle("#bbrl", "(", undefined, undefined, 57);
    handle("#bbrr", ")", undefined, undefined, 48);
    handle("#badd", "+", 107, undefined, 187);
    handle("#bsub", "-", 109, 189);
    handle("#bdiv", "/", 111, 191);
    handle("#bmult", "*", 106, undefined, 56);
    handle("#bdel", "", 8, undefined, undefined, backspace);
    handle("#bclr", "", 67, 46, undefined, clear);
    handle("#beq", "", 13, 187, undefined, equals);
    handle("#bl", "", 37, undefined, undefined, left);
    handle("#br", "", 39, undefined, undefined, right);



    function handle(query, value, key0, key1, skey, func) { //button event handler
        //(query selector, input value, key 1, key 2, shift + key, special function)
        if (!func) { //events that add to the string value
            q(query).addEventListener("click", () => { //button press
                preCursor += value;
                update();
            });
            window.addEventListener("keydown", (e) => { //key press
                var key = e.keyCode;
                if (event.shiftKey) {
                    if (key === skey) {
                        q(query).classList.add('hover');
                        preCursor += value;
                        update();
                    }
                } else {
                    if (key === key0 || key === key1) {
                        q(query).classList.add('hover');
                        preCursor += value;
                        update();
                    }
                }
            });
        } else { //events with special functionality
            q(query).addEventListener("click", () => { //button press
                func();
            });
            window.addEventListener("keydown", (e) => { //key press
                var key = e.keyCode;
                if (key === key0 || key === key1) {
                    q(query).classList.add('hover');
                    func();
                }
            });
        }
        window.addEventListener("keyup", (e) => {
            q(query).classList.remove('hover');
        })
    }

    function update() { //calculates and updates values on display
        totalString = preCursor + postCursor;
        q("#equation").innerHTML = preCursor + '<span id="cursor" class="blinker">|</span>' + postCursor;
        try {
            if (totalString !== "") {
                lastValid = eval(totalString);
                q('#result').classList.remove("error");
            } else {
                lastValid = 0;
                q('#result').classList.add("error");
            }
            q('#result').innerHTML = lastValid;
        } catch (e) {
            q('#result').classList.add("error");
        }
    }

    function backspace() { //delete before cursor
        preCursor = preCursor.slice(0, -1);
        update();
    }

    function left() { //move cursor left
        postCursor = preCursor.slice(-1) + postCursor;
        preCursor = preCursor.slice(0, -1);
        update();
    }

    function right() { //move cursor right
        preCursor = preCursor + postCursor.slice(0, 1);
        postCursor = postCursor.slice(1);
        update();
    }

    function clear() { //clear everything
        totalString = "";
        preCursor = "";
        postCursor = "";
        lastValid = 0;
        update();
    }

    function equals() { //get result as operand
        try {
            if (totalString !== ""){
                totalString = eval(totalString);
                preCursor = totalString.toString();
                postCursor = "";
            }
        } catch (e) {
            
        }
        update();
    }
});
              
            
!
999px

Console