Here's a sleek, modern calculator design in a single HTML file with HTML, CSS, and JavaScript. It features a dark theme, glowing buttons, and smooth animations:
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modern Calculator</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(45deg, #1a1a1a, #2a2a2a);
}
.calculator {
background: #222;
padding: 20px;
border-radius: 15px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
width: 320px;
backdrop-filter: blur(10px);
}
.display {
background: #1a1a1a;
color: #fff;
font-size: 2em;
padding: 20px;
text-align: right;
border-radius: 10px;
margin-bottom: 20px;
overflow-x: auto;
white-space: nowrap;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
button {
padding: 15px;
font-size: 1.2em;
border: none;
border-radius: 10px;
cursor: pointer;
background: #333;
color: #fff;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}
.operator {
background: #ff9500;
}
.operator:hover {
background: #ffaa33;
}
.equals {
background: #00cc66;
grid-column: span 2;
}
.equals:hover {
background: #00e673;
}
.clear {
background: #ff3333;
}
.clear:hover {
background: #ff6666;
}
button:active {
transform: translateY(0);
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}
/* Scrollbar styling for display */
.display::-webkit-scrollbar {
height: 8px;
}
.display::-webkit-scrollbar-track {
background: #1a1a1a;
}
.display::-webkit-scrollbar-thumb {
background: #444;
border-radius: 4px;
}
.display::-webkit-scrollbar-thumb:hover {
background: #555;
}
</style>
</head>
<body>
<div class="calculator">
<div class="display" id="display">0</div>
<div class="buttons">
<button class="clear" onclick="clearDisplay()">C</button>
<button class="operator" onclick="appendToDisplay('/')">/</button>
<button class="operator" onclick="appendToDisplay('*')">×</button>
<button class="operator" onclick="appendToDisplay('-')">-</button>
<button onclick="appendToDisplay('7')">7</button>
<button onclick="appendToDisplay('8')">8</button>
<button onclick="appendToDisplay('9')">9</button>
<button class="operator" onclick="appendToDisplay('+')">+</button>
<button onclick="appendToDisplay('4')">4</button>
<button onclick="appendToDisplay('5')">5</button>
<button onclick="appendToDisplay('6')">6</button>
<button onclick="appendToDisplay('.')">.</button>
<button onclick="appendToDisplay('1')">1</button>
<button onclick="appendToDisplay('2')">2</button>
<button onclick="appendToDisplay('3')">3</button>
<button onclick="appendToDisplay('0')">0</button>
<button class="equals" onclick="calculate()">=</button>
</div>
</div>
<script>
const display = document.getElementById('display');
let currentInput = '0';
let shouldReset = false;
function appendToDisplay(value) {
if (shouldReset) {
currentInput = '0';
shouldReset = false;
}
if (currentInput === '0' && value !== '.') {
currentInput = value;
} else {
currentInput += value;
}
updateDisplay();
}
function clearDisplay() {
currentInput = '0';
updateDisplay();
}
function calculate() {
try {
currentInput = eval(currentInput).toString();
shouldReset = true;
} catch (error) {
currentInput = 'Error';
shouldReset = true;
}
updateDisplay();
}
function updateDisplay() {
display.textContent = currentInput;
// Auto-scroll to the end
display.scrollLeft = display.scrollWidth;
}
// Keyboard support
document.addEventListener('keydown', (event) => {
const key = event.key;
if (!isNaN(key)) {
appendToDisplay(key);
} else if (key === '.') {
appendToDisplay('.');
} else if (key === '+' || key === '-' || key === '*' || key === '/') {
appendToDisplay(key);
} else if (key === 'Enter') {
calculate();
} else if (key === 'Escape') {
clearDisplay();
}
});
</script>
</body>
</html>
This calculator features:
Modern Design:
Dark theme with gradient background
Glassmorphism effect
Smooth animations and transitions
Glowing button effects
Custom scrollbar for the display
Functionality:
Basic arithmetic operations (+, -, ×, /)
Decimal point support
Clear function
Error handling
Keyboard support (numbers, operators, Enter, Escape)
Interactive Features:
Hover effects on buttons
Click animations
Auto-scrolling display for long expressions
Responsive design
Button Categories:
Number buttons (gray)
Operator buttons (orange)
Equals button (green)
Clear button (red)
To use this calculator:
Save the code as calculator.html
Open it in a web browser
Use either mouse clicks or keyboard input:
Numbers and operators work as expected
Enter key for equals
Escape key to clear
Mouse clicks on buttons work too
The calculator handles basic error cases and provides a smooth user experience with animations and visual feedback. The design is modern and professional, suitable for both practical use and as a portfolio piece to showcase web development skills.