Build your Celsius to Fahrenheit Converter using JavaScript

Build Celsius to Fahrenheit Temperature Converter

A Celsius to Fahrenheit converter is a tool or method used to convert temperatures from the Celsius scale (°C) to the Fahrenheit scale (°F). This conversion is essential for various applications, including meteorology, cooking, scientific research, and everyday temperature comparisons. The Celsius is a temperature scale proposed by the Swedish astronomer Anders Celsius in 1742.

Introduction

In this tutorial, we’ll guide you through the process of creating a simple and effective Celsius to Fahrenheit converter web application. Learn the basics of HTML, enhance the user interface with CSS, and implement the conversion logic using JavaScript. Elevate your web programming skills with this hands-on project.

How Celsius to Fahrenheit Converter works

The Fahrenheit and Celsius scales are two common units of temperature measurement. The Celsius scale, also known as the Centigrade scale, is commonly used internationally, while the Fahrenheit scale is more prevalent in the United States and a few other countries.

The formula for converting Celsius to Fahrenheit Converter is:

Celsius to Fahrenheit Formula

In this formula:

  • Fahrenheit represents the temperature in degrees Fahrenheit (°F).
  • Celsius represents the temperature in degrees Celsius (°C).
Celsius to Fahrenheit Temperature Converter

Prerequisites

Before diving into the code, make sure you have a basic understanding of HTML, CSS, and JavaScript. Ensure you have a text editor like Visual Studio Code or Sublime Text installed for a seamless coding experience.

Setting Up the HTML Structure

Create a new HTML file and set up the basic structure. Define input fields for Celsius, along with a button to trigger the conversion. Also, include a section to display the result.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>Celsius to Fahrenheit Converter</title>
</head>
<body>
    <div class="calculator">
        <h1>Celsius to Fahrenheit Converter</h1>

        <label for="celsius">Celsius:</label>
        <input type="number" id="celsius" placeholder="Enter temperature in Celsius">

        <br/>

        <button onclick="convertTemperature()">Convert</button>

        <hr />

        <h4 id="result">Result: </h4>
    </div>

    <script src="script.js"></script>
</body>
</html>

Styling with CSS

Enhance the visual appeal of your Celsius to Fahrenheit Converter by styling it with CSS. Create a new file named styles.css and add the following styles:

body {
    font-family: 'Arial', sans-serif;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    margin: 0;
    font-size: 17px;
    background: #fafafa;
}
h1 {
    margin: 5px 0 15px 0;
    text-align: center;
    text-transform: uppercase;
    font-size: 20px;
    letter-spacing: 1px
}
.calculator {
    width: 400px;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 5px;
    background: #fff;
}
label, input, button {
    margin: 10px 0;
}
input {
    display: block;
    width: calc(100% - 20px);
    padding: 10px;
}
button {
    background-color: #000;
    color: #fff;
    outline: 0;
    border: 0;
    padding: 10px 15px;
    cursor: pointer;
    font-size: 16px;
    margin: 0 auto; 
    border-radius: 5px;
    margin-bottom: 5px;
}
hr {
    margin: 15px 0;
}
h4 {
    margin-bottom: 0;
}

These are just the basic stylings, which you can enhance and modify as per your design requirements.

Implementing JavaScript Logic

Now, create a JavaScript file named script.js to implement the conversion logic. Add the following code:

function convertTemperature() {
    // Retrieve input values
    const celsiusInput = document.getElementById('celsius');

    // Validate input
    if (celsiusInput.value === '') {
        alert('Please enter a temperature value.');
        return;
    }

    // Celsius to Fahrenheit conversion
    if (celsiusInput.value !== '') {
        const celsiusValue = parseFloat(celsiusInput.value);
        const fahrenheitValue = (celsiusValue * 9/5) + 32;
        document.getElementById('result').innerText = `Result: ${celsiusValue}°C = ${fahrenheitValue.toFixed(2)}°F`;
    }
}

Code Explanation

The HTML sets up the structure, CSS styles the page, and JavaScript handles the conversion logic. The convertTemperature() function is triggered by the button click, fetching input values, and performing the conversion. The result will be displayed in the result div.

Conclusion

Congratulations! You’ve built a Celsius to Fahrenheit Converter using HTML, CSS, and JavaScript. Feel free to customize and expand this project to further enhance your web programming skills.

This tutorial focuses on the Celsius to Fahrenheit Converter. This will help you to grasp the HTML forms and the JavaScript concepts. Keep exploring and building to refine your skills and create impressive web applications.

Now, deploy your temperature converter and share your creation with the world!

Also, learn How to Build a Fahrenheit to Celsius Converter Using JavaScript.

Related Posts