Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Weather - Zachary Parsons</title>
  <meta name="description" content="A weather app created by Zachary Parsons">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link type="text/css" rel="stylesheet" href="../style.css">
</head>

<body>
  <div class="entry">
    <!-- <h1 class="header" id="title">Zed Weather App</h1> -->
    <p>A simple weather app - search for your city now 👇</p>
    <div id="search">
        <input type="text" placeholder="City Name" id="searchInput" name="city">
        <input type="submit" class="searchControl" id="searchButton" value="Search 🔍">
    </div>
  </div>
  <div id="weather-card">
    <img id="weather-image" src="https://source.unsplash.com/collection/1525589">
    <div id="weather-description">
      <div id="city-icon">
        <h2 id="city-header"></h2>
        <img id="icon-image" src="">
      </div>
      <hr>
      <div id="weather-main">
        <div id="weather-info">
          <h3 id="weather-header"></h3>
        </div>
        <div>
          <p id="temp"></p>
        </div>
      </div>
      <div id="bottom-details">
        <div id="wind-speed">

        </div>
        <div id="humidity">

        </div>
      </div>
    </div>
  </div>
  <div class="outro">
    <button id="theme">Change Theme 🚀</button>
    <p>Created by <a target="_blank" href="https://zacharyparsons.co.uk">Zachary</a></p>
  </div>
  <script src="../script.js" async defer></script>
</body>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Catamaran:wght@700&family=Roboto&display=swap');

:root {
    --bg: #abd1c6;
    --h: #001e1d;
    --p: #0f3433;
    --card-bg: #004643;
    --card-h: #fffffe;
    --card-p: #abd1c6;

    --button: #faae2b;
    --button-text: #00473e;

    --link: #f9bc60;
    --link-bg: #00473ee3;
}

[data-theme="dark"] {
    --bg: #004643;
    --h: #fffffe;
    --p: #abd1c6;
    --card-bg: #e8e4e6;
    --card-h: #001e1d;
    --card-p: #0f3433;
}

body {
    background-color: var(--bg);
    font-family: 'Roboto', sans-serif;
    padding: 20px;
}

h1, h2, h3 {
    color: var(--h);
    font-family: 'Catamaran', sans-serif;
}

p {
    color: var(--p);
}

.entry, .outro {
    text-align: center;
}

.entry {
    padding: 0 20px 20px;
}

.outro {
    padding: 20px 20px 0;
}

#weather-card h2, #weather-card h3 {
    color: var(--card-h);
    text-transform: capitalize;
}

#weather-card p {
    color: var(--card-p);
}

a {
    color: var(--link);
    text-decoration: underline;
    background: var(--link-bg);
    padding: .2rem .4rem .3rem .4rem;
}

/* Media queries for size of card */

@media (min-width:320px)  { #weather-card { width: 90% } }
@media (min-width:481px)  { #weather-card { width: 85% } }
@media (min-width:641px)  { #weather-card { width: 80% } }
@media (min-width:961px)  { #weather-card { width: 75% } }
@media (min-width:1025px) { #weather-card { width: 70% } }
@media (min-width:1281px) { #weather-card { width: 60% } }

#weather-card {
    background-color: var(--card-bg);
    margin: auto;
    /* width: 60%; */
    max-width: 400px;
    box-shadow: 0 6px 10px 0 rgba(0,0,0,0.3);
    transition: 0.3s;
    border-radius: 2%;
}

#weather-card:hover {
    box-shadow: 0 10px 18px 0 rgba(0,0,0,0.5);
}

#weather-description {
    padding: 2px 16px;
}

#weather-image {
    object-fit: cover;
    width: 100%;
    max-height: 325px;
    border-radius: 2% 2% 0 0;
}

#city-icon {
    display: grid;
    grid-template-columns: 65% 35%;
    align-items: center;
}

#weather-info {
    line-height: 0;
}

#theme {
    margin: 5px;
}

hr {
    border: 0;
    border-top: 1px solid rgba(0,0,0,0.1);
}

#bottom-details {
    display: grid;
    margin-bottom: 10px;
    grid-template-areas: 
        "a b"
        "c c";
}

#wind-speed {
    grid-area: a;
}
#humidity {
    grid-area: b;
}
#bottom-details a {
    grid-area: c;
    text-align: center;
}

button, .searchControl {
    background-color: var(--button);
    color: var(--button-text);
    border: none;
    padding: 10px 15px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    cursor: pointer;
}
button:hover, .searchControl:hover {
    box-shadow: 0 8px 12px 0 rgba(0,0,0,0.24), 0 15px 40px 0 rgba(0,0,0,0.19);
}

#searchInput {
    color: var(--button-text);
    background-color: #e8e4e6;
    border: none;
    padding: 10px 15px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
}
              
            
!

JS

              
                let image = document.getElementById("weather-image");
let weatherIcon = document.getElementById("icon-image");
let iconURL = "https://openweathermap.org/img/wn/";
let cityHeader = document.getElementById("city-header");
let weatherInfo = document.getElementById("weather-info");
let weatherHeader = document.getElementById("weather-header");
let temp = document.getElementById("temp");
let windSpeed = document.getElementById("wind-speed");
let humidity = document.getElementById("humidity");
let bottomDetails = document.getElementById("bottom-details");
let appId = '1584d7453071c7ed7e97c6ffd5f2a688';
const currentTheme = localStorage.getItem("theme")
  ? localStorage.getItem("theme")
  : null;
if (currentTheme) {
  document.documentElement.setAttribute("data-theme", currentTheme);
}

document.getElementById("theme").addEventListener("click", () => {
  toggle();
});

document.getElementById("searchButton").focus();

document.getElementById("searchButton").addEventListener("click", () => {
  let city = document.getElementById("searchInput").value;
  if (city) {
    searchWeather(city);
  }
});

// Functions 👇

function toggle() {
  if (
    document.documentElement.getAttribute("data-theme") == null ||
    document.documentElement.getAttribute("data-theme") == "light"
  ) {
    document.documentElement.setAttribute("data-theme", "dark");
    localStorage.setItem("theme", "dark");
  } else {
    document.documentElement.setAttribute("data-theme", "light");
    localStorage.setItem("theme", "light");
  }
}

function searchWeather(cityName) {
  fetch(
    `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${appId}&units=metric`
  )
    .then((result) => {
      return result.json();
    })
    .then((result) => {
      cityHeader.innerHTML = result.name;
      temp.innerHTML = `Temp: ${Math.round(result.main.temp)}°C`;
      weatherHeader.innerHTML = result.weather[0].description;
      let moreInfo = document.createElement("a");
      let moreInfoText = document.createTextNode("More info...");
      moreInfo.appendChild(moreInfoText);
      moreInfo.target = "_blank";
      moreInfo.href = `https://openweathermap.org/find?q=${result.name}`;
      bottomDetails.appendChild(moreInfo);
      windSpeed.innerHTML = `<p>Wind Speed: ${result.wind.speed}M/S</p>`;
      humidity.innerHTML = `<p>Humidity: ${result.main.humidity}%</p>`;
      initWeather(result);
    });
}

function initWeather(res) {
  console.log(res);
  switch (res.weather[0].main) {
    case "Clear":
      fetch("https://source.unsplash.com/featured/?clear,sky")
      .then(response => {
        image.src = response.url;
      })
      weatherIcon.src = iconURL + "01d@2x.png";
      break;
    case "Clouds":
      fetch("https://source.unsplash.com/featured/?clouds")
      .then(response => {
        image.src = response.url;
      });
      weatherIcon.src = iconURL + "02d@2x.png";
      break;
    case "Rain":
    case "Drizzle":
    case "Mist":
      fetch("https://source.unsplash.com/featured/?rain")
      .then(response => {
        image.src = response.url;
      });
      weatherIcon.src = iconURL + "10d@2x.png";
      break;
    case "Thunderstorm":
      fetch("https://source.unsplash.com/featured/?storm")
      .then(response => {
        image.src = response.url;
      });
      weatherIcon.src = iconURL + "11d@2x.png";
      break;
    case "Snow":
      fetch("https://source.unsplash.com/featured/?snow")
      .then(response => {
        image.src = response.url;
      });
      weatherIcon.src = iconURL + "13d@2x.png";
      break;

    default:
      break;
  }
}

              
            
!
999px

Console