⚡ Javascript Beginner Featured

Temperature Converter Tool

A clean, responsive temperature converter that converts between Celsius, Fahrenheit, and Kelvin.

48 views 5 mins Admin 1 month ago

About This Script

Build a beautiful temperature converter with real-time conversion as you type. This tool demonstrates modern HTML/CSS/JS practices for creating calculators and converters.

Features:

  • Real-time conversion
  • Three temperature scales
  • Responsive design
  • Input validation
  • Clean UI with animations
Source Code
<!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>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        
        .container {
            background: white;
            padding: 40px;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            max-width: 500px;
            width: 100%;
        }
        
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
            font-size: 2em;
        }
        
        .converter-box {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 600;
        }
        
        input {
            width: 100%;
            padding: 15px;
            border: 2px solid #ddd;
            border-radius: 10px;
            font-size: 18px;
            transition: all 0.3s ease;
        }
        
        input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 10px rgba(102, 126, 234, 0.3);
        }
        
        .result {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 10px;
            margin-top: 20px;
        }
        
        .result-item {
            display: flex;
            justify-content: space-between;
            padding: 10px 0;
            border-bottom: 1px solid #e0e0e0;
        }
        
        .result-item:last-child {
            border-bottom: none;
        }
        
        .result-label {
            font-weight: 600;
            color: #555;
        }
        
        .result-value {
            color: #667eea;
            font-weight: bold;
            font-size: 1.1em;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🌡️ Temperature Converter</h1>
        
        <div class="converter-box">
            <label for="celsius">Celsius (°C)</label>
            <input type="number" id="celsius" placeholder="Enter temperature in Celsius">
        </div>
        
        <div class="result">
            <div class="result-item">
                <span class="result-label">Fahrenheit:</span>
                <span class="result-value" id="fahrenheit">-</span>
            </div>
            <div class="result-item">
                <span class="result-label">Kelvin:</span>
                <span class="result-value" id="kelvin">-</span>
            </div>
        </div>
    </div>

    <script>
        const celsiusInput = document.getElementById('celsius');
        const fahrenheitDisplay = document.getElementById('fahrenheit');
        const kelvinDisplay = document.getElementById('kelvin');
        
        function convertTemperature() {
            const celsius = parseFloat(celsiusInput.value);
            
            if (isNaN(celsius)) {
                fahrenheitDisplay.textContent = '-';
                kelvinDisplay.textContent = '-';
                return;
            }
            
            // Convert to Fahrenheit: F = C × 9/5 + 32
            const fahrenheit = (celsius * 9/5) + 32;
            
            // Convert to Kelvin: K = C + 273.15
            const kelvin = celsius + 273.15;
            
            fahrenheitDisplay.textContent = fahrenheit.toFixed(2) + ' °F';
            kelvinDisplay.textContent = kelvin.toFixed(2) + ' K';
        }
        
        celsiusInput.addEventListener('input', convertTemperature);
    </script>
</body>
</html>
Requirements
None - just copy and paste!
Quick Actions
More in Tool Builders