,

Creating a Weather App Employing HTML, CSS, and JavaScript

Posted by






Build A Weather App Using HTML, CSS and JavaScript

Build A Weather App Using HTML, CSS and JavaScript

With the power of HTML, CSS, and JavaScript, you can create a simple yet effective weather app that can display current weather conditions for any location. In this tutorial, we will walk you through the process of building your very own weather app.

Step 1: Set up your project structure

Start by creating a new folder for your project and inside it, create three files: index.html, styles.css, and app.js. Your project structure should look like this:

  /* Project Folder */
  -- index.html
  -- styles.css
  -- app.js
  

Step 2: Create the HTML structure

Open the index.html file and start by creating a basic HTML structure. Add elements for the input field where the user can search for a location, a button to submit the search, and a container to display the weather information.

index.html

  <!DOCTYPE html>
  <html>
  <head>
    <title>Weather App</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
  </head>
  <body>

    <div class="search-container">
      <input type="text" id="search-input" placeholder="Enter location">
      <button id="search-btn">Search</button>
    </div>

    <div id="weather-info">
      <!-- Weather information will be displayed here -->
    </div>

    <script src="app.js"></script>
  </body>
  </html>
  

Step 3: Style your weather app

Next, open the styles.css file and add some basic styling to make your weather app look presentable. You can add a background image, change the font styles, and adjust the layout to your liking.

styles.css

  body {
    background: url('background.jpg') no-repeat center center fixed;
    background-size: cover;
    font-family: Arial, sans-serif;
  }

  .search-container {
    text-align: center;
    margin-top: 50px;
  }

  input[type="text"] {
    padding: 10px;
    width: 200px;
    margin-right: 10px;
  }

  #search-btn {
    padding: 10px;
  }

  #weather-info {
    text-align: center;
    margin-top: 20px;
  }
  

Step 4: Add functionality using JavaScript

Finally, open the app.js file and write the JavaScript code to make your weather app functional. You will need to make an API request to a weather service provider, retrieve the weather data, and display it on the web page.

app.js

  document.getElementById('search-btn').addEventListener('click', function() {
    var location = document.getElementById('search-input').value;

    fetch('https://api.openweathermap.org/data/2.5/weather?q=' + location + '&appid=YOUR_API_KEY')
      .then(response => response.json())
      .then(data => {
        document.getElementById('weather-info').innerHTML = `
          

${data.name}

Temperature: ${data.main.temp} °C

Weather: ${data.weather[0].description}

`; }); });

Step 5: Get your API key

In the JavaScript code, you will need to replace ‘YOUR_API_KEY’ with an actual API key from a weather service provider. You can sign up for a free account and obtain an API key from providers like OpenWeatherMap or WeatherAPI.

Step 6: Test your weather app

Once you have completed the above steps, open the index.html file in a web browser and test your weather app. Enter a location in the input field and click the search button to see the current weather conditions for that location displayed on the page.

Congratulations, you have successfully built a weather app using HTML, CSS, and JavaScript!


0 0 votes
Article Rating
11 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Luciano Diaz
7 months ago

Hi ¿how it's going? when you change temperture celcius to fahrenheit the time changes too ¿why is the reason?

Comspon photography
7 months ago

Great video love it ❤

Noname Nosurname
7 months ago

miami 85 degrees???

Akhil Mishra
7 months ago

Hey man, This was really very helpful in terms of getting to know about the nitty-gritty details. Also explanation of HTML CSS AND JS was on point.
Just a small request when I searched for Delhi India it wasn't giving the correct output, I believe the time zone properties needs to be changed.
if you could help in this, that will be great ! 🙂 : )

Prateek
7 months ago

7:55

Lauren
7 months ago

This was a lot of fun to do and learn from, thank you!! ✨

Sosal Basnet
7 months ago

How can you put a weathericon by using api?

Kalu, Vincent Chigozie
7 months ago

Nice video bruh, i am going to try this.

SANU
7 months ago

The code is not working in local

A Kaz
7 months ago

Hi Ope, thanks so much for your tutorial.

Negvanyan
7 months ago

Very informative. Appreciate the video!!!