You are currently viewing How to Build a Tip Calculator Using HTML, CSS & JavaScript

How to Build a Tip Calculator Using HTML, CSS & JavaScript

How to Build a Tip Calculator Using HTML, CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make a Calculator which will indicate how much tip should be given according to the bill and no of people. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Project.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Tip Calculator. Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Tip Calculator project. At last, we will use JS (JavaScript) which will add logic to make the Tip Calculator project responsive from the user end.

In this blog post, we will discuss How to Build a Tip Calculator Using HTML, CSS & JavaScript with complete source code so you can just copy and paste them into your own project. Happy exploring and learning !!

I hope you have got an idea about the project.

HTML Code for Calculator

<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tip Calculator</title>
    <!--Google Font-->
    <link href="https://fonts.googleapis.com/css2?family=Rubik:wght@400;500&display=swap" rel="stylesheet">
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <section>
            <div class="wrapper">
                <label for="bill" class="lbl">
                    Bill
                </label>
                <div class="input-box">
                    <span>$</span>
                    <input type="number" value="0.00" id="bill" class="val">
                </div>
            </div>
            <div class="wrapper">
                <span class="lbl">Tip</span>
                <span id="tip-amount" class="val">
                    $0
                </span>
            </div>
            <hr>
            <div class="wrapper">
                <span class="lbl">Total Amount</span>
                <span id="total-amount" class="val">
                    $0
                </span>
            </div>
        </section>
        <section>
            <div class="wrapper">
                <label for="tip" class="lbl">
                    Tip %
                </label>
                <span id="tip-percent" class="val">
                    0
                </span>
            </div>
            <input type="range" min="1" id="tip" value="15">
            <div class="wrapper">
                <label for="no-of-people" class="lbl">
                    No. Of People
                </label>
                <span id="split-num" class="val">
                    0
                </span>
            </div>
            <input type="range" min="1" max="15" id="no-of-people" value="1">
        </section>
        <section>
            <div class="wrapper">
                <span class="lbl">
                    Tip Per Person
                </span>
                <span id="tip-per-person" class="val">
                    $0
                </span>
            </div>
            <div class="wrapper">
                <span class="lbl">
                    Total Per Person
                </span>
                <span id="total-per-person" class="val">
                    $0
                </span>
            </div>
        </section>
    </div>
    <script src="script.js"></script>
</body>
</html>

First, we’ll start with creating the structure of the Tip Calculator project for that as you can see in the above code we have used all the necessary elements & attributes. Let us know code the CSS part to add styling and aligned the tags.

How to Create Palindrome Checker Using HTML, CSS & JavaScript

CSS Code for Calculator

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: "Rubik",sans-serif;
    letter-spacing: 0.4px;
}
body{
    background-color: orange;
    height: 100%;
    background-size: 100% 50%;
    background-repeat: no-repeat;
}
.container{
    background-color: #ffffff;
    width: 450px;
    padding: 50px 40px;
    border-radius: 10px;
    position: absolute;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
    box-shadow: 0 30px 50px rgba(0,0,0,0.1);
}
.wrapper{
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 12px 0;
}
.lbl{
    font-size: 16px;
    color: #44475b;
    font-weight: 400;
}
.val{
    color: #44475b;
    font-weight: 500;
    font-size: 18px;
}
.input-box{
    background-color: #cdccfd;
    color: #6f6df4;
    padding: 5px;
}
#bill{
    background-color: transparent;
    border: none;
    outline: none;
    width: 100px;
    text-align: right;
    color: #6f6df4;
}
hr{
    border: none;
    border-bottom: 1px solid #cdccfd;
}
input[type="range"]{
    width: 100%;
    cursor: pointer;
}
input[type="range"]:not(:last-child){
    margin-bottom: 5px;
}
section:not(:last-child){
    margin-bottom: 40px;
}
section:not(:first-child){
    margin-top: 40px;
}
section:last-child{
    background-color: #cdccfd;
    padding: 10px;
    border-radius: 5px;
}
section:last-child .val,
section:last-child .lbl{
    color: #4c4af4;
}

Second, comes the CSS code which we have styled for the structure we have padded as well as aligned the Tip Calculator project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now, let’s code the JavaScript part to make it responsive.

Language Translator Using HTML, CSS & JavaScript

JavaScript Code for Calculator

const sliders = document.querySelectorAll("input[type='range']");
sliders.forEach(function(slider){
    slider.addEventListener("input",calculateTip);
});

const billInput = document.getElementById("bill");
billInput.addEventListener("change",calculateTip);


function calculateTip(){
    let bill = parseFloat(billInput.value);
    let tipPercent = document.getElementById("tip").value;
    let noOfPeople = document.getElementById("no-of-people").value;

    billInput.value = bill.toFixed(2);

    let totalTip = parseFloat((bill * (tipPercent/100)).toFixed(2));
    let total = parseFloat((bill + totalTip).toFixed(2));

    let tipPerPerson = (totalTip / noOfPeople).toFixed(2);
    let totalPerPerson = (total / noOfPeople).toFixed(2);

    document.getElementById("tip-amount").textContent = `\$ ${totalTip}`;
    document.getElementById("total-amount").textContent = `\$ ${total}`;
    
    document.getElementById("tip-percent").textContent = `${tipPercent}%`;
    document.getElementById("split-num").textContent = noOfPeople;

    document.getElementById("tip-per-person").textContent = `\$ ${tipPerPerson}`;
    document.getElementById("total-per-person").textContent = `\$ ${totalPerPerson}`;
}
calculateTip();

Last stage of the project the JavaScript which we added the logic and coded as per the requirement with some conditions. Let us see the Final Output of the project Tip Calculator using HTML, CSS & JavaScript 

Final Output


We have successfully created our Tip Calculator using HTML, CSS & JavaScript (Source Code). You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search codewithrandom on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Thank You and Happy Learning!!!

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9

Leave a Reply