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.
<div class="bg"></div>
<div class="Weather column column-center">
<div class="Weather__current">
<h1 class="title">Current Weather</h1>
<div class="Weather__current-wrap column column-center">
<h1 class="Weather__current-location" id="location">Your Location</h1>
<h1 class="Weather__current-temp" id="temp">70 °F</h1>
<p class="Weather__current-condition" id="condition">Sunny</p>
<button id="toggle">Show Celcius</button>
</div>
</div>
<div class="Weather__forecast column column-center">
<h1 class="title">5 Day Forecast</h1>
<div class="Weather__forecast-wrap row"></div>
</div>
</div>
<div class="range column column-center">
<h2>Test Buttons:</h2>
<div class="row">
<button id="cold">Below 40</button>
<button id="cool">Below 55/Above 40</button>
<button id="warm">Below 70/Above 55</button>
<button id="perfect">Below 85/Above 69</button>
<button id="hot">Above 85</button>
</div>
<div class="row">
<button id="reset">Reset To Current Temp</button>
</div>
</div>
// Resets
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Lato', sans-serif;
}
button {
outline: none;
background: none;
cursor: pointer;
}
// Globals
.row, .column {
display: flex;
}
.column {
flex-direction: column;
}
.center {
justify-content: center;
}
.column-center {
align-items: center;
}
.split {
justify-content: space-between;
}
// Page Styles
.bg {
background-size: cover;
background-repeat: no-repeat;
min-height: 100vh;
width: 100%;
position: absolute;
top: 0;
left: 0;
z-index: -1;
opacity: .3;
}
.Weather {
padding-top: 50px;
.title {
font-weight: 300;
margin-bottom: 20px;
}
&__current {
margin-bottom: 40px;
&-location {
font-weight: 300;
margin-bottom: 10px;
}
&-temp {
font-size: 3rem;
font-weight: 400;
color: #555;
}
&-condition {
font-size: 1.3rem;
letter-spacing: 1px;
margin-top: 5px;
color: #555;
}
#toggle {
border: 1px solid #333;
color: #333;
padding: 3px 10px;
border-radius: 2px;
margin-top: 10px;
&:active {
background: rgba(0, 0, 0, .1);
}
}
}
&__forecast {
&-day {
margin: 0 15px;
h2 {
font-weight: 300;
font-size: 1.4rem;
margin-bottom: 5px;
}
.wi {
font-size: 2rem;
color: #5f5f5f;
margin-bottom: 8px;
}
}
&-high, &-low {
font-size: 1.05rem;
}
&-high {
color: #333;
margin-right: 10px;
}
&-low {
color: #6e6e6e;
}
}
}
.range {
margin-top: 20px;
h2 {
margin-bottom: 12px;
font-weight: 300;
position: relative;
}
button {
border: 1px solid #444;
padding: 3px 5px;
border-radius: 2px;
margin: 0 5px;
&:active {
background: rgba(0, 0, 0, .1);
}
}
#reset {
border: 1px solid #444;
padding: 3px 5px;
border-radius: 2px;
margin-top: 15px;
}
}
var weather = (function() {
var lat, long, country, city, tempF, tempC, condition, forecast,
isFar = true, lows = [], highs = [];
var init = function() {
getLocation();
$('#toggle').on('click', toggleUnit);
$('.range').find('button').on('click', changeBg);
$('#reset').on('click', reset);
};
var getLocation = function() {
if ( window.chrome ) {
$.getJSON('http://ip-api.com/json', function(json) {
lat = json.lat;
long = json.lon;
getWeather();
});
} else {
if ( navigator.geolocation ) {
navigator.geolocation.getCurrentPosition(function(data) {
lat = data.coords.latitude;
long = data.coords.longitude;
getWeather();
});
}
}
};
var getWeather = function() {
// Get Current Weather
$.getJSON('http://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + long + '&units=imperial&APPID=3acc16ffae9e45df92a064e41646355f', function(json) {
city = json.name;
country = json.sys.country;
tempF = Math.round(json.main.temp);
tempC = Math.round((tempF - 32) * 5 / 9);
condition = json.weather[0].description.split(' ').map(function(a) {
return a[0].toUpperCase() + a.slice(1);
}).join(' ');
showCurrentWeather();
});
// Get Forecast Weather
$.getJSON('http://api.openweathermap.org/data/2.5/forecast/daily?lat=' + lat + '&lon=' + long + '&units=imperial&cnt=5&APPID=3acc16ffae9e45df92a064e41646355f', function(json) {
forecast = json.list;
showForecast();
});
};
var showCurrentWeather = function() {
var image = tempF > 85 ? '789' : tempF < 85 && tempF > 69 ? '977' : tempF > 55 && tempF < 70 ? '957' : tempF < 55 && tempF > 40 ? '652' : tempF < 40 ? '971' : '';
$('#location').html(city + ', ' + country);
$('#temp').html(tempF + ' °F');
$('#condition').html(condition);
$('.bg').css('background-image', 'url(https://unsplash.it/1500/1000?image=' + image + ')');
};
var showForecast = function() {
var today = new Date().getDate(),
day, icon, low, high;
forecast.map(function(obj) {
day = new Date(obj.dt * 1000).getDate() === today ? 'Today' : new Date(obj.dt * 1000).toString().split(' ')[0];
icon = 'wi wi-owm-' + obj.weather[0].id; // Using erikflowers.github.io/weather-icons/
low = Math.round(obj.temp.min);
high = Math.round(obj.temp.max);
// Going to use these for changing the unit on the forecast temps when button is clicked
lows.push(low);
highs.push(high);
$('.Weather__forecast-wrap').append(
'<div class="Weather__forecast-day column column-center">' +
'<h2>' + day + '</h2>' +
'<i class="' + icon + '"></i>' +
'<div class="row">' +
'<span class="Weather__forecast-high">' + high + '°</span>' +
'<span class="Weather__forecast-low">' + low + '°</span>' +
'</div>' +
'</div>'
);
});
};
var toggleUnit = function() {
if ( isFar ) {
/**
* We go through each span on the page and toggle the temp based on fahrenheit
* true/false value. I use the lows/highs arrays to get the temps that were
* originally sent through the api. I stored those temps in the arrays so I
* could use them down here.
**/
$('.Weather__forecast-high').each(function(index, span) {
span.innerHTML = Math.round((highs[index] -32) * 5 / 9) + '°';
});
$('.Weather__forecast-low').each(function(index, span) {
span.innerHTML = Math.round((lows[index] -32) * 5 / 9) + '°';
});
// This is for the current weather text
$('#temp').html(tempC + ' °C');
$('#toggle').text('Show Fahrenheit');
isFar = false;
} else { // Same as above, just for fahrenheit
$('.Weather__forecast-high').each(function(index, span) {
span.innerHTML = highs[index] + '°';
});
$('.Weather__forecast-low').each(function(index, span) {
span.innerHTML = lows[index] + '°';
});
$('#temp').html(tempF + ' °F');
$('#toggle').text('Show Celcius');
isFar = true;
}
};
var changeBg = function() {
var btn = $(this).attr('id');
var image = btn == 'hot' ? '789' : btn == 'perfect' ? '977' : btn == 'warm' ? '957' : btn == 'cool' ? '652' : btn == 'cold' ? '971' : '';
$('.bg').css('background-image', 'url(https://unsplash.it/1500/1000?image=' + image + ')');
};
var reset = function() {
$('.Weather__forecast-wrap').empty();
getLocation();
if ( !isFar ) toggleUnit();
};
return {
init: init
};
})();
weather.init();
Also see: Tab Triggers