Monday, September 09, 2024
Temperature Unit App is a temperature conversion tool that allows users to convert temperature values between various temperature units. Using a simple and intuitive interface, the app supports conversion between various temperature scales, including:
With these features, your Temperature Unit app will have complete functionality and an attractive design, making it not only useful but also fun to use. Using Bootstrap, TailwindCSS, and JavaScript, this app provides a responsive and interactive experience for users when converting temperatures between different units.
By building this Temperature Unit project using Bootstrap, TailwindCSS, and JavaScript, you can gain the following skills:
Here are the complete steps to create a Temperature Unit application using Bootstrap 5, TailwindCSS, and JavaScript:
Create a new HTML file, for example index.html, and create the basic HTML structure using the following code:
<!DOCTYPE html>
<!-- Coding By CodingIndo - https://codingindo.vercel.app/ -->
<html lang="en">
<head>
<!-- Meta tag for character encoding -->
<meta charset="UTF-8">
<!-- Meta tag for setting the viewport to be responsive on mobile devices -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Page title -->
<title>Temperature Unit</title>
<!-- Link to local CSS file -->
<link rel="stylesheet" href="style.css">
<!-- Link to Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Link to TailwindCSS -->
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body>
<!-- Temperature converter card -->
<div class="converter-card">
<!-- Converter title -->
<h2 class="text-center text-2xl font-bold mb-4">Temperature Unit</h2>
<!-- Form converter -->
<form id="converterForm" class="space-y-4">
<!-- Temperature input -->
<div class="form-group">
<label for="temperature" class="form-label">Enter Temperature:</label>
<input type="number" id="temperature" class="form-control" placeholder="Enter value" required>
</div>
<!-- Original unit selection -->
<div class="form-group">
<label for="fromUnit" class="form-label">From Unit:</label>
<select id="fromUnit" class="form-select">
<option value="C">Celsius (°C)</option>
<option value="F">Fahrenheit (°F)</option>
<option value="K">Kelvin (K)</option>
<option value="R">Rankine (°R)</option>
<option value="Re">Reaumur (°Ré)</option>
<option value="De">Delisle (°De)</option>
<option value="N">Newton (°N)</option>
<option value="Ro">Rømer (°Rø)</option>
<option value="T">Triple Point of Water (K)</option>
</select>
</div>
<!-- Destination unit selection -->
<div class="form-group">
<label for="toUnit" class="form-label">To Unit:</label>
<select id="toUnit" class="form-select">
<option value="C">Celsius (°C)</option>
<option value="F">Fahrenheit (°F)</option>
<option value="K">Kelvin (K)</option>
<option value="R">Rankine (°R)</option>
<option value="Re">Reaumur (°Ré)</option>
<option value="De">Delisle (°De)</option>
<option value="N">Newton (°N)</option>
<option value="Ro">Rømer (°Rø)</option>
<option value="T">Triple Point of Water (K)</option>
</select>
</div>
<!-- Convert button -->
<div class="form-group text-center">
<button type="submit" class="btn btn-primary btn-lg w-100">Convert</button>
</div>
</form>
<!-- Conversion result -->
<div id="result" class="mt-4 text-center"></div>
</div>
<!-- Link to local JavaScript file -->
<script src="script.js"></script>
<!-- Link to Bootstrap JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Number | Line of Code | Explanation |
---|
Add CSS styling to give your app a nice look using a combination of Bootstrap and TailwindCSS:
/* Sets styles for the body element */
body {
background: linear-gradient(135deg, #667eea, #764ba2);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: 'Poppins', sans-serif;
}
/* Sets styles for elements with the converter-card class */
.converter-card {
background-color: white;
border-radius: 1rem;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
padding: 2rem;
max-width: 400px;
width: 100%;
}
/* Sets the style for the h2 element inside the converter-card */
.converter-card h2 {
color: #764ba2;
}
/* Sets the style for elements with class btn-primary */
.btn-primary {
background-color: #667eea;
border-color: #667eea;
}
/* Sets the style for elements with class btn-primary on hover */
.btn-primary:hover {
background-color: #764ba2;
border-color: #764ba2;
}
/* Sets the style for the element with the id result */
#result {
color: #764ba2;
font-weight: bold;
font-size: 1.25rem;
}
Number | Line of Code | Explanation |
---|
Added JavaScript script to handle temperature conversion logic:
// Add event listener for form submit
document.getElementById('converterForm').addEventListener('submit', function(e) {
e.preventDefault();
// Get the temperature value from the input
const temp = parseFloat(document.getElementById('temperature').value);
// Get the original unit value from the input
const fromUnit = document.getElementById('fromUnit').value;
// Get the target unit value from the input
const toUnit = document.getElementById('toUnit').value;
let result;
// Check if the temperature value is valid
if (isNaN(temp)) {
result = "Silakan masukkan suhu yang valid.";
} else {
// Convert temperature
result = convertTemperature(temp, fromUnit, toUnit);
}
// Display the conversion result
document.getElementById('result').textContent = result;
});
// Function to convert temperature
function convertTemperature(temp, from, to) {
let kelvin;
// Converts the original units to Kelvin
switch (from) {
case 'C': kelvin = temp + 273.15; break;
case 'F': kelvin = (temp + 459.67) * 5/9; break;
case 'K': kelvin = temp; break;
case 'R': kelvin = temp * 5/9; break;
case 'Re': kelvin = temp * 1.25 + 273.15; break;
case 'De': kelvin = 373.15 - temp * 2/3; break;
case 'N': kelvin = temp * 100/33 + 273.15; break;
case 'Ro': kelvin = (temp - 7.5) * 40/21 + 273.15; break;
case 'T': kelvin = 273.16; break;
default: return "Unit input tidak valid.";
}
let finalTemp;
// Convert from Kelvin to target units
switch (to) {
case 'C': finalTemp = kelvin - 273.15; break;
case 'F': finalTemp = kelvin * 9/5 - 459.67; break;
case 'K': finalTemp = kelvin; break;
case 'R': finalTemp = kelvin * 9/5; break;
case 'Re': finalTemp = (kelvin - 273.15) * 0.8; break;
case 'De': finalTemp = (373.15 - kelvin) * 3/2; break;
case 'N': finalTemp = (kelvin - 273.15) * 33/100; break;
case 'Ro': finalTemp = (kelvin - 273.15) * 21/40 + 7.5; break;
case 'T': finalTemp = 273.16; break;
default: return "Unit output tidak valid.";
}
// Returns the conversion result in string format
return `${temp} ${from} = ${finalTemp.toFixed(2)} ${to}`;
}
Number | Line of Code | Explanation |
---|
Open the index.html file in a browser. Now, the temperature converter application can be used to convert between various temperature units with an attractive display.
By keeping these notes in mind, you will be able to create better, easier-to-use applications that can meet the needs of a wide range of users. Happy developing!
The Temperature Unit app offers a practical and efficient solution for converting temperatures between various scales, both common ones like Celsius and Fahrenheit and more specialized ones like Kelvin, Rankine, and others. With a simple interface and powered by modern technologies like Bootstrap, TailwindCSS, and JavaScript, the app ensures a comfortable and responsive user experience across devices.
As a useful conversion tool, Temperature Unit not only makes everyday calculations easier but also acts as an educational medium in understanding the differences in temperature scales. With accurate results and an attractive appearance, this application is ready to help anyone, whether students, professionals, or hobbyists, in completing their temperature conversion needs easily.
Thus, this application is the right choice for those of you who want a reliable temperature conversion tool. We hope this tool can facilitate your various activities in understanding and processing temperature data, both for learning and work purposes. Happy using and hopefully useful!
If you are having trouble creating the Temperature Unit app using Bootstrap, TailwindCSS, and JavaScript, you can download the source code files for this project for free by clicking the “Download” button. You can also view a live demo of this app by clicking the “View Live” button to see how the design and functionality of the app work in practice.
By accessing the source code files and the live demo, you can easily understand the code implementation and customize the project according to your needs. Feel free to explore the code, experiment with styles, and modify features to improve your skills in HTML, CSS, and JavaScript.
This is a great opportunity to learn more about integrating CSS frameworks with JavaScript and to deepen your understanding of responsive web design. Happy learning and experimenting!
Thursday, October 17, 2024
Sunday, September 01, 2024
Saturday, August 31, 2024
Sunday, September 08, 2024
LEAVE A REPLY