Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Building a Basic Weather App using HTML, CSS, and JavaScript

Building a Basic Weather App using HTML, CSS, and JavaScript

Become More Then A coder | Learn & Start Coding Now.

Building a Basic Weather App using HTML, CSS, and JavaScript

**Introduction**

Weather apps provide real-time weather information to users, making them a practical and popular tool. Creating a basic weather app can introduce you to the world of web development and APIs. In this article, we'll guide you through building a simple weather app using HTML, CSS, and JavaScript. By the end, you'll have a functional weather app that fetches and displays weather data for a specific location.

**Getting Started: Setting Up the Weather App Structure**

Begin by creating a new HTML document in your preferred text editor. Let's structure our weather app.

```html
<!DOCTYPE html>
<html>
<head>
    <title>Weather App</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
    <div class="container">
        <h1>Weather App</h1>
        <input type="text" id="cityInput" placeholder="Enter city name">
        <button onclick="getWeather()">Get Weather</button>
        <div id="weatherInfo"></div>
    </div>
    <script src="script.js"></script>
</body>
</html>
```

**Creating the CSS Styling**

Create a `styles.css` file in the same directory as your HTML file for styling.

```css
body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f5f5f5;
}

.container {
    text-align: center;
    padding: 2em;
    background-color: white;
    max-width: 400px;
    margin: 0 auto;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
}

input {
    padding: 10px;
    width: 70%;
    margin-bottom: 10px;
}

button {
    background-color: #333;
    color: white;
    border: none;
    padding: 10px 20px;
    cursor: pointer;
}

#weatherInfo {
    margin-top: 20px;
    font-size: 18px;
}
```

**Adding Functionality with JavaScript**

Create a `script.js` file in the same directory as your HTML file to add JavaScript functionality.

```javascript
function getWeather() {
    var cityInput = document.getElementById("cityInput").value;
    var weatherInfo = document.getElementById("weatherInfo");

    // Replace 'YOUR_API_KEY' with your actual API key
    var apiKey = 'YOUR_API_KEY';
    var apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${cityInput}&appid=${apiKey}&units=metric`;

    fetch(apiUrl)
        .then(response => response.json())
        .then(data => {
            var temperature = data.main.temp;
            var description = data.weather[0].description;
            weatherInfo.innerHTML = `Temperature: ${temperature}°C<br>Conditions: ${description}`;
        })
        .catch(error => {
            weatherInfo.innerHTML = "City not found.";
        });
}
```

**Breaking Down the Code**

1. `<!DOCTYPE html>`: The document type declaration for HTML5.
2. `<html>`: The root element of the HTML document.
3. `<head>`: Contains meta-information, including the link to the external CSS file.
4. `<title>`: Sets the title displayed in the browser tab.
5. `<link>`: Links the external `styles.css` file for styling.
6. `<body>`: The main content of the web page.
7. `.container`: Styles the app container.
8. `input`, `button`: Styles for input fields and the "Get Weather" button.
9. `#weatherInfo`: Styles the weather information display.
10. `<script>`: Includes the JavaScript file.
11. `function getWeather()`: Fetches weather data from the API and displays it.

**Conclusion**

Building a basic weather app using HTML, CSS, and JavaScript is a great way to learn about integrating APIs into your web applications. As you continue to explore web development, you can enhance your weather app with features like dynamic backgrounds based on weather conditions, location-based weather, and more interactive user interfaces.

---

Remember to replace `'YOUR_API_KEY'` with an actual API key from a weather API service, such as OpenWeatherMap. You can copy and paste the HTML, CSS, and JavaScript code into separate files in a text editor to see the result for yourself. This simple weather app will help you understand how to retrieve and display data from an API using JavaScript.

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.