Simple HTML Weather App Code Example

Answered using GPT-4o mini by AI Answer Generator

Question:

Make me html code for weather

Answer:

Certainly! Below is a simple HTML code snippet that creates a basic weather display. This example uses HTML, CSS, and a bit of JavaScript to fetch weather data from a public API (OpenWeatherMap). You'll need to sign up for an API key from OpenWeatherMap to use this code.

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Weather App</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .weather-container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            text-align: center;
        }
        input {
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        button {
            padding: 10px 15px;
            border: none;
            background-color: #007BFF;
            color: white;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .weather-info {
            margin-top: 20px;
        }
    </style>
</head>
<body>

<div class="weather-container">
    <h1>Weather App</h1>
    <input type="text" id="city" placeholder="Enter city name">
    <button onclick="getWeather()">Get Weather</button>
    <div class="weather-info" id="weather-info"></div>
</div>

<script>
    async function getWeather() {
        const city = document.getElementById('city').value;
        const apiKey = 'YOUR_API_KEY'; // Replace with your OpenWeatherMap API key
        const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;

        try {
            const response = await fetch(url);
            if (!response.ok) {
                throw new Error('City not found');
            }
            const data = await response.json();
            const weatherInfo = `
                <h2>${data.name}, ${data.sys.country}</h2>
                <p>Temperature: ${data.main.temp} °C</p>
                <p>Weather: ${data.weather[0].description}</p>
                <p>Humidity: ${data.main.humidity}%</p>
                <p>Wind Speed: ${data.wind.speed} m/s</p>
            `;
            document.getElementById('weather-info').innerHTML = weatherInfo;
        } catch (error) {
            document.getElementById('weather-info').innerHTML = `<p>${error.message}</p>`;
        }
    }
</script>

</body>
</html>

Instructions

  1. Get an API Key: Sign up at OpenWeatherMap and get your API key.
  2. Replace the API Key: In the JavaScript code, replace 'YOUR_API_KEY' with your actual API key.
  3. Run the Code: Save the code in an HTML file (e.g., weather.html) and open it in a web browser.

How It Works

  • The user enters a city name and clicks the "Get Weather" button.
  • The JavaScript function getWeather() fetches weather data from the OpenWeatherMap API.
  • The weather information is displayed on the page, including temperature, weather description, humidity, and wind speed.

Feel free to customize the styles and layout as per your needs!