Create a Length Converter using HTML CSS and JavaScript Last Updated : 19 Jul, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to create a length converter using HTML, CSS, and JavaScript. The Length Converter is an intuitive web-based application that eliminates the complexities associated with manual conversions. Its user-friendly interface allows users to input a numerical value and select the units they want to convert from and to. With a diverse range of measurement options such as centimeters, inches, feet, and meters, the Length Converter provides versatility and caters to a wide array of conversion scenarios. Prerequisites: HTML, CSS, JavaScript. Approach: Let's explore the approach taken to build this project: Create a container div that holds all the elements, including the input field, unit selection dropdowns, and result display area.Now apply some styles to elements such as the container, heading, input field, dropdowns, button, and result area. The styling aims to create a clean and user-friendly interface.Add JavaScript to handle the conversion logic and interaction with the elements. In the below example, we have created a convert() function, which is triggered when the conversion is required. It retrieves the input value and the selected units from the dropdowns, and calculates the conversion based on the selected units. The calculated result is then displayed in the result area.Example: The below example shows a web application that converts the different types of length conversions. HTML <!DOCTYPE html> <html> <head> <title>Length Converter</title> <style> .container { margin: 20px auto; width: 300px; background-color: #f1f1f1; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } h1 { text-align: center; margin-bottom: 20px; } input[type="number"] { width: 100%; padding: 10px; margin-bottom: 10px; box-sizing: border-box; } select { width: 100%; padding: 10px; margin-bottom: 10px; box-sizing: border-box; } p { text-align: center; margin-top: 20px; font-weight: bold; font-size: 20px; } </style> </head> <body> <div class="container"> <h1>Length Converter</h1> <input type="number" id="inputValue" placeholder="Enter value" oninput="convert()"> <select id="fromUnit" onchange="convert()"> <option value="cm">Centimeter (cm)</option> <option value="inch">Inch (in)</option> <option value="feet">Feet (ft)</option> <option value="meter">Meter (m)</option> <option value="yard">Yard (yd)</option> <option value="mile">Mile (mi)</option> <option value="kilometer">Kilometer (km)</option> </select> <select id="toUnit" onchange="convert()"> <option value="cm">Centimeter (cm)</option> <option value="inch">Inch (in)</option> <option value="feet">Feet (ft)</option> <option value="meter">Meter (m)</option> <option value="yard">Yard (yd)</option> <option value="mile">Mile (mi)</option> <option value="kilometer">Kilometer (km)</option> </select> <p id="result"></p> </div> <script> function convert() { // Retrieve input values let inputValue = document.getElementById("inputValue").value; let fromUnit = document.getElementById("fromUnit").value; let toUnit = document.getElementById("toUnit").value; // Convert the length based on the selected units let result; if (fromUnit === "cm" && toUnit === "inch") { result = inputValue / 2.54; } else if (fromUnit === "inch" && toUnit === "cm") { result = inputValue * 2.54; } else if (fromUnit === "cm" && toUnit === "feet") { result = inputValue / 30.48; } else if (fromUnit === "feet" && toUnit === "cm") { result = inputValue * 30.48; } else if (fromUnit === "cm" && toUnit === "meter") { result = inputValue / 100; } else if (fromUnit === "meter" && toUnit === "cm") { result = inputValue * 100; } else if (fromUnit === "inch" && toUnit === "feet") { result = inputValue / 12; } else if (fromUnit === "feet" && toUnit === "inch") { result = inputValue * 12; } else if (fromUnit === "inch" && toUnit === "meter") { result = inputValue * 0.0254; } else if (fromUnit === "meter" && toUnit === "inch") { result = inputValue / 0.0254; } else if (fromUnit === "feet" && toUnit === "meter") { result = inputValue * 0.3048; } else if (fromUnit === "meter" && toUnit === "feet") { result = inputValue / 0.3048; } else if (fromUnit === "cm" && toUnit === "yard") { result = inputValue / 91.44; } else if (fromUnit === "yard" && toUnit === "cm") { result = inputValue * 91.44; } else if (fromUnit === "cm" && toUnit === "mile") { result = inputValue / 160934.4; } else if (fromUnit === "mile" && toUnit === "cm") { result = inputValue * 160934.4; } else if (fromUnit === "cm" && toUnit === "kilometer") { result = inputValue / 100000; } else if (fromUnit === "kilometer" && toUnit === "cm") { result = inputValue * 100000; } else if (fromUnit === "inch" && toUnit === "yard") { result = inputValue / 36; } else if (fromUnit === "yard" && toUnit === "inch") { result = inputValue * 36; } else if (fromUnit === "inch" && toUnit === "mile") { result = inputValue / 63360; } else if (fromUnit === "mile" && toUnit === "inch") { result = inputValue * 63360; } else if (fromUnit === "inch" && toUnit === "kilometer") { result = inputValue * 0.0000254; } else if (fromUnit === "kilometer" && toUnit === "inch") { result = inputValue / 0.0000254; } else if (fromUnit === "feet" && toUnit === "yard") { result = inputValue / 3; } else if (fromUnit === "yard" && toUnit === "feet") { result = inputValue * 3; } else if (fromUnit === "feet" && toUnit === "mile") { result = inputValue / 5280; } else if (fromUnit === "mile" && toUnit === "feet") { result = inputValue * 5280; } else if (fromUnit === "feet" && toUnit === "kilometer") { result = inputValue * 0.0003048; } else if (fromUnit === "kilometer" && toUnit === "feet") { result = inputValue / 0.0003048; } else if (fromUnit === "meter" && toUnit === "yard") { result = inputValue * 1.09361; } else if (fromUnit === "yard" && toUnit === "meter") { result = inputValue / 1.09361; } else if (fromUnit === "meter" && toUnit === "mile") { result = inputValue / 1609.34; } else if (fromUnit === "mile" && toUnit === "meter") { result = inputValue * 1609.34; } else if (fromUnit === "meter" && toUnit === "kilometer") { result = inputValue / 1000; } else if (fromUnit === "kilometer" && toUnit === "meter") { result = inputValue * 1000; } else if (fromUnit === "yard" && toUnit === "mile") { result = inputValue / 1760; } else if (fromUnit === "mile" && toUnit === "yard") { result = inputValue * 1760; } else if (fromUnit === "yard" && toUnit === "kilometer") { result = inputValue / 1093.61; } else if (fromUnit === "kilometer" && toUnit === "yard") { result = inputValue * 1093.61; } else if (fromUnit === "mile" && toUnit === "kilometer") { result = inputValue * 1.60934; } else if (fromUnit === "kilometer" && toUnit === "mile") { result = inputValue / 1.60934; } else { result = inputValue; // No conversion needed } // Display the result document.getElementById("result").innerHTML = result.toFixed(4); } </script> </body> </html> Output: Length Converter Comment More infoAdvertise with us vishaldhaygude01 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Projects Similar Reads HTML Calculator HTML calculator is used for performing basic mathematical operations like Addition, subtraction, multiplication, and division.You can find the live preview below, try it: To design the basic calculator, we will use HTML , CSS , and JavaScript . HTML is used to design the basic structure of the calcu 3 min read JavaScript Calculator To build a simple calculator using JavaScript, we need to handle basic arithmetic operations such as addition, subtraction, multiplication, and division. JavaScript, along with HTML and CSS, is commonly used to create interactive web-based calculators.What We Are Going to CreateWe will build a simpl 7 min read JavaScript Scientific Calculator The HTML Scientific Calculator is a tool for performing advanced scientific calculations like finding exponents, logarithms, factorials, and more. This calculator comprises two sections: the input section, where the user types in their mathematical problem, and the output screen, which displays all 4 min read JavaScript Neumorphism Effect Calculator In this article, we will learn how to create a working calculator with the Neumorphism effect using HTML, CSS, and JavaScript. Basic mathematical operations such as addition, subtraction, multiplication, and division can be performed using this calculator. Approach: Neumorphism is a contemporary app 3 min read JavaScript Age Calculator In the Age Calculator, the user enters their date of birth using a date input field. The tool calculates and displays the exact age in years, months, and days from the current date (or a specified date). We'll design the layout using HTML and CSS, and add functionality using JavaScript. It will also 3 min read JavaScript Tip Calculator The tip is the money given as a gift for good service to the person who serves you in a restaurant. In this project, a simple tip calculator is made that takes the billing amount, type of service, and the number of persons as input. As per the three inputs, it generates a tip for the serving person. 4 min read JavaScript Geometry Calculator In this article, we will see how to design a Geometry Calculator using HTML, CSS, and JavaScript. A Geometry calculator is used to calculate the area and parameters of different shapes and figures, like circles, rectangles, squares, triangles, etc. This can be helpful in cases where one wants to cal 4 min read JavaScript Aspect Ratio Calculator In this article, we are going to implement an aspect ratio calculator. An aspect ratio calculator proves to be a useful tool for individuals seeking to determine the proportions of images or videos based on their width and height. Our aspect ratio calculator has a live preview option that enables us 5 min read JavaScript Binary Calculator HTML or HyperText Markup Language along with CSS (Cascading Stylesheet) and JavaScript can be used to develop interactive user applications that can perform certain functionalities. Similarly, a binary calculator can be developed using HTML, CSS, and JS altogether. Binary Calculator performs arithme 5 min read JavaScript Percentage Calculator The percentage calculator is useful for students, shopkeepers, and for solving basic mathematical problems related to percentages. In this article, we are going to learn, how to make a percentage calculator using HTML CSS, and JavaScriptFormula used:What is X percent of Y is given by the formula: X 3 min read JavaScript Profit and Loss Calculator In this article, we will create a profit & loss calculator using HTML, CSS & Javascript for adding the basic functionality along with adding the design and layout. Profit and Loss Calculator is basically used to calculate the amount or percentage received after selling a particular price or 3 min read JavaScript BMI Calculator A BMI (Body Mass Index) Calculator measures body fat based on weight and height, providing a numerical value to categorize individuals as underweight, normal weight, overweight, or obese. Itâs widely used to assess health risks and guide lifestyle or medical decisions.A BMI Calculator using JavaScri 3 min read JavaScript Temperature Calculator In this article, we will see Temperature Conversion between Celsius, Fahrenheit & Kelvin using HTML CSS & JavaScript. The Temperature is generally measured in terms of unit degree., i.e. in degrees centigrade, in degrees, Fahrenheit & Kelvin. Celsius is a standard unit of temperature on 3 min read JavaScript Student Grade Calculator A Student Grade Calculator is a tool used to compute students' grades based on their scores in various assessments, such as assignments, quizzes, exams, or projects. It helps standardize grading, ensures accuracy, and provides students with a clear understanding of their academic performance.Formula 4 min read JavaScript Loan Calculator The Loan Calculator can be used to calculate the monthly EMI of the loan by taking the total amount, months to repay, and the rate of interest.Formula Used:interest = (amount * (rate * 0.01))/months;total = ((amount/months) + interest);ApproachExtract values from HTML input elements (#amount, #rate, 2 min read JavaScript Interest Calculator Simple Interest is the term used to describe the rate at which money is borrowed or lent. Simple Interest Calculator serves as a practical tool for computing interest on loans or savings without compounding. It allows you to determine the simple interest on the principal amount, offering flexibility 3 min read JavaScript CGPA Calculator Nowadays, colleges use grades like A, B, C, D, and F, and assign credits to courses. In this calculator, you just need to input your subject name, grade, and credit. We'll also include options to update or delete information in case of mistakes. This way, you won't have to re-enter all details if yo 4 min read JavaScript Bartletts Test Calculator In this article, we are going to implement Bartlett's test calculator in JavaScript. Bartlett's test is a statistical test used to determine whether the variances of multiple groups are homogeneous and plays a crucial role in various fields of experimental research such as analysis of variance and h 3 min read JavaScript Unit Converter In this article, we will be developing an interactive and styled unit converter using HTML, CSS, and JavaScript.In this application, we have a select category option box in which different types of units are specified, like temperature, area, weight, length, and time. As per the user selection, the 7 min read JavaScript Length Converter In this article, we will learn how to create a length converter using HTML, CSS, and JavaScript. The Length Converter is an intuitive web-based application that eliminates the complexities associated with manual conversions. Its user-friendly interface allows users to input a numerical value and sel 6 min read Like