Tip Calculator using HTML, CSS and JavaScript - Coding Torque

Tip Calculator using HTML, CSS and JavaScript - Coding Torque

Tip Calculator

Hello Guys! In this blog, I'm going to explain to you how to make a Tip Calculator using HTML, CSS, and JavaScript. You can use this project and upgrade it as a huge app by adding more calculation functionalities to it. This will be a step-by-step guide. Let's get started 🚀.

Let's go step by step:

Step 1: IMPORT font awesome CDN

Now let's import the font awesome CDN in our HTML head tag. fontawesome is a library that is used for icons in a website.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA=="
crossorigin="anonymous" />

Step 2: IMPORT fonts CDN from google fonts

Now let's import the fonts using Google Fonts API. Below is the code for Poppins Font. Paste the below code in head tag.

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">


Step 3: HTML Code


<html lang="en">

    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    
        <!-- Font Awesome Icons  -->
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
            integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA=="
            crossorigin="anonymous" />
    
        <!-- Google Fonts  -->
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
    
        <title>Tip Calculator using JavaScript - @code.scientist x @codingtorque</title>
    </head>
    
    <body>
        <div class="calculator">
            <label for="totalBill">Bill</label>
            <input type="number" oninput="calculateTip()" onchange="calculateTip()" id="totalBill"
                placeholder="Enter Your Bill">
            <label for="tipPercentage" style="display: flex; justify-content:space-between;">
                <span>Tip %</span>
                <span id="tipPercentageText">15%</span>
            </label>
            <input type="range" oninput="calculateTip()" onchange="calculateTip()" id="tipPercentage">
            <label for="numOfPerson" style="display: flex; justify-content:space-between;">
                <span>
                    No. of people
                </span>
                <span id="numOfPersonText">15</span>
            </label>
            <input type="range" oninput="calculateTip()" onchange="calculateTip()" id="numOfPerson">
            <div class="result" id="result">
                <h5 style="margin: 8px 0;display: flex; justify-content:space-between;">
                    <span>Tip</span>
                    <span style="font-size:1.2rem;">__.__</spam>
                </h5>
                <h5 style="margin: 8px 0;display: flex; justify-content:space-between;">
                    <span>Total</span>
                    <span style="font-size:1.2rem;">__.__</spam>
                </h5>
                <h5 style="margin: 8px 0;display: flex; justify-content:space-between;">
                    <span>Each Person Pay</span>
                    <span style="font-size:1.2rem;">__.__</spam>
                </h5>
            </div>
        </div>
    </body>
    
    </html>


Output Till Now

Tip Calculator using HTML, CSS and JavaScript - Coding Torque

Step 4: CSS Code


* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: "Poppins", sans-serif;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #4e4cff;
}

.calculator {
    margin-top: 7rem;
    display: flex;
    flex-direction: column;
    background-color: white;
    border-radius: 15px;
    height: 30rem;
    width: 20rem;
    padding: 20px 30px;
}

#totalBill {
    margin: 10px 0;
    height: 38px;
    border: none;
    background: #f1f1f1;
    padding: 0 10px;
    border-radius: 5px;
}

#totalBill:focus {
    outline: 2px solid #4b49f1;
}

input[type='range'] {
    accent-color: #4b49f1;
    margin: 10px 0;
}

.result {
    width: 100%;
    min-height: 100px;
    margin-top: 20px;
    padding: 20px;
    background-color: #4b49f1;
    border-radius: 15px;
    font-size: 1.2rem;
    color: white;
}
Tip Calculator using HTML, CSS and JavaScript - Coding Torque

Step 5: JavaScript Code

let result = document.getElementById("result");

const calculateTip = () => {
    let bill = Number(document.getElementById("totalBill").value);
    let numOfPerson = Number(document.getElementById("numOfPerson").value);
    let tipPercentage = Number(document.getElementById("tipPercentage").value);

    let tip = bill * (tipPercentage / 100);
    let totalBill = bill + tip;
    let perPersonPay = (totalBill / numOfPerson);

    document.getElementById("tipPercentageText").innerHTML = `${tipPercentage}%`;
    document.getElementById("numOfPersonText").innerHTML = `${numOfPerson}`;

    result.innerHTML = `            
        <h5 style="margin: 8px 0;display: flex; justify-content:space-between;">
    <span>Tip</span>
    <span style="font-size:1.2rem;">${tip.toFixed(2)}</span>
</h5>
<h5 style="margin: 8px 0;display: flex; justify-content:space-between;">
    <span>Total</span>
    <span style="font-size:1.2rem;">${totalBill.toFixed(2)}</span>
</h5>
<h5 style="margin: 8px 0;display: flex; justify-content:space-between;">
    <span>Each Person Pay</span>
    <span style="font-size:1.2rem;">${perPersonPay.toFixed(2)}</span>
</h5>
    `
}

Final Output


If you want me to code any project or post any specific post, feel free to DM me at IG @code.scientist or @codingtorque

 If you have any doubt or any project ideas feel free to Contact Us 

Hope you find this post helpful💖 

 Written by: Coding Torque | Piyush Patil 

 Follow us on Instagram for more projects like this👨‍💻 

Post a Comment

Previous Post Next Post