Sitemap
JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

Building a Temperature Converter App using HTML, CSS, and JavaScript

5 min readJan 24, 2023

--

screenshot of the temperature converter app

Prerequisites

Getting Started

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!--LINK CSS FILE-->
<link rel="stylesheet" href="style.css" />
<title>Temperature Converter App</title>
</head>
<body>
<div class="container">
<div class="temperature-converter">
<h1 class="title">Temperature Converter</h1>
<div class="result">
<span class="result-heading">Result (Celsius)</span>
<h2 class="celsius-value" id="celsius"></h2>
</div>
<div class="degree-type">
<div class="degree-field">
<label for="degree">Degrees</label>
<input type="number" name="degree" id="degree" />
</div>
<div class="temp-field">
<label for="temp-type">Type</label>
<select name="temperatures" id="temp-type" autocomplete="on">
<option id="fahrenheit" value="fahrenheit">Fahrenheit</option>
<option id="kelvin" value="kelvin">Kelvin</option>
</select>
</div>
</div>
<button id="convert-btn">Convert</button>
</div>
</div>
<!--LINK JAVASCRIPT FILE-->
<script src="./script.js"></script>
</body>
</html>
<link rel="stylesheet" href="style.css" />
<script src="./script.js"></script>

CSS

* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
background-color: #2321af;
color: #222;
font-size: 1rem;
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
}
/*style.css */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}

body {
background-color: #2321af;
color: #222;
font-size: 1rem;
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
}
.title {
color: #222;
font-size: 1.5rem;
text-align: center;
}
.temperature-converter {
background: #ecebeb;
border-radius: 5px;
position: relative;
display: flex;
flex-direction: column;
justify-content: center;
padding: 30px 25px 20px;
min-width: 320px;
width: 100%;
height: auto;
}
.result {
text-align: center;
margin: 20px 0;
}
.result-heading {
color: #555;
font-size: 1rem;
font-weight: 600;
}
.celsius-value {
border-bottom: 2px solid #ccc;
padding: 10px;
}
label {
color: #555;
font-size: 0.8rem;
margin-bottom: 5px;
}
input,
select {
background: transparent;
border: none;
outline: none;
border-bottom: 2px solid #ccc;
padding: 8px;
margin-bottom: 15px;
}
input:focus {
border: 1px solid #4c49f3;
}
.degree-type {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 0;
}
.degree-field {
display: flex;
flex-direction: column;
width: 46%;
}
option {
background-color: #3c39e7;
color: #f6f6f6;
}
#convert-btn {
background: linear-gradient(to bottom, #3633f3, #120fb9);
border: none;
outline: none;
border-radius: 5px;
color: #f6f6f6;
cursor: pointer;
font-weight: bold;
letter-spacing: 1px;
text-transform: uppercase;
margin-top: 10px;
padding: 12px 80px;
transition: all 0.3s ease-in;
}
#convert-btn:hover {
background: linear-gradient(to bottom, #4a47e7, #322fda);
}

JavaScript

let inputElems = document.querySelectorAll("input[type='number']");
const bill = document.querySelector("#bill");
const tip = document.querySelector("#tip-percentage");
const total = document.querySelector("#totalOutput");
const numberOfPersons = document.querySelector("#persons");
const calculateBtn = document.querySelector(".calculate-btn");
function convertToCelsius() {
{
//code to be executed
}
convertBtn.addEventListener("click", (e) => {
e.preventDefault();
convertToCelsius();
});
window.addEventListener("load", () => {
degree.value = "";
celsiusElem.innerHTML = "";
});
let inputValue = degree.value;
if (tempType.value === "fahrenheit") {
const FahrenheitToCelsius = (inputValue - 32) * (5 / 9);
celsiusElem.innerHTML = `${FahrenheitToCelsius.toFixed(3)} &deg;c`;
} else if (tempType.value === "kelvin") {
const KelvinToCelsius = inputValue - 273.15;
celsiusElem.innerHTML = `${KelvinToCelsius} &deg;c`;
}
// script.js
const celsiusElem = document.querySelector("#celsius");
const degree = document.querySelector("#degree");
const convertBtn = document.querySelector("#convert-btn");
const tempType = document.querySelector("#temp-type");

window.addEventListener("load", () => {
degree.value = "";
celsiusElem.innerHTML = "";
});

convertBtn.addEventListener("click", (e) => {
e.preventDefault();
convertToCelsius();
});

function convertToCelsius() {
let inputValue = degree.value;

if (tempType.value === "fahrenheit") {
const FahrenheitToCelsius = (inputValue - 32) * (5 / 9);
celsiusElem.innerHTML = `${FahrenheitToCelsius.toFixed(3)} &deg;c`;
} else if (tempType.value === "kelvin") {
const KelvinToCelsius = inputValue - 273.15;
celsiusElem.innerHTML = `${KelvinToCelsius.toFixed(3)} &deg;c`;
}
}

Conclusion

In Plain English 🚀

--

--

JavaScript in Plain English
JavaScript in Plain English

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Franklin Ohaegbulam
Franklin Ohaegbulam

No responses yet

Write a response