<main class="container">
<div class="calculator-case">
<input id="display" class="display" readonly type="text" dir="rtl" maxlength="8" placeholder="" />
<div class="button-grid">
<div class="top-rows">
<div class="button-body button-blue">
<button class="button-cap blue-cap-blue">C</button>
</div>
<div class="button-body button-blue">
<button class="button-cap blue-cap-blue">CE</button>
</div>
<div class="button-body button-blue">
<button class="button-cap blue-cap-blue">%</button>
</div>
<div class="button-body button-blue">
<button class="button-cap blue-cap-blue">÷</button>
</div>
<div class="button-body">
<button class="button-cap">7</button>
</div>
<div class="button-body">
<button class="button-cap">8</button>
</div>
<div class="button-body">
<button class="button-cap">9</button>
</div>
<div class="button-body button-blue">
<button class="button-cap blue-cap-blue">x</button>
</div>
<div class="button-body">
<button class="button-cap">4</button>
</div>
<div class="button-body">
<button class="button-cap">5</button>
</div>
<div class="button-body">
<button class="button-cap">6</button>
</div>
<div class="button-body button-blue">
<button class="button-cap blue-cap-blue">-</button>
</div>
<div class="button-body">
<button class="button-cap">1</button>
</div>
<div class="button-body">
<button class="button-cap">2</button>
</div>
<div class="button-body">
<button class="button-cap">3</button>
</div>
<div class="button-body button-blue">
<button class="button-cap blue-cap-blue">+</button>
</div>
</div>
<div class="bottom-row">
<div class="button-body wider-button">
<button class="button-cap wider-cap">0</button>
</div>
<div class="button-body">
<button class="button-cap">.</button>
</div>
<div class="button-body wider-button">
<button class="button-cap wider-cap">=</button>
</div>
</div>
</div>
</main>
@import url(https://fonts.googleapis.com/css?family=Audiowide);
* {
box-sizing: border-box;
}
body {
padding: 0;
margin: 0;
}
// I'm using this to center the button to the screen
.container {
/* layout */
display: flex;
width: 100vw;
height: 100vh;
justify-content: center;
align-items: center;
/* style */
background: #2b3d25;
}
.button-grid {
display: grid;
}
.top-rows {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 3px;
}
.button-body {
/* layout */
display: flex;
justify-content: center;
width: 75px;
height: 75px;
padding-top: 2px;
/* style */
background: linear-gradient(145deg, rgb(120, 118, 99), #fff9d6);
border-radius: 3px;
box-shadow: 0 0 0 1px rgba(16, 22, 26, 0.1), 0 0px 2px rgba(16, 22, 26, 0.2),
0 0px 1px rgba(16, 22, 26, 0.2);
/* effects */
&:active {
transform: scale(0.95); /* Add this line */
background: linear-gradient(145deg, rgb(120, 118, 99), #fff9d6);
box-shadow: 0 0 0 1px rgba(16, 22, 26, 0.1), 0 0px 2px rgba(16, 22, 26, 0.2),
0 0px 1px rgba(16, 22, 26, 0.2);
}
}
.button-cap {
/* layout */
width: 65px;
height: 65px;
border-radius: 5px;
/* style */
border: none;
background: linear-gradient(145deg, #d6d2b4, #fff9d6);
box-shadow: 0 0 0 1px rgba(16, 22, 26, 0.1), 0 0px 2px rgba(16, 22, 26, 0.2),
0 0px 1px rgba(16, 22, 26, 0.2);
/* typography */
font-family: "Audiowide", cursive;
color: black;
font-size: larger;
/* effects */
&:active {
transform: scale(0.95); /* Add this line */
background: linear-gradient(145deg, #d6d2b4, #fff9d6);
box-shadow: 0 0 0 1px rgba(16, 22, 26, 0.1), 0 0px 2px rgba(16, 22, 26, 0.2),
0 0px 1px rgba(16, 22, 26, 0.2);
}
&:hover {
cursor: pointer;
}
}
.button-blue {
background: linear-gradient(145deg, rgb(110, 131, 129), #d6fff9);
/* effects */
&:active {
transform: scale(0.95); /* Add this line */
background: linear-gradient(145deg, rgb(110, 131, 129), #d6fff9);
box-shadow: 0 0 0 1px rgba(16, 22, 26, 0.1), 0 0px 2px rgba(16, 22, 26, 0.2),
0 0px 1px rgba(16, 22, 26, 0.2);
}
}
.blue-cap-blue {
background: linear-gradient(145deg, #b4d6d2, #d6fff9);
&:active {
transform: scale(0.95); /* Add this line */
background: linear-gradient(145deg, #b4d6d2, #d6fff9);
box-shadow: 0 0 0 1px rgba(16, 22, 26, 0.1), 0 0px 2px rgba(16, 22, 26, 0.2),
0 0px 1px rgba(16, 22, 26, 0.2);
}
}
.bottom-row {
display: flex;
justify-content: space-between;
gap: 5px;
padding-top: 3px;
}
.wider-button {
width: 35%;
}
.wider-cap {
width: 90%;
}
.display {
/* layout */
overflow: hidden;
padding: 5px;
height: 75px;
width: 320px;
/* style */
background: rgba(56, 62, 37, 1);
box-shadow: inset 1px 1px 6px 0px #050505, 0px 0px 4px 2px #000,
0px 0px 4px 2px #777;
/* typography */
color: black;
font-size: 3rem;
font-family: "Audiowide", cursive;
/* effects */
&:hover {
cursor: cell;
}
}
.calculator-case {
/* layout */
display: flex;
flex-direction: column;
gap: 20px;
padding: 25px;
/* style */
border-radius: 15px;
background: #d6d2b4;
box-shadow: inset 5px 5px 18px #c7c3a7, inset -5px -5px 18px #e5e1c1;
}
View Compiled
document.addEventListener('DOMContentLoaded', function () {
const display = document.getElementById('display');
const buttons = document.querySelectorAll('.button-cap');
let currentInput = '';
let previousInput = '';
let operator = '';
buttons.forEach(button => {
button.addEventListener('click', function () {
handleButtonClick(this.innerText);
updateDisplay();
});
});
function handleButtonClick(value) {
if (isNumber(value) || value === '.') {
handleNumberClick(value);
} else if (isOperator(value)) {
handleOperatorClick(value);
} else if (value === 'C') {
clearDisplay();
} else if (value === 'CE') {
clearEntry();
} else if (value === '%') {
calculatePercentage();
} else if (value === '=') {
calculateResult();
}
}
function isNumber(value) {
return !isNaN(parseFloat(value)) && isFinite(value);
}
function isOperator(value) {
return ['+', '-', 'x', '÷'].includes(value);
}
function handleNumberClick(value) {
if (currentInput.length < 8) {
currentInput += value;
}
}
function handleOperatorClick(value) {
if (currentInput !== '') {
if (previousInput !== '') {
calculateResult();
}
operator = value;
previousInput = currentInput;
currentInput = '';
}
}
function calculateResult() {
if (currentInput !== '') {
switch (operator) {
case '+':
currentInput = (parseFloat(previousInput) + parseFloat(currentInput)).toString();
break;
case '-':
currentInput = (parseFloat(previousInput) - parseFloat(currentInput)).toString();
break;
case 'x':
currentInput = (parseFloat(previousInput) * parseFloat(currentInput)).toString();
break;
case '÷':
currentInput = (parseFloat(previousInput) / parseFloat(currentInput)).toString();
break;
}
operator = '';
previousInput = '';
}
currentInput = currentInput.length > 8 ? 'ERROR' : currentInput;
}
function calculatePercentage() {
if (currentInput !== '') {
currentInput = (parseFloat(currentInput) / 100).toString();
}
}
function clearDisplay() {
currentInput = '';
previousInput = '';
operator = '';
}
function clearEntry() {
currentInput = '';
}
function updateDisplay() {
display.value = currentInput === '' ? '0' : currentInput;
}
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.