Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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="min-h-screen p-16 bg-gray-100">


<div
	x-data="calculator()"
	@keydown.window.tab="usedKeyboard = true">
	
	<h1 class="font-bold text-xl mb-2" id="title">A Calculator</h1>
    <div
        role="application"
        aria-labelledby="title"
        style="max-width:396px"
        class="bg-gray-200">
        <div class="relative">
            <div
				aria-live="polite"
				x-text="formula"
				class="absolute top-0 right-0 text-green-400 font-bold pt-2 pr-4"></div>
            <div
                aria-live="polite"
				aria-atomic="true"
                style="background:#282828"
                class="h-32 w-full text-white text-6xl flex items-center pt-8 pr-5 justify-end mb-1">
                <span class="sr-only">The total is:</span>
				<span x-text="total"></span>
            </div>
        </div>
        <div @click="updateTotal($event.target.value)" class="flex space-x-1">
            <div id="controls" class="gap-1 grid grid-cols-1">
                <button
                    value=''
                    class="text-3xl h-24 w-24 flex items-center justify-center bg-green-400 hover:bg-green-500"
					:class="{'focus:outline-none': !usedKeyboard}"
                    aria-label="Clear all">
                    C
                </button>
                <button
                    value='BS'
                    class="text-3xl h-24 w-24 flex items-center justify-center bg-green-400 hover:bg-green-500"
					:class="{'focus:outline-none': !usedKeyboard}"
                    aria-label="Backspace">
                    BS
                </button>
                <button
                    value='+'
                    class="text-3xl h-24 w-24 flex items-center justify-center bg-green-400 hover:bg-green-500"
					:class="{'focus:outline-none': !usedKeyboard}"
                    aria-label="Add">
                    +
                </button>
                <button
                    value='-'
                    class="text-3xl h-24 w-24 flex items-center justify-center bg-green-400 hover:bg-green-500"
					:class="{'focus:outline-none': !usedKeyboard}"
                    aria-label="Subtract">
                    -
                </button>
            </div>
            <div id="buttons" class="gap-1 grid grid-cols-3">
                <button
					class="text-3xl h-24 w-24 bg-gray-400 hover:bg-gray-300"
					:class="{'focus:outline-none': !usedKeyboard}"
					value="9">9</button>
                <button
					class="text-3xl h-24 w-24 bg-gray-400 hover:bg-gray-300"
					:class="{'focus:outline-none': !usedKeyboard}"
					value="8">8</button>
                <button
					class="text-3xl h-24 w-24 bg-gray-400 hover:bg-gray-300"
					:class="{'focus:outline-none': !usedKeyboard}"
					value="7">7</button>
                <button
					class="text-3xl h-24 w-24 bg-gray-400 hover:bg-gray-300"
					:class="{'focus:outline-none': !usedKeyboard}"
					value="6">6</button>
                <button
					class="text-3xl h-24 w-24 bg-gray-400 hover:bg-gray-300"
					:class="{'focus:outline-none': !usedKeyboard}"
					value="5">5</button>
                <button
					class="text-3xl h-24 w-24 bg-gray-400 hover:bg-gray-300"
					:class="{'focus:outline-none': !usedKeyboard}"
					value="4">4</button>
                <button
					class="text-3xl h-24 w-24 bg-gray-400 hover:bg-gray-300"
					:class="{'focus:outline-none': !usedKeyboard}"
					value="3">3</button>
                <button
					class="text-3xl h-24 w-24 bg-gray-400 hover:bg-gray-300"
					:class="{'focus:outline-none': !usedKeyboard}"
					value="2">2</button>
                <button
					class="text-3xl h-24 w-24 bg-gray-400 hover:bg-gray-300"
					:class="{'focus:outline-none': !usedKeyboard}"
					value="1">1</button>
                <button
					class="text-3xl h-24 col-span-3 bg-gray-400 hover:bg-gray-300"
					:class="{'focus:outline-none': !usedKeyboard}"
					value="0">0</button>
            </div>
        </div>
    </div>
	
</div>
	
	
	
	
	
	
</div>

              
            
!

CSS

              
                [x-cloak] { display: none; }
              
            
!

JS

              
                function calculator() {
	return {
		usedKeyboard: false,
		formula: '',
		updateTotal(nextInput) {
			if (this.startsWithOperator(nextInput)) return
			if (this.hasDoubleOperators(nextInput)) return

			if (nextInput === 'BS') {
				// Baackspace
				this.formula = this.formula.slice(0, -1)
			} else if (nextInput === '') {
				// Clear
				this.formula = ''
			} else {
				this.formula = this.formula + nextInput
			}
			
		},
		get total() {
            let total = 0
			let temp
            temp = this.formula.match(/[+\-]*(\.\d+|\d+(\.\d+)?)/g) || []
            while (temp.length) {
                total += parseFloat(temp.shift())
            }
            return total
        },
		hasDoubleOperators(nextInput) {
			return ['+', '-'].includes(nextInput.slice(-1)) && ['+', '-'].includes(this.formula.slice(-1))
		},
		startsWithOperator(nextInput) {
			return ['+', '-'].includes(nextInput) && !this.formula.length
		}
	}
}
              
            
!
999px

Console