This code creates a basic calculator interface using HTML and JavaScript. It allows you to perform addition, subtraction, multiplication, and division calculations. The calculator’s main functionality is to display input and calculate results, providing a user-friendly tool for simple arithmetic operations.
You can integrate this calculator widget into your web project wherever basic calculations are required, offering users a convenient tool for performing arithmetic. Furthermore, you can customize the calculator interface with additional CSS according to your web/app theme.
How to Create Simple Calculator Code In HTML and JavaScript
1. First, insert the following HTML code into your project’s HTML file. This code provides the structure and user interface for the calculator.
<p class="m">Calculator</p> <br> <div class="calculator"> <div class="screen">0</div> <br /> <div class="buttons"> <button>7</button> <button>8</button> <button>9</button> <button>+</button> <button>4</button> <button>5</button> <button>6</button> <button>-</button> <button>1</button> <button>2</button> <button>3</button> <button>*</button> <button>0</button> <button>.</button> <button>/</button> <button id="equals">=</button> </div> <br /> <button id="clear">CLEAR</button> </div>
2. Copy the following CSS code into your project’s CSS file or include it in the HTML file using <style>
tags. This CSS code ensures the calculator looks visually appealing.
@import url('https://fonts.googleapis.com/css2?family=Black+Ops+One&family=Borel&family=Cormorant+Upright&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Lato:wght@100;200;300;400;500;700&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Dancing Script&family=Source+Code+Pro'); @import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@100;300;500;600;700&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Creepster&display=swap'); body { background: rgba(0, 0, 0, 0.955); background-attachment: fixed; display: flex; justify-content: center; align-items: center; height: 100vh; } .calculator { background-color: rgb(0, 0, 0); border: solid rgb(44, 44, 44) 1px; padding: 20px; border-radius: 10px; } .screen { width: 224px; background-color: rgba(103, 103, 103, 0.294); border: solid rgb(44, 44, 44) 1px; font-size: 40px; text-align: end; padding: 4px; color: white; } .buttons { width: 232px; display: flex; flex-wrap: wrap; } button { font-family: "Black Ops One"; background-color: rgb(44, 44, 44); border: none; color: rgb(255, 255, 255); font-size: 40px; width: 50px; height: 50px; border-radius: 20px; margin: 4px; } button:hover { background-color: rgb(111, 110, 110); } button:active { background-color: rgb(192, 190, 190); box-shadow: rgba(50, 50, 93, 0.25) 0px 30px 60px -12px inset, rgba(0, 0, 0, 0.3) 0px 18px 36px -18px inset; } #equals { background-color: rgb(252, 20, 27); } #equals:hover { background-color: rgba(198, 1, 1, 0.953); } #clear { width: 100%; background-color: rgb(44, 44, 44); font-size: 30px; } #clear:hover { background-color: rgb(255, 213, 61); color: black; } .m { font-family: "Black Ops One"; margin-top: -550px; text-align: center; align-items: center; color: white; font-size: 50px; position: absolute; }
3. Finally, add the following JavaScript code to your project. This code handles the calculator’s functionality, including button clicks and calculation logic.
const screenDisplay = document.querySelector('.screen') const buttons = document.querySelectorAll('button') let calculation = [] let accumlativeCalculation function calculate(button) { const value = button.textContent if (value == "CLEAR") { calculation = [] screenDisplay.textContent = '0' } else if(value === "="){ screenDisplay.textContent = eval(accumlativeCalculation) } else { calculation.push(value) accumlativeCalculation = calculation.join('') screenDisplay.textContent = accumlativeCalculation } } buttons.forEach(button => button.addEventListener('click', () => calculate(button)))
That’s all! You’ve successfully added a simple calculator widget to your web project. Users can now utilize this tool for basic arithmetic calculations directly on your website. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.