cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <html lang="en-US">

<body>
    <div class="container">

        <header class="main-header">
            <h1>Local Weather</h1>
        </header>

        <div class="display">
            <div class="location">
                <h2 id="currentLocation">Getting location...</h2>
            </div>
            <div class="weather">
                <span id="currentIcon"></span>
                <h3 id="currentWeather">...</h3>
                <h2 id="currentTemp">...</h2>
            </div>
            <div class="forcast">
                <h4 id="highTemp">High:</h4>
                <h4 id="lowTemp">Low:</h4>
            </div>
            <div class="switch">
                <input type="checkbox" id="toggle">
                <label for="toggle">Toggle</label>
                <h6>Fahrenheit/Celsius</h6>
            </div>
        </div>

    </div>

</body>

</html>
            
          
!
            
              * {
    box-sizing: border-box;
}

html {
    background: #000 url("https://regularlyscheduledprogrammingblog.files.wordpress.com/2018/01/global-weather.jpg") no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

body {
    color: #fff;
    font-family: 'Roboto', sans-serif;
    text-shadow: 0em 0.2em 0.2em rgba(0, 0, 0, .9);
}

h1 {
    margin: 0.7em auto;
}

h6 {
    margin: 1em auto 2em auto;
}

.container {
    display: flex;
    flex-wrap: wrap;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}

.main-header,
.display {
    background: rgba(2, 30, 115, 0.75);
    text-align: center;
    border: 0.2rem #fff solid;
    border-radius: 8px;
		box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.main-header {
    font-weight: 700;
    margin-bottom: 2em;
    padding: 0 1.3em;
    width: 90%;
}

.display {
    padding: 0 1.3em;
    width: 60%;
}

#currentLocation {
    margin: 1em auto 0.5em auto;
    font-weight: 400;
}

#currentWeather {
    margin: 0.5em auto 1em auto;
}

#currentTemp {
    margin: 0.7em auto;
    font-weight: 700;
    font-size: 3em;
}

#currentWeather,
#highTemp,
#lowTemp,
h6 {
    font-weight: 300;
}

#highTemp,
#lowTemp {
    margin: 0.8em auto;
}

/* Button Styles */
input[type=checkbox] {
    height: 0;
    width: 0;
    visibility: hidden;
}

label {
    cursor: pointer;
    text-indent: -9999px;
    width: 50px;
    height: 25px;
    background: #b0b2b8;
    display: block;
    border-radius: 100px;
    position: relative;
    margin: auto;
}

label:after {
    content: '';
    position: absolute;
    top: 5px;
    left: 5px;
    width: 16px;
    height: 16px;
    background: #fff;
    border-radius: 90px;
    transition: 0.3s;
}

input:checked+label {
    background: #7d7e83;
}

input:checked+label:after {
    left: calc(100% - 5px);
    transform: translateX(-100%);
}

label:active:after {
    width: 30px;
}

/* Media Queries */
@media (min-width: 595px) {
    .main-header {
        margin-bottom: 1em;
        width: 70%;
        font-size: 1.4em;
    }
    .display {
        width: 50%;
    }
    #currentLocation {
        font-size: 2.2em;
        margin: 0.5em auto 0 auto;
    }
    #currentWeather {
        font-size: 1.6em;
        margin: 0 auto;
    }
    #currentTemp {
        margin: 0.5em auto;
        font-size: 4em;
    }
    #highTemp,
    #lowTemp {
        font-size: 1.3em;
    }
    h1 {
        margin: 0.5em auto;
    }
    h6 {
        font-size: 0.85em;
        margin-bottom: 1.3em;
    }
    img {
        height: 100px;
        width: 100px;
    }
    label {
        width: 100px;
        height: 50px;
    }
    label:after {
        width: 40px;
        height: 40px;
    }
    label:active:after {
        width: 65px;
    }
}

@media (min-width: 960px) {
    .main-header {
        margin-bottom: 1em;
        width: 60%;
        font-size: 1.8em;
    }
    .display {
        width: 40%;
    }
    #currentLocation {
        font-size: 2.6em;
        margin: 0.5em auto 0 auto;
    }
    #currentWeather {
        font-size: 1.9em;
    }
    #currentTemp {
        margin: 0.4em auto;
        font-size: 4.5em;
    }
    img {
        height: 105px;
        width: 105px;
    }
    h1 {
        margin: 0.3em auto;
    }
}

@media (min-width: 1290px) {
    .main-header {
        margin-bottom: 0.7em;
        max-width: 637px;
        /* 		width: 50%; */
        font-size: 1.9em;
    }
    .display {
        max-width: 445px;
    }
    #currentLocation {
        font-size: 3em;
        margin: 0.3em auto 0 auto;
    }
    #currentWeather {
        font-size: 2.2em;
    }
    #currentTemp {
        margin: 0.3em auto;
        font-size: 4.9em;
    }
    #highTemp,
    #lowTemp {
        font-size: 1.5em;
    }
}
            
          
!
            
              var API_KEY = "25a6ca31843cdb2c119eb1f9147a9776";
