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>
<head>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.0.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.0.0/mapbox-gl.css' rel='stylesheet' />
</head>
<body>
<header>
<h1>Koordinates tile services with Mapbox GL demo</h1>
</header>
<!-- Map container -->
<div id='map'></div>
<!-- HTML5 template for popup content -->
<template id="popup-template">
<div class="popup">
<h2>Nearest Quake</h2>
<dl>
<dt>Time</dt>
<dd class="origintime"></dd>
<dt>Magnitude</dt>
<dd class="magnitude"></dd>
<dt>Depth</dt>
<dd><span class="depth"></span> km</dd>
</dl>
</div>
</template>
</body>
</html>
html,
body {
height: 100%;
margin: 0;
padding: 0 10px;
font-family: sans-serif;
}
body {
display: flex;
flex-direction: column;
}
header {
min-height: 60px;
}
/* make the map expand to fill available space */
#map {
flex-grow: 1;
overflow: auto;
}
/* set the cursor for map navigation */
#map canvas {
cursor: crosshair;
}
/* popup styling */
.popup dt {
font-weight: bold;
text-transform: uppercase;
}
.popup dd {
margin-left: 10px;
}
mapboxgl.accessToken =
"pk.eyJ1Ijoia29vcmRpbmF0ZXMiLCJhIjoiMDgzRHJlYyJ9.s6MmnjRMGuaRgRg_efw6Zg";
var map = new mapboxgl.Map({
container: "map", // container id
style: "mapbox://styles/mapbox/outdoors-v11",
center: [175, -41],
zoom: 6
});
console.log(map);
// Insert the Koordinates layer *below* the basemap labels + roads
map.on("load", function() {
// Find the ID of the first symbol layer in the map style
// We use this to insert our layer beneath roads/pois/labels/etc
var firstSymbolId = map.getStyle().layers.find(l => (l.type === "symbol")).id;
map.addLayer(
{
id: "koordinates-earthquakes",
type: "raster",
source: {
type: "raster",
tiles: [
// This is the XYZ template URL from the Koordinates layer
// services page: https://labs.koordinates.com/layer/7328-new-zealand-earthquakes/webservices/
"https://koordinates-tiles-a.global.ssl.fastly.net/services;key=1c6ead7c5174463eab66bcea1005233d/tiles/v4/layer=7328/EPSG:3857/{z}/{x}/{y}.png"
],
tileSize: 256,
maxzoom: 22,
attribution:
"<a href='https://labs.koordinates.com/layer/7328-new-zealand-earthquakes/'>New Zealand Earthquakes</a>"
}
},
firstSymbolId // Insert the layer beneath the first symbol layer.
);
// When a click event occurs on a feature in the states layer, open a popup at the
// location of the click, with description HTML from its properties.
var quakeMarker;
map.on("click", function(e) {
// construct the query url
let queryUrl = new URL('https://labs.koordinates.com/services/query/v1/vector.json');
// documentation of parameters at https://help.koordinates.com/api/query-api/vector-query/
let queryParams = {
key: '1c6ead7c5174463eab66bcea1005233d', // Koordinates API Key
layer: 7328, // Koordinates Layer ID
x: e.lngLat.lng, // map click location
y: e.lngLat.lat,
max_results: 1, // find the closest result
radius: 1000, // max. search distance 1km
geometry: true // Include geometry in the response
};
queryUrl.search = new URLSearchParams(queryParams);
// make the API request
fetch(queryUrl)
.then(function(response) {
if (!response.ok) {
throw new Error("HTTP error, status = " + response.status);
}
return response.json();
})
.then(function(response) {
// extract the first feature from the response
const feature = response.vectorQuery.layers['7328'].features[0];
console.log("Feature:", feature);
if (feature === undefined) {
// no features within 1km
if (quakeMarker) {
// remove marker from map
quakeMarker.remove();
quakeMarker = null;
}
return;
}
// position map marker
if (!quakeMarker) {
// add marker
quakeMarker = new mapboxgl.Marker()
.setLngLat(feature.geometry.coordinates)
.addTo(map);
} else {
// move existing marker
quakeMarker.setLngLat(feature.geometry.coordinates);
}
// create popup content
let popupTemplate = document.getElementById('popup-template');
let popupContent = document.importNode(popupTemplate.content, true);
popupContent.querySelector('.origintime').textContent = feature.properties.origintime;
popupContent.querySelector('.magnitude').textContent = feature.properties.magnitude;
popupContent.querySelector('.depth').textContent = feature.properties.depth;
// open map popup
let popup = new mapboxgl.Popup({anchor: 'left', offset: 10})
.setDOMContent(popupContent)
.setLngLat(feature.geometry.coordinates)
.addTo(map);
});
});
});
Also see: Tab Triggers