Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                <html lang="en">
<head>
</head>
<body>
<div id="main-wrapper">
  <div id="location-wrapper">
    <h1 id="city"></h1>
    <h3 id="country"></h3>
  </div>
  <div id="data-wrapper">
    <div>
      <span id="temperature"></span>
      <span id="degreeSymbol"></span>
      <span id="icon"></span>
    </div>
    <div id="font-size-wrapper">
    <div id="conditions"></div>
    <div id="winds"></div>
    <div id="pressure"></div>
    <div id="humidity"></div>
    <div id="sunrise"></div>
    <div id="sunset"></div>
    </div> <!-- end font-size-wrapper -->
    <div>
      <button id="unit" type="button" class="btn btn-primary">Use Imperial Units</button>
    </div>
  </div> <!-- end data-wrapper -->
</div> <!-- end main-wrapper -->
</body>
</html>
              
            
!

CSS

              
                html {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
}

*, *:before, *:after {
    box-sizing: inherit;
}

body {
    width: 100%;
    height: 100%;
    font-size: 14px;
    background-color: #b0c4de !important;
    display: flex;
}

#main-wrapper {
    margin: 2px auto;
    width: 990px;
    height: 742px;
    border: 3px solid lightsteelblue;
    border-radius: 16px;
}

#location-wrapper {
    width: 250px;
    height: 100px;
    border: 10px solid dodgerblue;
    border-radius: 16px;
    margin: 60px 30px 20px 30px;
    background-color: dodgerblue;
    opacity: 0.85;
}

#location-wrapper h1 {
    margin: 6px 0 0 0;
    font-size: 1.8em;
}

#location-wrapper h3 {
    margin: 0;
}

#data-wrapper {
    width: 250px;
    height: 380px;
    border: 10px solid dodgerblue;
    border-radius: 16px;
    margin: 10px 30px;
    background-color: dodgerblue;
    opacity: 0.85;
    font-size: 1.1em;
}

#temperature {
    font-size: 4.0em;
    margin-left: 10px;
}

#degreeSymbol {
    position: relative;
    font-size: 2.0em;
    top: -24px;
}

#icons {
    height: 80px;
    width: 80px;
    position: relative;
    top: -22px;
}

#conditions {
    font-size: 2em;
}

#unit {
    margin-top: 30px;
    margin-left: 40px;
}

@media all and (max-width: 990px) {

    body {
        font-size: 12px;
    }

    #main-wrapper {
        width: 700px;
        height: 525px;
    }

    #location-wrapper {
        width: 177px;
        height: 71px;
        margin: 30px 30px 20px 30px;
    }

    #location-wrapper h1 {
        font-size: 1.4em;
        margin-top: 0;
    }

    #location-wrapper h3 {
        font-size: 1.2em;
    }

    #data-wrapper {
        width: 177px;
        height: 269px;
        font-size: 1.1em;
    }

    #temperature {
        font-size: 3.0em;
    }

    #degreeSymbol {
        position: relative;
        font-size: 1.6em;
        top: -14px;
    }

    #icons {
        height: 50px;
        width: 50px;
        position: relative;
        top: -12px;
    }

    #font-size-wrapper {
        font-size: 0.8em;
    }

    #unit {
        margin-top: 10px;
        margin-left: 8px;
    }

}

@media all and (max-width: 700px) {

    #main-wrapper {
        min-width: 496px;
        height: 372px;
    }

    #location-wrapper {
        margin: 10px 20px 10px 16px;
    }

    #data-wrapper {
        margin: 0 20px 20px 16px;
    }
}
              
            
!

JS

              
                /* The Local Weather App  first discovers website user's location, then queries the openweathermap.org
 * API for current weather at that location, and then displays the results. */