var celsius = false;
var weatherData;

function displayTemp(fTemp, c) {
	if (c) return Math.round((fTemp - 32) * (5 / 9)) + "° C";
	return Math.round(fTemp) + "° F";
}

function render(weatherData, celsius) {
	var currentLocation = weatherData.name;
	var currentIcon = weatherData.weather[0].icon;
	var currentWeather = weatherData.weather[0].description;
	var currentTemp = displayTemp(weatherData.main.temp, celsius);
	var highTemp = displayTemp(weatherData.main.temp_max, celsius);
	var lowTemp = displayTemp(weatherData.main.temp_min, celsius);
	var iconSrc = "https://openweathermap.org/img/w/" + currentIcon + ".png";

	$("#currentLocation").html(currentLocation);
	$("#currentIcon").html("<img src=" + iconSrc + ">");
	$("#currentWeather").html(currentWeather);
	$("#currentTemp").html(currentTemp);
	$("#highTemp").html("High: " + highTemp);
	$("#lowTemp").html("Low: " + lowTemp);

		// Changes HTML background image and .main-header and .display background color based on current weather icon
	if (currentIcon == "01d") {
		$("html").css("background-image", "url(https://regularlyscheduledprogrammingblog.files.wordpress.com/2018/01/clear-sun.jpg)")
		$(".main-header").css("background", "rgba(223, 89, 8, 0.75)")
		$(".display").css("background", "rgba(223, 89, 8, 0.75)")
	} else if (currentIcon == "01n") {
		$("html").css("background-image", "url(https://regularlyscheduledprogrammingblog.files.wordpress.com/2018/01/clear-moon.jpg)")
		$(".main-header").css("background", "rgba(8, 96, 156, 0.75)")
		$(".display").css("background", "rgba(8, 96, 156, 0.75)")
	} else if (currentIcon == "02d" || currentIcon == "03d" || currentIcon == "04d") {
		$("html").css("background-image", "url(https://regularlyscheduledprogrammingblog.files.wordpress.com/2018/01/cloudy-day.jpg)")
		$(".main-header").css("background", "rgba(41, 90, 231, 0.75)")
		$(".display").css("background", "rgba(41, 90, 231, 0.75)")
	} else if (currentIcon == "02n" || currentIcon == "03n" || currentIcon == "04n") {
		$("html").css("background-image", "url(https://regularlyscheduledprogrammingblog.files.wordpress.com/2018/01/cloudy-night.jpg)")
		$(".main-header").css("background", "rgba(2, 30, 115, 0.75)")
		$(".display").css("background", "rgba(2, 30, 115, 0.75)")
	} else if (currentIcon == "09d" || currentIcon == "10d" || currentIcon == "11d" || currentIcon == "09n" || currentIcon == "10n" || currentIcon == "11n") {
		$("html").css("background-image", "url(https://regularlyscheduledprogrammingblog.files.wordpress.com/2018/01/rain.jpg)")
		$(".main-header").css("background", "rgba(59, 64, 77, 0.75)")
		$(".display").css("background", "rgba(59, 64, 77, 0.75)")
	} else if (currentIcon == "13d" || currentIcon == "13n") {
		$("html").css("background-image", "url(https://regularlyscheduledprogrammingblog.files.wordpress.com/2018/01/snow.jpg)")
		$(".main-header").css("background", "rgba(105, 128, 195, 0.75)")
		$(".display").css("background", "rgba(105, 128, 195, 0.75)")
	} else if (currentIcon == "50d" || currentIcon == "50n") {
		$("html").css("background-image", "url(https://regularlyscheduledprogrammingblog.files.wordpress.com/2018/01/mist.jpg)")
		$(".main-header").css("background", "rgba(105, 128, 195, 0.75)")
		$(".display").css("background", "rgba(105, 128, 195, 0.75)")
	} else {
		$("html").css("background-image", "url(https://regularlyscheduledprogrammingblog.files.wordpress.com/2018/01/global-weather.jpg)")
		$(".main-header").css("background", "rgba(2, 30, 115, 0.75)")
		$(".display").css("background", "rgba(2, 30, 115, 0.75)")
	}

}

$(function () {
	var location;
	var long;
	var lat;

	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(function (position) {
			long = position.coords.longitude;
			lat = position.coords.latitude;

			$.getJSON("https://api.openweathermap.org/data/2.5/weather?units=imperial&lat=" + lat + "&lon=" + long + "&appid=" + API_KEY, function (apiData) {
				weatherData = apiData;

				render(weatherData, celsius);

				$("#toggle").click(function () {
					celsius = !celsius;
					render(weatherData, celsius);
				})
			})
		})
	}
})
            
          
!
999px
Loading ..................

Console