12 Sept 2023

Integrating a weather widget into a website with HTML, CSS, and JavaScript

Weather widgets are a great addition to any website. They offer users the ability to check the weather quickly and easily, without having to leave the website. In this blog, we will walk you through how to integrate a weather widget into your website using HTML, CSS, and JavaScript.

Register for an API key

The first step in integrating a weather widget into your website is to register for an API key from a weather service provider such as OpenWeatherMap, AccuWeather, or Weather Underground. Once you have obtained an API key, you will be able to use it to access weather data.

Create an HTML file

The next step is to create an HTML file for your website. In this file, you will include the necessary elements to display the weather widget.

First, create a container div that will hold the weather widget. Give it an id of "weather-widget" to make it easier to style with CSS.

<div id="weather-widget">
  <!-- Weather widget will go here -->

Next, include the necessary JavaScript libraries in the head section of your HTML file. We will be using jQuery and the OpenWeatherMap API for this tutorial.

  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY"></script>

Replace "YOUR_API_KEY" with the API key you obtained in Step 1.

Add CSS styles

The next step is to add CSS styles to the weather widget. We will be using Flexbox to create a simple layout.

#weather-widget {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background-color: #f1f1f1;

#weather-icon {
  margin-right: 20px;

#weather-description {
  font-size: 24px;
  font-weight: bold;

#weather-temperature {
  font-size: 24px;
  margin-left: auto;

Create the JavaScript function

The final step is to create a JavaScript function that will retrieve the weather data from the API and display it in the weather widget.

function getWeather(city) {
    url: "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=YOUR_API_KEY&units=metric",
    type: "GET",
    dataType: "jsonp",
    success: function (data) {

      var icon = "https://openweathermap.org/img/w/" + data.weather[0].icon + ".png";
      var description = data.weather[0].description;
      var temperature = Math.round(data.main.temp) + "°C";

      $("#weather-icon").attr("src", icon);

This function takes a city parameter and sends a GET request to the OpenWeatherMap API to retrieve the weather data for that city. It then extracts the necessary data from the API response and updates the weather widget.

Display the weather widget

The final step is to call the getWeather function and pass in the city for which you want to display the weather. For example, if you want to display the weather for London, you would call the function like this


This will display the weather widget for London on your website. You can customize the widget further by modifying the CSS styles and adding additional elements such as a city selector.

In conclusion, integrating a weather widget into a website using HTML, CSS, and JavaScript is a straightforward process that can greatly enhance the user experience. With just a few lines of code, you can provide your website visitors with up-to-date weather information and keep them engaged with your site. By following the steps outlined in this tutorial, you can easily add a weather widget to your website and start enjoying the benefits of this useful feature.