(function () {
    'use strict';
    var displayCity = document.getElementById("city");                  // get DOM references.
    var displayCountry = document.getElementById("country");
    var displayTemperature = document.getElementById("temperature");
    var displayDegreeSymbol = document.getElementById("degreeSymbol");
    var displayHumidity = document.getElementById("humidity");
    var displayConditions = document.getElementById("conditions");
    var displayWinds = document.getElementById("winds");
    var displayPressure = document.getElementById("pressure");
    var displaySunrise = document.getElementById("sunrise");
    var displaySunset = document.getElementById("sunset");
    var button = document.getElementById("unit");
    var backgroundPicture = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/368633/clear.jpg"; // set default background picture.
    var cityName = "";
    var regionName = "";
    var countryName = "";
    var locationString = "";
    var latitude = "";
    var longitude = "";
    var countryUnits = "metric";                                        // set default measurement system to metric.
    var temperature = "";
    var windSpeed = "";
    var windDirection = "";
    var humidity = "";
    var pressure = "";
    var pressureSymbol = "kPa";                                         // set default pressure units.
    var sunrise = "";
    var sunset = "";
    var currentWeather = "";
    var tempSymbol = "C";                                               // set default temperature units.
    var windSymbol = 'km/h';                                            // set default wind speed units.
    var iconURL = "";

    /* function locationByIP() sets user's location information based upon the user's IP address
     * using the webservice at https://ipinfo.io/ */
    function locationByIP() {
        var locationRequest = new XMLHttpRequest();
        locationRequest.onreadystatechange = function () {
            if (locationRequest.readyState === 4 && locationRequest.status === 200) {  // ready state 'complete.'
                var locationObj = JSON.parse(locationRequest.responseText);            // parse JSON response.
                cityName = locationObj.city;                            // extract user's city, region, country.
                regionName = locationObj.region;
                countryName = locationObj.country;
                locationString = locationObj.loc.split(',');           // split loc ( loc": "45.0834,-64.4988" )
                latitude = Number(locationString[0]);
                longitude = Number(locationString[1]);
                setCountryUnits();                                      // set units of measure, imperial or metric.
                getWeatherData();                                       // get user's local weather.
            }
        };
        locationRequest.open("GET", 'https://ipinfo.io/json', true);     // true sets asynchronous mode.
        locationRequest.send();                                         // send the information request to website.
    }

    /* function setCountryUnits() determines if user's country uses imperial or metric units of measure. */
    function setCountryUnits() {
        if (countryName === 'US' || countryName === 'LY') {             // in year 2016 only USA and Libya not metric.
            countryUnits = 'imperial';
        }
    }

    /* function getWeatherData() queries openweathermap.org for local weather at user's longitude and latitude. */
    function getWeatherData() {
        var url = 'https://cors.5apps.com/?uri=http://api.openweathermap.org/data/2.5/weather?lat=' + latitude + '&lon=' +  // build query url.
            longitude + '&APPID=608f2325a0ac32149fd498a3ebf24641' +
            '&units=' + countryUnits + '&preventCache=' + new Date();   // Date prevents caching old data.
        var weatherRequest = new XMLHttpRequest();
        weatherRequest.onreadystatechange = function () {
            if (weatherRequest.readyState === 4 && weatherRequest.status === 200) {
                var obj = JSON.parse(weatherRequest.responseText);
                processResponse(obj);                     // call processResponse() passing in local weather data.
            }
        };
        weatherRequest.open("GET", url, true);                          // true sets asynchronous mode.
        weatherRequest.send();                                          // send the information request to website.
    }

    /* function processResponse() receives local weather data and updates weather data variables. */
    function processResponse(obj) {
        temperature = Math.round(obj.main.temp);
        if (countryUnits === 'metric') {                                // checks if metric country.
            windSpeed = Math.round(obj.wind.speed * 18 / 5);            // convert meter/sec to km/hour. (metric)
            pressure = Math.round(obj.main.pressure) / 10;              // convert to kPa from hPa. (metric)
        }
        else {                                                          // else use imperial units.
            windSpeed = Math.round(obj.wind.speed);
            pressure = Math.round(obj.main.pressure);
        }
        windDirection = degreeToCardinal(obj.wind.deg);      // convert from degrees to cardinal wind direction.
        currentWeather = obj.weather[0].description;
        humidity = obj.main.humidity;
        var sunriseDateObj = unixTimeToLocal(obj.sys.sunrise);          // convert to user's time zone, pretty format.
        sunrise = sunriseDateObj.toLocaleTimeString();
        var sunsetDateObj = unixTimeToLocal(obj.sys.sunset);            // convert to user's time zone, pretty format.
        sunset = sunsetDateObj.toLocaleTimeString();
        iconURL = 'https://cors.5apps.com/?uri=http://openweathermap.org/img/w/' + obj.weather[0].icon + '.png';  // fetch correct weather icon.
        weatherPicture();                         // set appropriate background picture to local weather conditions.
        displayRefresh();                         // update webpage with new data.
    }

    /* function displayRefresh() updates the DOM using latest weather data. */
    function displayRefresh() {
        displayCity.innerHTML = cityName;
        displayCountry.innerHTML = regionName + ", " + countryName;
        displayTemperature.innerHTML = temperature;
        displayDegreeSymbol.innerHTML = " &deg;" + tempSymbol;
        displayConditions.innerHTML = currentWeather;
        displayWinds.innerHTML = "Winds " + windDirection + " " + windSpeed + " " + windSymbol;
        displayPressure.innerHTML = "Barometric Pressure: " + pressure + " " + pressureSymbol;
        displayHumidity.innerHTML = "Humidity: " + humidity + "%";
        displaySunrise.innerHTML = "Sunrise at " + sunrise;
        displaySunset.innerHTML = "Sunset at " + sunset;
        var newElement = document.createElement('img');                 // new DOM element for weather icon.
        newElement.src = iconURL;
        newElement.setAttribute("id", "icons");
        document.getElementById("icon").appendChild(newElement);
        var image = backgroundPicture;
        var referenceMainWrapper = document.getElementById("main-wrapper");
        referenceMainWrapper.style.backgroundImage = 'url(' + image + ')';
        referenceMainWrapper.style.backgroundSize = "100% auto";
    }

    /* function toggleUnits() allows user to flip between imperial and metric units of measure. */
    function toggleUnits() {
        if (countryUnits === 'metric') {                           // check if currently set to imperial or metric.
            tempSymbol = 'F';
            windSymbol = 'miles/hour';
            countryUnits = 'imperial';
            pressureSymbol = 'mb';
            button.innerHTML = 'Use Metric Units';
            temperature = Math.round((temperature * 9 / 5) + 32);       // convert temperature to 'fahrenheit'.
            displayTemperature.innerHTML = temperature;
            displayDegreeSymbol.innerHTML = " &deg;" + tempSymbol;
            windSpeed = Math.round(windSpeed / 1.609344);               // convert wind speed to 'miles/hr'.
            displayWinds.innerHTML = "Winds " + windDirection + " " + windSpeed + " " + windSymbol;
            pressure = pressure * 10;                                   // convert pressure to 'mb'.
            displayPressure.innerHTML = "Barometric Pressure: " + pressure + " " + pressureSymbol;
        }
        else {
            tempSymbol = 'C';
            countryUnits = 'metric';
            windSymbol = 'km/hour';
            pressureSymbol = 'kPa';
            button.innerHTML = 'Use Imperial Units';
            temperature = Math.round((temperature - 32) * 5 / 9);       // convert temperature to 'celsius'.
            displayTemperature.innerHTML = temperature;
            displayDegreeSymbol.innerHTML = " &deg;" + tempSymbol;
            windSpeed = Math.round(windSpeed * 1.609344);               // convert wind speed to 'Km/h'.
            displayWinds.innerHTML = "Winds " + windDirection + " " + windSpeed + " " + windSymbol;
            pressure = pressure / 10;                                   // convert pressure to'KPa'.
            displayPressure.innerHTML = "Barometric Pressure: " + pressure + " " + pressureSymbol;
        }
    }

    /* function unixTimeToLocal() is passed a date set in unix time, and returns a Date object in the user's
     * local time. */
    function unixTimeToLocal(unix) {
        var local = new Date(0);
        local.setUTCSeconds(unix);
        return local;
    }

    /* function degreeToCardinal() is passed a direction in degrees, and returns a cardinal direction. */
    function degreeToCardinal(degree) {
        if (degree > 337.5 && degree < 22.5) {
            return "N";
        } else if (degree > 22.5 && degree < 67.5) {
            return "NE";
        } else if (degree > 67.5 && degree < 112.5) {
            return "E";
        } else if (degree > 112.5 && degree < 157.5) {
            return "SE";
        } else if (degree > 157.5 && degree < 202.5) {
            return "S";
        } else if (degree > 202.5 && degree < 247.5) {
            return "SW";
        } else if (degree > 247.5 && degree < 292.5) {
            return "W";
        } else if (degree > 292.5 && degree < 337.5) {
            return "NW";
        }
    }

    /* function weatherPicture() sets the background picture to match the current local weather conditions. */
    function weatherPicture() {
        switch (true) {
            case /\bclear\b/i.test(currentWeather):                     // match uses regular expression.
                backgroundPicture = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/368633/clear.jpg';
                break;
            case /\bovercast\b/i.test(currentWeather):
                backgroundPicture = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/368633/overcast.jpg';
                break;
            case /\bclouds\b/i.test(currentWeather):
                backgroundPicture = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/368633/mostly_cloudy.jpg';
                break;
            case /\brain\b/i.test(currentWeather):
                backgroundPicture = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/368633/rainy.jpg';
                break;
            case /\bdrizzle\b/i.test(currentWeather):
                backgroundPicture = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/368633/rainy.jpg';
                break;
            case /\bthunderstorm\b/i.test(currentWeather):
                backgroundPicture = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/368633/thunderstorm.jpg';
                break;
            case /\bsnow\b/i.test(currentWeather):
                backgroundPicture = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/368633/snow.jpg';
                break;
            case /\bmist\b/i.test(currentWeather):
                backgroundPicture = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/368633/mist.jpg';
                break;
            case /\bfog\b/i.test(currentWeather):
                backgroundPicture = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/368633/mist.jpg';
                break;
        }
    }

    document.getElementById("unit").onclick = function () {             // add event listener to 'change units' button.
        toggleUnits();                                                  // on button click toggle units of measurement.
    };
    locationByIP();                                                     // main entry point for program execution.
})();

              
            
!
999px

Console