HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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>
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;
}
}
/* 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 = " °" + 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 = " °" + 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 = " °" + 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.
})();
Also see: Tab Triggers