Ad GEEKSFORGEEKS HTML Calculator HTML calculator is used to perform the basic mathematical operations like Addition, subtraction, multiplication, and division. To design the basic calculator, we will use HTML, CSS, and JavaScript. HTML is used to design the basic structure of the calculator. CSS styles are used to apply styles on the calculator and JavaScript is used to add the calculation functionality. Complete Code: <html> <head> <script> //function that display value function dis(val) { document.getElementById("result").value+=val } //function that evaluates the digit and return result function solve() { let x = document.getElementById("result").value let y = eval(x) document.getElementById("result").value = y } //function that clear the display function clr() { document.getElementById("result").value = "" } </script> <!-- for styling --> <style> .title{ margin-bottom: 10px; text-align:center; width: 210px; color:green; border: solid black 2px; } input[type="button"] { background-color:green; color: black; border: solid black 2px; width:100% } input[type="text"] { background-color:white; border: solid black 2px; width:100% } </style> </head> <!-- create table --> <body> <div class = title >GeeksforGeeks Calculator</div> <table border="1"> <tr> <td colspan="3"><input type="text" id="result"/></td> <!-- clr() function will call clr to clear all value --> <td><input type="button" value="c" onclick="clr()"/> </td> </tr> <tr> <!-- create button and assign value to each button --> <!-- dis("1") will call function dis to display value --> <td><input type="button" value="1" onclick="dis('1')"/> </td> <td><input type="button" value="2" onclick="dis('2')"/> </td> <td><input type="button" value="3" onclick="dis('3')"/> </td> <td><input type="button" value="/" onclick="dis('/')"/> </td> </tr> <tr> <td><input type="button" value="4" onclick="dis('4')"/> </td> <td><input type="button" value="5" onclick="dis('5')"/> </td> <td><input type="button" value="6" onclick="dis('6')"/> </td> <td><input type="button" value="-" onclick="dis('-')"/> </td> </tr> <tr> <td><input type="button" value="7" onclick="dis('7')"/> </td> <td><input type="button" value="8" onclick="dis('8')"/> </td> <td><input type="button" value="9" onclick="dis('9')"/> </td> <td><input type="button" value="+" onclick="dis('+')"/> </td> </tr> <tr> <td><input type="button" value="." onclick="dis('.')"/> </td> <td><input type="button" value="0" onclick="dis('0')"/> </td> <!-- solve function call function solve to evaluate value --> <td><input type="button" value="=" onclick="solve()"/> </td> <td><input type="button" value="*" onclick="dis('*')"/> </td> </tr> </table> </body> </html>
Write, Run & Share HTML code online using OneCompiler's HTML online Code editor for free. It's one of the robust, feature-rich online Code editor for HTML language, running on the latest version HTML5. Getting started with the OneCompiler's HTML compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as HTML
. You can also specify the stylesheet information in styles.css
tab and scripts information in scripts.js
tab and start coding.
HTML(Hyper Text Markup language) is the standard markup language for Web pages, was created by Berners-Lee in the year 1991. Almost every web page over internet might be using HTML.
<!DOCTYPE html>
<html>
and ends with </html>
<h1>
to <h6>
where <h1>
is the highest important heading and <h6>
is the least important sub-heading.<p>..</p>
tag.<a>
tag.
<a href="https://onecompiler.com/html">HTML online compiler</a>
<img>
tag, where src
attribute consists of image name.<button>..</button>
tag<ul>
for unordered/bullet list and <ol>
for ordered/number list, and the list items are defined in <li>
.<a href="https://onecompiler.com/html">HTML online compiler</a>
CSS(cascading style sheets) describes how HTML elements will look on the web page like color, font-style, font-size, background color etc.
Below is a sample style sheet which displays heading in green and in Candara font with padding space of 25px.
body{
padding: 25px;
}
.title {
color: #228B22;
font-family: Candara;
}
<table>
tag.<tr>
tag<th>
tag<td>
tag<caption>
tag<script>
is the tag used to write scripts in HTML<script src="script.js"></script>