Home >Web Front-end >CSS Tutorial >Build a Weight Converter Website

Build a Weight Converter Website

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2024-08-18 06:32:32955browse

Build a Weight Converter Website

Introduction

Hello, fellow developers! I'm thrilled to introduce my latest project: a Weight Converter. This project is a simple yet practical tool that helps users convert weight from pounds to kilograms. It's an excellent way to practice JavaScript, particularly in handling user inputs, calculations, and DOM manipulation. Whether you're new to JavaScript or looking for a fun, quick project, this weight converter is perfect for you.

Project Overview

The Weight Converter is a web application that allows users to input a weight in pounds and get the equivalent weight in kilograms. This project showcases how to work with form inputs, perform calculations in JavaScript, and dynamically update the webpage content based on user input.

Features

  • User Input Handling: Accepts user input in pounds and converts it to kilograms.
  • Validation: Displays error messages for invalid inputs (e.g., non-numeric or negative values).
  • Real-time Calculation: Updates the conversion result immediately as the user types.
  • Responsive Design: The layout is designed to be responsive, providing a consistent experience across devices.

Technologies Used

  • HTML: Structures the weight converter interface.
  • CSS: Styles the converter, including responsive layout and error handling.
  • JavaScript: Manages user input validation, weight conversion, and DOM updates.

Project Structure

Here's a quick look at the project structure:

Weight-Converter/
├── index.html
├── styles.css
└── script.js
  • index.html: Contains the HTML structure of the weight converter.
  • styles.css: Includes CSS styles for the layout and responsive design.
  • script.js: Handles the logic for weight conversion and input validation.

Installation

To get started with the project, follow these steps:

  1. Clone the repository:

    git clone https://github.com/abhishekgurjar-in/Weight-Converter.git
    
  2. Open the project directory:

    cd Weight-Converter
    
  3. Run the project:

    • Open the index.html file in a web browser to use the weight converter.

Usage

  1. Open the website in a web browser.
  2. Enter a weight in pounds into the input field.
  3. View the converted weight in kilograms displayed on the page. If the input is invalid, an error message will appear.

Code Explanation

HTML

The index.html file sets up the structure of the weight converter, including the input field and the result display. Here’s a snippet:

8b05045a5be5764f313ed5b9168a17e6
49099650ebdc5f3125501fa170048923
  93f0f5c25f18dab9d176bd4f6de5d30e
    7c8d9f814bcad6a1d7abe4eda5f773e5
    26faf3d1af674280d03ba217d87e9421
    b2386ffb911b14667cb8f0f91ea547a7Weight Converter6e916e0f7d1e588d4f442bf645aedb2f
    d8b7823904473d155afe66ded7e78f93
    5de102113aede4703971b3b780c58efb2cacc6d41bbb37262a98f745aa00fbf0
  9c3bca370b5104690d9ef395f2c5f8d1
  6c04bd5ca3fcae76e30b72ad730ca86d
    4883ec0eb33c31828b7c767c806e14c7
      4a249f0d628e2318394fd9b75b4636b1Weight Converter473f0a7621bec819994bb5020d29372a
      1adfcf403e84ee950da4e51ec7bcc58ePounds8c1ecd4bb896b2264e0711597d40766c
      2bd967a228ad4dd6dd46bd4135780e50
      e388a4556c0f65e1904146cc1a846beeYour weight in kg is: 1d8fe9c7fe60ca65fea7ef18556b479354bdf357c58b8a65c66d7c19c8e4d11494b3e26ee717c64999d7867364b1b4a3
      fcc3d86900ee5e367762a3d4e4f4c3eb94b3e26ee717c64999d7867364b1b4a3
    16b28748ea4df4d9c2150843fecfba68
    ffd6ba4147bda351239915f463e46e38
      e388a4556c0f65e1904146cc1a846beeMade with ❤️ by Abhishek Gurjar94b3e26ee717c64999d7867364b1b4a3
    16b28748ea4df4d9c2150843fecfba68
  36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

CSS

The styles.css file styles the weight converter, including the input field, error messages, and layout. Here are some key styles:

body {
    margin: 0;
    background-color: black;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    font-family: 'Courier New', Courier, monospace;
    color: white;
  }

  .container {
    background: rgba(141, 133, 133, 0.632);
    padding: 20px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
    border-radius: 10px;
    width: 85%;
    max-width: 450px;
    margin-bottom: 200px;
  }

  .input-container {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 18px;
    font-weight: 700;
  }

  .input {
    padding: 10px;
    width: 70%;
    background: rgb(255, 253, 253);
    border-color: rgba(0, 0, 0, 0.5);
    font-size: 18px;
    border-radius: 10px;
    color: rgb(0, 0, 0);
    outline: none;
  }

  .error {
    color: red;
  }

  #result {
    color: rgb(0, 255, 0);
  }

JavaScript

The script.js file manages the conversion logic, input validation, and updates the DOM. Here’s a snippet:

const inputEl = document.getElementById("input");
const errorEl = document.getElementById("error");
const resultEl = document.getElementById("result");
let errorTime;
let resultTime;

function updateResults() {
  if (inputEl.value 2d1b547ac038f694c7ef60a480b9dfa0 {
      errorEl.innerText = "";
      inputEl.value = "";
    }, 2000);
  } else {
    resultEl.innerText = (+inputEl.value / 2.2).toFixed(2);
    clearTimeout(resultTime);
    resultTime = setTimeout(() => {
      resultEl.innerText = "";
      inputEl.value = "";
    }, 10000);
  }
}

inputEl.addEventListener("input", updateResults);

Live Demo

You can check out the live demo of the Weight Converter here.

Conclusion

Building this Weight Converter was an enjoyable project that allowed me to explore JavaScript’s potential in handling real-time input validation and calculations. I hope this project inspires you to create similar tools that make everyday tasks easier. Feel free to explore, customize, and improve the code to suit your needs. Happy coding!

Credits

This project was developed as a practical application of JavaScript for user input handling and DOM manipulation.

Author

  • Abhishek Gurjar
    • GitHub Profile

The above is the detailed content of Build a Weight Converter Website. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn