Here’s a Temperature Converter built using HTML, CSS, and JavaScript. This simple app allows users to convert temperatures between Celsius (°C), Fahrenheit (°F), and Kelvin (K).
Features:
Convert temperature between Celsius, Fahrenheit, and Kelvin.
Real-time conversion as the user types
Simple, clean, and responsive UI
No external libraries pure HTML, CSS, and JavaScript
Full Code (HTML, CSS, JavaScript)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Temperature Converter</title> <style> body { font-family: Arial, sans-serif; background-color: #f4f4f4; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } .container { background: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); text-align: center; width: 320px; } h2 { margin-bottom: 15px; } .input-group { margin-bottom: 10px; } label { font-weight: bold; display: block; } input { width: 100%; padding: 8px; margin-top: 5px; border: 1px solid #ccc; border-radius: 5px; font-size: 16px; text-align: center; } </style> </head> <body> <div class="container"> <h2>Temperature Converter</h2> <div class="input-group"> <label for="celsius">Celsius (°C)</label> <input type="number" id="celsius" oninput="convertFromCelsius(this.value)"> </div> <div class="input-group"> <label for="fahrenheit">Fahrenheit (°F)</label> <input type="number" id="fahrenheit" oninput="convertFromFahrenheit(this.value)"> </div> <div class="input-group"> <label for="kelvin">Kelvin (K)</label> <input type="number" id="kelvin" oninput="convertFromKelvin(this.value)"> </div> </div> <script> function convertFromCelsius(value) { let celsius = parseFloat(value); if (!isNaN(celsius)) { document.getElementById('fahrenheit').value = (celsius * 9/5 + 32).toFixed(2); document.getElementById('kelvin').value = (celsius + 273.15).toFixed(2); } else { clearFields(); } } function convertFromFahrenheit(value) { let fahrenheit = parseFloat(value); if (!isNaN(fahrenheit)) { document.getElementById('celsius').value = ((fahrenheit - 32) * 5/9).toFixed(2); document.getElementById('kelvin').value = ((fahrenheit - 32) * 5/9 + 273.15).toFixed(2); } else { clearFields(); } } function convertFromKelvin(value) { let kelvin = parseFloat(value); if (!isNaN(kelvin)) { document.getElementById('celsius').value = (kelvin - 273.15).toFixed(2); document.getElementById('fahrenheit').value = ((kelvin - 273.15) * 9/5 + 32).toFixed(2); } else { clearFields(); } } function clearFields() { document.getElementById('celsius').value = ""; document.getElementById('fahrenheit').value = ""; document.getElementById('kelvin').value = ""; } </script> </body> </html>
How It Works?
1. User enters a temperature in any field (Celsius, Fahrenheit, or Kelvin).
2. The script instantly converts and updates the other two fields.
3. Formulas Used for Conversion:
– Celsius to Fahrenheit: `(C × 9/5) + 32`
– Celsius to Kelvin: `C + 273.15`
– Fahrenheit to Celsius: `(F – 32) × 5/9`
– Fahrenheit to Kelvin: `((F – 32) × 5/9) + 273.15`
– Kelvin to Celsius: `K – 273.15`
– Kelvin to Fahrenheit: `((K – 273.15) × 9/5) + 32`
4. If the input is empty or invalid, the fields are cleared automatically.