Building a Colorful Calculator with HTML and JavaScript

Spread the love

Calculators are an essential tool in various industries, from finance to engineering. With the rise of web development, it is no surprise that developers have created calculators for the web. One such calculator is the colorful calculator built using HTML and JavaScript. This article will walk you through the process of building a colorful calculator, and we’ll discuss the importance of creating a responsive design.

Building a Colorful Calculator

The colorful calculator is built using HTML, CSS, and JavaScript. HTML is used to create the structure of the calculator, CSS for styling, and JavaScript to handle the calculator’s functions. Let’s break down the code and see how each part works together.

HTML Structure

The calculator’s structure consists of a div with an ID of “calculator” that holds the input field and the calculator’s buttons. The input field is an HTML input element with a type of “text,” and an ID of “result.” The calculator’s buttons are also input elements, but their type is “button,” and each has a value that corresponds to a number or an arithmetic operator.

CSS Styling

The CSS styling gives the calculator its colorful and sleek design. The background color of the calculator is set to a light grey (#ECECEC), and the font is set to Arial, sans-serif. The calculator has a box-shadow property that adds a slight drop shadow effect. The buttons are styled with a background color of light peach (#FFDAB9), and they have a border-radius of 5px to give them a rounded look.

JavaScript Functions

The JavaScript functions handle the calculator’s operations. The addToResult function is responsible for adding the value of the clicked button to the input field. The clearResult function resets the input field to an empty string. The calculateResult function uses the eval method to evaluate the input field’s value and displays the result in the input field.

Creating a Responsive Design

A responsive design is essential for any web development project, including a calculator. A responsive design ensures that the calculator looks good and functions properly on any device, whether it’s a desktop or a mobile device.

To create a responsive design, we used media queries in the CSS styling. The media queries adjust the calculator’s width, font-size, and button size based on the device’s screen size. The input field also adjusts its width to fit the device’s screen size.

here are the steps to build the colorful calculator using HTML and JavaScript:

  1. Open a text editor or an integrated development environment (IDE) such as Visual Studio Code or Atom.
  2. Create a new HTML file and save it with a filename of your choice, such as “calculator.html”.
  3. In the HTML file, create a div with an ID of “calculator”. Inside the div, create an input field with a type of “text” and an ID of “result”.
  4. Below the input field, create a series of input elements with a type of “button”. Each button should have a value corresponding to a number or an arithmetic operator such as addition, subtraction, multiplication, and division. Give each button a class name of “button” for styling.
  5. In the head section of the HTML file, link a CSS file that will contain the styling for the calculator. Create a new CSS file and save it with a filename of your choice, such as “calculator.css”.
  6. In the CSS file, style the calculator’s background color, font, button size, and other styling properties as desired.
  7. Add media queries to the CSS file to ensure that the calculator’s design is responsive and adapts to various screen sizes.
  8. In the HTML file, create a script tag and add JavaScript code to handle the calculator’s operations.
  9. Define three functions in the JavaScript code: addToResult, clearResult, and calculateResult. The addToResult function should add the clicked button’s value to the input field, the clearResult function should reset the input field to an empty string, and the calculateResult function should evaluate the input field’s value and display the result in the input field.
  10. Add event listeners to the buttons in the JavaScript code. When a button is clicked, call the appropriate function to handle the button’s value.
  11. Save the files and open the HTML file in a web browser to test the calculator.

By following these steps, you can create a colorful calculator using HTML and JavaScript that has a responsive design and basic arithmetic functions.




<!DOCTYPE html>
		body {
			background-color: #ECECEC;
			font-family: Arial, sans-serif;

		h1 {
			text-align: center;
			color: #333333;
			margin-top: 30px;

		#calculator {
			width: 300px;
			margin: 0 auto;
			background-color: #FFFFFF;
			border-radius: 5px;
			padding: 20px;
			box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
			text-align: center;
			font-size: 24px;

		input[type="button"] {
			background-color: #FFDAB9;
			color: #333333;
			border: none;
			padding: 10px;
			margin: 5px;
			border-radius: 5px;
			width: 50px;
			height: 50px;
			font-size: 24px;
			cursor: pointer;

		input[type="text"] {
			background-color: #F0F0F0;
			color: #333333;
			border: none;
			padding: 10px;
			margin: 5px;
			border-radius: 5px;
			width: 90%;
			font-size: 24px;
			text-align: right;
			-webkit-appearance: none;
	<div id="calculator">
		<input type="text" id="result" disabled>
		<input type="button" value="1" onclick="addToResult('1')">
		<input type="button" value="2" onclick="addToResult('2')">
		<input type="button" value="3" onclick="addToResult('3')">
		<input type="button" value="+" onclick="addToResult('+')">
		<input type="button" value="4" onclick="addToResult('4')">
		<input type="button" value="5" onclick="addToResult('5')">
		<input type="button" value="6" onclick="addToResult('6')">
		<input type="button" value="-" onclick="addToResult('-')">
		<input type="button" value="7" onclick="addToResult('7')">
		<input type="button" value="8" onclick="addToResult('8')">
		<input type="button" value="9" onclick="addToResult('9')">
		<input type="button" value="*" onclick="addToResult('*')">
		<input type="button" value="C" onclick="clearResult()">
		<input type="button" value="0" onclick="addToResult('0')">
		<input type="button" value="/" onclick="addToResult('/')">
		<input type="button" value="=" onclick="calculateResult()">

		function addToResult(value) {
			document.getElementById('result').value += value;

		function clearResult() {
			document.getElementById('result').value = '';

		function calculateResult() {
			var result = eval(document.getElementById('result').value);
			document.getElementById('result').value = result;

The above code can be used in various web development projects that require a simple yet stylish calculator. Here are some examples:

  1. Finance apps: Financial calculators are essential for apps that deal with investments, loans, mortgages, and other financial calculations. The colorful calculator can be customized to include financial functions and integrated into finance apps.
  2. Educational websites: Educational websites that teach math, science, or engineering can use the colorful calculator to provide students with a tool for solving equations and performing calculations.
  3. Games: Games that involve math or strategy can use the colorful calculator as a gameplay element. For example, a game that involves solving math problems could use the calculator as a tool for entering answers.
  4. E-commerce websites: E-commerce websites that sell products that require size, weight, or other measurements can use the colorful calculator as a tool for calculating the total price based on the input values.

Overall, the colorful calculator code can be used in various web development projects where a calculator is required, and the code can be easily customized to fit the project’s needs.


In conclusion, building a colorful calculator with HTML and JavaScript is a fun and educational project for any web developer. The colorful design makes it stand out, and the responsive design ensures that it functions properly on any device. Whether you’re building a calculator for a finance app or a math game, this code can be easily customized to fit your needs.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *