Calculator using html css

WHAT TO KNOW - Sep 10 - - Dev Community

<!DOCTYPE html>



Building a Calculator with HTML and CSS

<br> body {<br> font-family: Arial, sans-serif;<br> display: flex;<br> justify-content: center;<br> align-items: center;<br> min-height: 100vh;<br> background-color: #f4f4f4;<br> }</p> <p>.calculator {<br> background-color: #fff;<br> padding: 20px;<br> border-radius: 5px;<br> box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);<br> }</p> <p>.display {<br> background-color: #eee;<br> padding: 10px;<br> border-radius: 5px;<br> font-size: 24px;<br> text-align: right;<br> margin-bottom: 10px;<br> }</p> <p>.buttons {<br> display: grid;<br> grid-template-columns: repeat(4, 1fr);<br> grid-gap: 10px;<br> }</p> <p>.button {<br> background-color: #eee;<br> padding: 15px;<br> border-radius: 5px;<br> text-align: center;<br> font-size: 18px;<br> cursor: pointer;<br> }</p> <p>.button:hover {<br> background-color: #ddd;<br> }</p> <p>.operator {<br> background-color: #ffc107;<br> }</p> <p>.operator:hover {<br> background-color: #ffb300;<br> }</p> <p>.equal {<br> background-color: #28a745;<br> }</p> <p>.equal:hover {<br> background-color: #218838;<br> }</p> <p>.clear {<br> background-color: #dc3545;<br> }</p> <p>.clear:hover {<br> background-color: #c82333;<br> }<br>




0



C


/


*


-


7


8


9


+


4


5


6


=


1


2


3


0


.



<br> let display = document.getElementById(&#39;display&#39;);<br> let currentInput = &#39;&#39;;</p> <p>function appendNumber(number) {<br> currentInput += number;<br> display.textContent = currentInput;<br> }</p> <p>function appendOperator(operator) {<br> currentInput += operator;<br> display.textContent = currentInput;<br> }</p> <p>function calculate() {<br> try {<br> currentInput = eval(currentInput);<br> display.textContent = currentInput;<br> } catch (error) {<br> display.textContent = &#39;Error&#39;;<br> }<br> }</p> <p>function clearDisplay() {<br> currentInput = &#39;&#39;;<br> display.textContent = &#39;0&#39;;<br> }<br>

Building a Calculator with HTML and CSS

This article will guide you through the process of creating a functional calculator using HTML, CSS, and a bit of JavaScript. While this may seem like a simple project, it provides a solid foundation for understanding basic web development principles and mastering interactive elements.

Introduction: Why Build a Calculator?

Building a calculator serves as an excellent entry point into web development. It allows you to:

  • Learn HTML: Structure your calculator's layout using tags.
  • Master CSS: Style the calculator's appearance, creating a visually appealing interface.
  • Introduce JavaScript: Implement the calculator's logic for calculations and user interactions.
  • Develop Problem-Solving Skills: Break down the calculator's functionality into manageable steps.

Furthermore, creating a calculator serves as a stepping stone to more complex projects, as it teaches you the core concepts of building dynamic web applications.

Deep Dive: Concepts and Techniques

Let's delve into the essential components of our calculator:

1. HTML Structure (index.html):

The HTML structure defines the layout of our calculator. We'll use a div container to hold the calculator elements:

<div class="calculator">
 <div class="display" id="display">
  0
 </div>
 <div class="buttons">
 </div>
</div>
  • display: This div will display the current input and results.
  • buttons: This div will contain all the calculator's buttons (numbers, operators, etc.).

2. CSS Styling (style.css):

We'll use CSS to style our calculator, creating a visually appealing and user-friendly interface.

body {
  font-family: Arial, sans-serif;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f4f4f4;
}

.calculator {
  background-color: #fff;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

/* Style the display, buttons, and button types */

3. JavaScript Logic (script.js):

JavaScript is the heart of our calculator, handling user interactions and calculations. We'll use JavaScript to:

  • Handle Button Clicks: Detect when buttons are pressed.
  • Update Display: Modify the text content of the display element.
  • Perform Calculations: Implement the calculator's logic using eval().

Step-by-Step Tutorial: Building the Calculator

Let's build our calculator step-by-step:

1. Create HTML Structure:

  • Create a new HTML file named index.html.
  • Add the basic HTML structure, including the calculator container, display, and buttons divs.

2. Style with CSS:

  • Create a new CSS file named style.css.
  • Link the CSS file to your HTML using the <link/> tag in the <head> section.
  • Style the calculator container, display, and buttons to your liking.

3. Implement JavaScript Functionality:

  • Create a new JavaScript file named script.js.
  • Link the JavaScript file to your HTML using the <script> tag in the <body> section.
  • Add the following JavaScript code to handle button clicks and calculations:
let display = document.getElementById('display');
let currentInput = '';

// Function to append a number to the display
function appendNumber(number) {
  currentInput += number;
  display.textContent = currentInput;
}

// Function to append an operator to the display
function appendOperator(operator) {
  currentInput += operator;
  display.textContent = currentInput;
}

// Function to calculate the result
function calculate() {
  try {
    currentInput = eval(currentInput);
    display.textContent = currentInput;
  } catch (error) {
    display.textContent = 'Error';
  }
}

// Function to clear the display
function clearDisplay() {
  currentInput = '';
  display.textContent = '0';
}

4. Add Buttons to the HTML:

  • In your buttons div, add buttons for numbers (0-9), operators (+, -, *, /), an equal sign (=), and a clear button (C).

5. Connect Buttons to JavaScript Functions:

  • Use the onclick attribute on each button to call the corresponding JavaScript function.
  • For example:
<button class="button" onclick="appendNumber('7')">7</button>

6. Run the Calculator:

  • Open index.html in your web browser to see your calculator in action.

Example: A Basic Calculator (HTML, CSS, JavaScript)

<!DOCTYPE html>
<html>
<head>
<title>Basic Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>

<div class="calculator">
  <div class="display" id="display">0</div>
  <div class="buttons">
    <button class="button clear" onclick="clearDisplay()">C</button>
    <button class="button operator" onclick="appendOperator('/')">/</button>
    <button class="button operator" onclick="appendOperator('*')">*</button>
    <button class="button operator" onclick="appendOperator('-')">-</button>
    <button class="button" onclick="appendNumber('7')">7</button>
    <button class="button" onclick="appendNumber('8')">8</button>
    <button class="button" onclick="appendNumber('9')">9</button>
    <button class="button operator" onclick="appendOperator('+')">+</button>
    <button class="button" onclick="appendNumber('4')">4</button>
    <button class="button" onclick="appendNumber('5')">5</button>
    <button class="button" onclick="appendNumber('6')">6</button>
    <button class="button equal" onclick="calculate()">=</button>
    <button class="button" onclick="appendNumber('1')">1</button>
    <button class="button" onclick="appendNumber('2')">2</button>
    <button class="button" onclick="appendNumber('3')">3</button>
    <button class="button" onclick="appendNumber('0')">0</button>
    <button class="button" onclick="appendNumber('.')">.</button>
  </div>
</div>

<script src="script.js">
 </script>

Conclusion: Best Practices and Further Enhancements

  • Valid HTML: Ensure your HTML code is well-structured and validated for optimal compatibility.
  • Semantic HTML: Use appropriate HTML elements for their intended purpose (e.g., button for buttons, div for containers).
  • CSS Organization: Use CSS classes and IDs effectively to organize your styles and make them reusable.
  • JavaScript Efficiency: Optimize your JavaScript code for performance and maintainability.
  • Error Handling: Implement error handling mechanisms to prevent unexpected behavior.
  • Accessibility: Consider accessibility best practices to make your calculator usable by people with disabilities.
  • Responsive Design: Ensure your calculator looks good and functions correctly on different screen sizes.
  • Advanced Features: Explore adding features like:
    • Memory Functions (M+, M-, MR)
    • Percentage Calculation (%)
    • Scientific Functions (sin, cos, tan, etc.)
    • History of Calculations

By building a simple calculator, you've taken your first steps towards becoming a web developer. You've learned the fundamentals of HTML, CSS, and JavaScript, and you've gained valuable experience in interactive web development. As you progress, you can build upon this foundation to create more complex and impressive web applications.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player