Have you ever wondered how weather apps work behind the scenes? In this blog, I’ll walk you through how I built a simple yet powerful weather app using HTML, CSS, JavaScript, and the OpenWeather API.
๐งพ Prerequisites
- Basic knowledge of HTML, CSS, and JavaScript
 - Code editor (like VS Code)
 - OpenWeather API key (free to generate)
 - Basic understanding of REST APIs
 
๐ Project Structure
weather-app/ ├── index.html ├── style.css ├── script.js
✨ Features
- ๐ Search weather by city name
 - ๐ก️ Display temperature, humidity, wind speed
 - ๐ค️ Dynamic icon and weather description
 - ๐ Background image changes based on weather
 - ๐ 5-day forecast using OpenWeather's Forecast API
 - ๐ Reset button to clear UI
 - ๐ฑ Responsive layout with Bootstrap
 
๐ How It Works
The app connects to two endpoints from the OpenWeather API:
/weather– Gets current weather data/forecast– Gets 5-day forecast in 3-hour intervals
When a user searches for a city:
- The current weather data is fetched and displayed
 - The 5-day forecast is filtered to one result per day (at noon)
 - The icon and background are updated dynamically
 
๐ผ️ Dynamic Background
Based on the weather type (like Clear, Rain, Clouds), the app changes the background image using Unsplash:
const backgroundImages = {
  Clear: "url('https://source.unsplash.com/1600x900/?sunny')",
  Rain: "url('https://source.unsplash.com/1600x900/?rain')",
  Clouds: "url('https://source.unsplash.com/1600x900/?cloudy')"
};
document.body.style.backgroundImage = backgroundImages[weatherMain] || defaultBg;
  
  ๐ Forecast Filter
To limit the forecast to one result per day (12:00pm):
const daily = forecastList.filter(item =>
  item.dt_txt.includes("12:00:00")
).slice(0, 5);
  
  ✅ What I Learned
- How to consume public APIs with Axios
 - Dynamically updating UI with JavaScript
 - Handling errors and form validation
 - Filtering and slicing forecast data from JSON
 - Creating responsive designs with Bootstrap
 
๐ Live Demo and Code
๐ฌ What’s Next?
- ๐ Add dark mode toggle
 - ๐ Use Geolocation API for local weather
 - ๐ฒ Turn it into a PWA for offline access
 
๐ Final Words
This project helped me solidify key frontend concepts and API handling. If you're learning web development, building something real like this is one of the best ways to learn. Try adding your own features and make it your own!
Comments
Post a Comment