How to design a Loan EMI Calculator using HTML, CSS and JavaScript?

To design a Loan EMI Calculator, we will be using HTML to create the basic structure, CSS to design the user interface, and JavaScript to add functionality for calculating EMI based on loan amount, interest rate, and time period.

In this article, we will understand the step-by-step process of creating a loan EMI calculator with a clean, interactive interface.

Table of Contents

Structuring Loan EMI Calculator using HTML

We will create the HTML structure with the following components:

  • Three input fields for loan amount, interest rate, and time period
  • A calculate button to trigger the EMI calculation
  • A result display area using span tag
  • All elements wrapped in a container div
<div class="container">
    <h1>Loan EMI Calculator</h1>
    <p>Loan Amount: $
        <input type="number" id="amount" placeholder="Enter loan amount">
    </p>
    <p>Interest Rate: %
        <input type="number" step="0.1" id="rate" placeholder="Enter annual rate">
    </p>
    <p>Time Period (Months):
        <input type="number" id="time" placeholder="Enter time in months">
    </p>
    <input type="button" value="Calculate EMI" id="btn" onclick="calculate()">
    <p>Monthly EMI: $
        <span id="output">0.00</span>
    </p>
</div>

Designing UI of Loan EMI Calculator using CSS

We will style the calculator with modern CSS properties:

  • The container class creates a centered, styled calculator box
  • Using Flexbox for layout management
  • Responsive design with proper spacing and colors
  • Interactive button styling with hover effects
.container {
    width: 400px;
    max-width: 90%;
    background-color: #031926;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: stretch;
    padding: 30px;
    border-radius: 15px;
    color: white;
    margin: 50px auto;
    box-shadow: 0 10px 25px rgba(0,0,0,0.3);
}

h1 {
    color: #4CAF50;
    text-align: center;
    margin-bottom: 25px;
    font-family: Arial, sans-serif;
}

p {
    color: white;
    font-size: 18px;
    font-family: Arial, sans-serif;
    margin-bottom: 15px;
    font-weight: 500;
}

input[type="number"] {
    width: 100%;
    height: 40px;
    background-color: white;
    font-size: 16px;
    color: #031926;
    padding: 10px;
    margin-top: 5px;
    border: none;
    border-radius: 5px;
    box-sizing: border-box;
}

#btn {
    background-color: #4CAF50;
    color: white;
    padding: 12px 25px;
    font-size: 16px;
    font-weight: bold;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    margin: 20px auto;
    transition: background-color 0.3s;
}

#btn:hover {
    background-color: #45a049;
}

#output {
    font-weight: bold;
    color: #4CAF50;
    font-size: 20px;
}

::placeholder {
    color: #666;
    opacity: 0.8;
}

Adding Functionalities using JavaScript

The JavaScript function implements the standard EMI calculation formula:

  • Retrieves user input values using getElementById
  • Converts annual interest rate to monthly rate
  • Applies EMI formula: P × r × (1 + r)^n / ((1 + r)^n - 1)
  • Displays result formatted to 2 decimal places
function calculate() {
    // Get input values
    let amount = parseFloat(document.getElementById('amount').value);
    let rate = parseFloat(document.getElementById('rate').value);
    let time = parseInt(document.getElementById('time').value);
    
    // Validate inputs
    if (!amount || !rate || !time) {
        document.getElementById("output").innerHTML = "Please fill all fields";
        return;
    }
    
    // Convert annual rate to monthly rate
    let monthlyRate = rate / 12 / 100;
    
    // Calculate EMI using formula: P × r × (1 + r)^n / ((1 + r)^n - 1)
    let emi = (amount * monthlyRate * Math.pow(1 + monthlyRate, time)) / 
              (Math.pow(1 + monthlyRate, time) - 1);
    
    // Display result
    let result = emi.toFixed(2);
    document.getElementById("output").innerHTML = result;
}

// Test the calculation
console.log("EMI Calculator loaded successfully");
EMI Calculator loaded successfully

Complete Example

Here's the complete working loan EMI calculator:

<!DOCTYPE html>
<html>
<head>
    <title>Loan EMI Calculator</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 0;
            padding: 20px;
            min-height: 100vh;
        }
        
        .container {
            width: 400px;
            max-width: 90%;
            background-color: #031926;
            display: flex;
            flex-direction: column;
            justify-content: flex-start;
            align-items: stretch;
            padding: 30px;
            border-radius: 15px;
            color: white;
            margin: 50px auto;
            box-shadow: 0 10px 25px rgba(0,0,0,0.3);
        }

        h1 {
            color: #4CAF50;
            text-align: center;
            margin-bottom: 25px;
            font-family: Arial, sans-serif;
        }

        p {
            color: white;
            font-size: 18px;
            font-family: Arial, sans-serif;
            margin-bottom: 15px;
            font-weight: 500;
        }

        input[type="number"] {
            width: 100%;
            height: 40px;
            background-color: white;
            font-size: 16px;
            color: #031926;
            padding: 10px;
            margin-top: 5px;
            border: none;
            border-radius: 5px;
            box-sizing: border-box;
        }

        #btn {
            background-color: #4CAF50;
            color: white;
            padding: 12px 25px;
            font-size: 16px;
            font-weight: bold;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            margin: 20px auto;
            transition: background-color 0.3s;
        }

        #btn:hover {
            background-color: #45a049;
        }

        #output {
            font-weight: bold;
            color: #4CAF50;
            font-size: 20px;
        }

        ::placeholder {
            color: #666;
            opacity: 0.8;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Loan EMI Calculator</h1>
        <p>Loan Amount: $
            <input type="number" id="amount" placeholder="Enter loan amount">
        </p>
        <p>Interest Rate: %
            <input type="number" step="0.1" id="rate" placeholder="Enter annual rate">
        </p>
        <p>Time Period (Months):
            <input type="number" id="time" placeholder="Enter time in months">
        </p>
        <input type="button" value="Calculate EMI" id="btn" onclick="calculate()">
        <p>Monthly EMI: $
            <span id="output">0.00</span>
        </p>
    </div>

    <script>
        function calculate() {
            // Get input values
            let amount = parseFloat(document.getElementById('amount').value);
            let rate = parseFloat(document.getElementById('rate').value);
            let time = parseInt(document.getElementById('time').value);
            
            // Validate inputs
            if (!amount || !rate || !time) {
                document.getElementById("output").innerHTML = "Please fill all fields";
                return;
            }
            
            // Convert annual rate to monthly rate
            let monthlyRate = rate / 12 / 100;
            
            // Calculate EMI using formula: P × r × (1 + r)^n / ((1 + r)^n - 1)
            let emi = (amount * monthlyRate * Math.pow(1 + monthlyRate, time)) / 
                      (Math.pow(1 + monthlyRate, time) - 1);
            
            // Display result
            let result = emi.toFixed(2);
            document.getElementById("output").innerHTML = result;
        }
    </script>
</body>
</html>

Key Features

  • Input Validation: Ensures all fields are filled before calculation
  • Responsive Design: Works on desktop and mobile devices
  • User-Friendly: Clear labels and placeholder text
  • Accurate Formula: Uses the standard EMI calculation method

Conclusion

This loan EMI calculator provides an interactive tool for users to quickly calculate their monthly loan payments. The combination of HTML structure, CSS styling, and JavaScript functionality creates a professional and user-friendly calculator that can be easily integrated into any website.

Updated on: 2026-03-15T23:19:00+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements