ADVERTISEMENT

Weight Converter using HTML, CSS & JavaScript

Telegram Group Join Now

ADVERTISEMENT

Weight Converter using HTML, CSS & JavaScript

Introduction

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Weight Converter which will generate a weight convertor with three input fields kilograms, ounces & pounds. The Kilograms input will the user input and the other to two fields will update the corresponding values. This project will is good for beginners and help them to build their front-end development skills. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Weight Converter Project.

ADVERTISEMENT

ADVERTISEMENT

Weight Converter Javascript

Project Description

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Weight Converter Project.

ADVERTISEMENT

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Weight Converter Project.

ADVERTISEMENT

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Weight Converter Project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Weight Converter

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

ADVERTISEMENT

<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Weight Converter</title>
    <!-- Google Fonts -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <h2>Weight Converter</h2>
      <div class="input-wrapper">
        <label for="kg">Kilogram:</label>
        <input type="number" id="kg" value="10" />
      </div>
      <div class="input-wrapper">
        <label for="lb">Pounds:</label>
        <input type="number" id="lb" />
      </div>
      <div class="input-wrapper">
        <label for="oz">Ounces:</label>
        <input type="number" id="oz" />
      </div>
    </div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>

Create a Quiz App Using HTML ,CSS & JavaScript

CSS Code for Weight Converter

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Weight Converter project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now we have created the structure using HTML and styled the webpage using CSS its time to add the functionality using JavaScript in this project.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  height: 100vh;
  background: linear-gradient(#25ff5f, #0cd943 50%, #ffffff 50%);
}
.container {
  background-color: #ffffff;
  width: 90vw;
  font-size: 16px;
  max-width: 28em;
  padding: 3em;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  box-shadow: 0 0 3.2em rgba(1, 3, 29, 0.2);
  border-radius: 0.6em;
}
h2 {
  font-size: 2em;
  text-align: center;
  margin-bottom: 1.5em;
}
.input-wrapper input {
  display: block;
  width: 100%;
  font-size: 1.35em;
  padding: 0.4em;
  border: 1px solid #a0a0a0;
  border-radius: 0.2em;
}
.input-wrapper label {
  display: block;
  font-size: 1.1em;
  font-weight: 600;
  margin-bottom: 0.25em;
}
.input-wrapper:not(:last-child) {
  margin-bottom: 1.2em;
}
@media screen and (max-width: 28em) {
  .container {
    font-size: 14px;
  }
}

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

ADVERTISEMENT

JavaScript Code for Weight Converter

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project Weight Converter using HTML, CSS & JavaScript (Source Code).

ADVERTISEMENT

let kgRef = document.getElementById("kg");
let lbRef = document.getElementById("lb");
let ozRef = document.getElementById("oz");

let convertFromKg = () => {
  let kg = kgRef.value;
  //toFixed(2) limits the decimals to 2 digits.
  lbRef.value = (kg * 2.205).toFixed(2);
  ozRef.value = (kg * 35.274).toFixed(2);
};

let convertFromLb = () => {
  let lb = lbRef.value;
  kgRef.value = (lb / 2.205).toFixed(2);
  ozRef.value = (lb * 16).toFixed(2);
};

let convertFromOz = () => {
  let oz = ozRef.value;
  kgRef.value = (oz / 35.274).toFixed(2);
  lbRef.value = (oz / 16).toFixed(2);
};

kgRef.addEventListener("input", convertFromKg);
lbRef.addEventListener("input", convertFromLb);
ozRef.addEventListener("input", convertFromOz);
window.addEventListener("load", convertFromKg);

Output

Live Preview of Weight Converter using HTML, CSS & JavaScript

See the Pen
Weight
by Harsh Sawant (@harshh9)
on CodePen.

We have Successfully created our Weight Converter 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 code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

ADVERTISEMENT

Code By – @harshh9

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Telegram Group Join Now

Share on:

Leave a Comment

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT