Simple Calculator Project Using Bootstrap and JavaScript in HTML/CSS
Key features Bootstrap 5 responsive layout Vanilla JavaScript with safe evaluation (basic sanitization) Keyboard support Clear, backspace, percentage, decimals Accessible buttons (ARIA labels & focus states) Mobile-friendly html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>Simple Calculator</title> <!-- Bootstrap 5 CDN --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-..." crossorigin="anonymous"> <style> body { background: #f0f2f5; min-height: 100vh; display: flex; align-items: center; padding: 1rem; } .calc { max-width: 380px; margin: 0 auto; } .display { font-size: 1.75rem; text-align: right; padding: 0.75rem; background: #ffffff; border-radius: 0.5rem; box-shadow: inset 0 0 4px rgba(0,0,0,0.08); min-height: 3.2rem; word-wrap: break-word; letter-spacing: 0.5px; } .btn-calculator { font-size: 1.1rem; padding: 0.75rem 0.5rem; border-radius: 0.5rem; min-width: 0; } .btn-operator { background-color: #ffb400; color: #fff; } .btn-equals { background-color: #28a745; color: #fff; } .btn-secondary-custom { background-color: #6c757d; color: white; } .visually-hidden-focus:focus { outline: 3px solid #0d6efd; outline-offset: 2px; } </style> </head> <body> <div class="calc card shadow-sm"> <div class="card-body"> <div class="mb-3"> <div id="display" class="display" role="textbox" aria-label="Calculator display" aria-live="polite">0</div> </div> <div class="gx-1 gy-1 row row-cols-4"> <!-- First row --> <div class="col mb-1"> <button class="btn btn-secondary btn-calculator w-100 visually-hidden-focus" data-action="clear" aria-label="Clear">C</button> </div> <div class="col mb-1"> <button class="btn btn-secondary btn-calculator w-100 visually-hidden-focus" data-action="back" aria-label="Backspace">←</button> </div> <div class="col mb-1"> <button class="btn btn-secondary btn-calculator w-100 visually-hidden-focus" data-value="%" aria-label="Percent">%</button> </div> <div class="col mb-1"> <button class="btn btn-operator btn-calculator w-100 visually-hidden-focus" data-value="/" aria-label="Divide">÷</button> </div> <!-- Second row --> <div class="col mb-1"> <button class="btn btn-light btn-calculator w-100 visually-hidden-focus" data-value="7" aria-label="7">7</button> </div> <div class="col mb-1"> <button class="btn btn-light btn-calculator w-100 visually-hidden-focus" data-value="8" aria-label="8">8</button> </div> <div class="col mb-1"> <button class="btn btn-light btn-calculator w-100 visually-hidden-focus" data-value="9" aria-label="9">9</button> </div> <div class="col mb-1"> <button class="btn btn-operator btn-calculator w-100 visually-hidden-focus" data-value="*" aria-label="Multiply">×</button> </div> <!-- Third row --> <div class="col mb-1"> <button class="btn btn-light btn-calculator w-100 visually-hidden-focus" data-value="4" aria-label="4">4</button> </div> <div class="col mb-1"> <button class="btn btn-light btn-calculator w-100 visually-hidden-focus" data-value="5" aria-label="5">5</button> </div> <div class="col mb-1"> <button class="btn btn-light btn-calculator w-100 visually-hidden-focus" data-value="6" aria-label="6">6</button> </div> <div class="col mb-1"> <button class="btn btn-operator btn-calculator w-100 visually-hidden-focus" data-value="-" aria-label="Subtract">−</button> </div> <!-- Fourth row --> <div class="col mb-1"> <button class="btn btn-light btn-calculator w-100 visually-hidden-focus" data-value="1" aria-label="1">1</button> </div> <div class="col mb-1"> <button class="btn btn-light btn-calculator w-100 visually-hidden-focus" data-value="2" aria-label="2">2</button> </div> <div class="col mb-1"> <button class="btn btn-light btn-calculator w-100 visually-hidden-focus" data-value="3" aria-label="3">3</button> </div> <div class="col mb-1"> <button class="btn btn-operator btn-calculator w-100 visually-hidden-focus" data-value="+" aria-label="Add">+</button> </div> <!-- Fifth row --> <div class="col-6 mb-1"> <button class="btn btn-light btn-calculator w-100 visually-hidden-focus" data-value="0" aria-label="0">0</button> </div> <div class="col mb-1"> <button class="btn btn-light btn-calculator w-100 visually-hidden-focus" data-value="." aria-label="Decimal point">.</button> </div> <div class="col mb-1"> <button class="btn btn-equals btn-calculator w-100 visually-hidden-focus" data-action="equals" aria-label="Equals">=</button> </div> </div> </div> <div class="card-footer text-muted small"> MIT Licensed — original by Rhalp Darren Cabrera. Keep attribution when redistributing. </div> </div> <script> (() => { const displayEl = document.getElementById('display'); let expr = ''; function updateDisplay(text) { displayEl.textContent = text || '0'; } function sanitize(input) { // Allow digits, operators, dot, parentheses, percent, spaces return /^[0-9+\-*/%.() ]*$/.test(input); } function evaluateExpression(input) { if (!sanitize(input)) return 'Error'; try { // Convert percentages like 25% to (25/100) const replaced = input.replace(/(\d+(\.\d+)?)%/g, '($1/100)'); // eslint-disable-next-line no-eval let result = eval(replaced); if (typeof result === 'number') { if (!isFinite(result)) return 'Error'; // Trim floating point artifacts result = Math.round((result + Number.EPSILON) * 1e12) / 1e12; return result; } return 'Error'; } catch { return 'Error'; } } function appendValue(v) { // Prevent two operators in a row (simple) if (/[\+\-*/%.]$/.test(expr) && /[\+\-*/%.]/.test(v)) { expr = expr.slice(0, -1) + v; } else { expr += v; } } document.querySelectorAll('button').forEach(btn => { btn.addEventListener('click', () => { const val = btn.getAttribute('data-value'); const action = btn.getAttribute('data-action'); if (action === 'clear') { expr = ''; } else if (action === 'back') { expr = expr.slice(0, -1); } else if (action === 'equals') { const res = evaluateExpression(expr); expr = (res === 'Error') ? '' : String(res); } else if (val !== null) { appendValue(val); } updateDisplay(expr); }); }); // Keyboard support document.addEventListener('keydown', e => { const key = e.key; if (/[\d.+\-*/%()]/.test(key)) { appendValue(key); } else if (key === 'Enter' || key === '=') { const res = evaluateExpression(expr); expr = (res === 'Error') ? '' : String(res); } else if (key === 'Backspace') { expr = expr.slice(0, -1); } else if (key.toLowerCase() === 'c') { expr = ''; } else { return; } e.preventDefault(); updateDisplay(expr); }); updateDisplay(expr); })(); </script> </body> </html> |
What to do with it Use as-is: Save above as calculator.html and open in a browser. Extend: Add memory buttons (M+, MR), scientific functions, theme toggles. …
Simple Calculator Project Using Bootstrap and JavaScript in HTML/CSS Read More »