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="container">
<h4>Shipping Address</h4>
<p>You can click the button below to use your current location as your shipping address</p>
<div id="map">
</div>
<p id="current_position"></p>
<button id="showMe" class="btn center-align">
<i class="material-icons">
my_location
</i>
Use My Location
</button>
<form id="billingAddress">
<div id="locationList"></div>
<br>
<div class="input-field">
<textarea class="materialize-textarea" id="address" type="text"></textarea>
<label class="active" for="address">Address (Area and Street)</label>
</div>
<div class="input-field">
<input id="locality" type="text">
<label class="active" for="locality">Locality</label>
</div>
<div class="input-field">
<input id="city" type="text">
<label class="active" for="city">City/District/Town</label>
</div>
<div class="input-field">
<input id="postal_code" type="text">
<label class="active" for="pin_code">Pin Code</label>
</div>
<div class="input-field">
<input id="landmark" type="text">
<label class="active" for="landmark">Landmark</label>
</div>
<div class="input-field">
<input id="state" type="text">
<label class="active" for="State">State</label>
</div>
</form>
</div>
.container{
width: 50%;
}
#map{
height: 50vh;
margin-bottom: 10px;
display: none;
}
#locationList .card{
padding: 10px;
}
#toast-container{
top: 50%;
bottom: unset;
}
.toast{
background-color: rgba(0,0,0,.8);
}
.btn i{
font-size: 1rem;
margin-right: 2px;
}
@media only screen and (min-width: 768px){
.container{
width: 80%;
max-width: 800px;
}
}
//This div will be used to show Google map
const mapArea = document.getElementById('map');
const $ = id => document.getElementById(id);
const actionBtn = document.getElementById('showMe');
const locationsAvailable = document.getElementById('locationList');
let Gmap, Gmarker;
const __KEY = "AIzaSyBGCql0HlN4C_D7B2BcIIhtuFvjrdfvoew";
actionBtn.addEventListener('click', e => {
// hide the button
actionBtn.style.display = "none";
// call Materialize toast to update user
M.toast({ html: 'I am fetching your current location', classes: 'rounded' });
// get the user's position
getLocation();
});
getLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayLocation, showError, options)
}
else {
M.toast({ html: 'Sorry, your browser does not support this feature... Please Update your Browser to enjoy it', classes: 'rounded' });
}
}
// displayLocation
displayLocation = (position) => {
const lat = position.coords.latitude;
const lng = position.coords.longitude;
const latlng = { lat, lng }
showMap(latlng, lat, lng);
createMarker(latlng);
mapArea.style.display = "block";
getGeolocation(lat, lng);
}
// Recreates the map
showMap = (latlng, lat, lng) => {
let mapOptions = {
center: latlng,
zoom: 17
};
Gmap = new google.maps.Map(mapArea, mapOptions);
Gmap.addListener('drag', function () {
Gmarker.setPosition(this.getCenter()); // set marker position to map center
});
Gmap.addListener('dragend', function () {
Gmarker.setPosition(this.getCenter()); // set marker position to map center
});
Gmap.addListener('idle', function () {
Gmarker.setPosition(this.getCenter()); // set marker position to map center
if (Gmarker.getPosition().lat() !== lat || Gmarker.getPosition().lng() !== lng) {
setTimeout(() => {
// console.log("I have to get new geocode here!")
updatePosition(this.getCenter().lat(), this.getCenter().lng()); // update position display
}, 2000);
}
});
}
// Creates marker on the screen
createMarker = (latlng) => {
let markerOptions = {
position: latlng,
map: Gmap,
animation: google.maps.Animation.BOUNCE,
clickable: true
// draggable: true
};
Gmarker = new google.maps.Marker(markerOptions);
}
// updatePosition on
updatePosition = (lat, lng) => {
getGeolocation(lat, lng);
}
// Displays the different error messages
showError = (error) => {
mapArea.style.display = "block"
switch (error.code) {
case error.PERMISSION_DENIED:
mapArea.innerHTML = "You denied the request for your location."
break;
case error.POSITION_UNAVAILABLE:
mapArea.innerHTML = "Your Location information is unavailable."
break;
case error.TIMEOUT:
mapArea.innerHTML = "Your request timed out. Please try again"
break;
case error.UNKNOWN_ERROR:
mapArea.innerHTML = "An unknown error occurred please try again after some time."
break;
}
}
const options = {
enableHighAccuracy: true
}
getGeolocation = (lat, lng) => {
const latlng = lat + "," + lng;
fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${latlng}&key=${__KEY}`)
.then(res => res.json())
.then(data => populateCard(data.results));
}
function removeAddressCards() {
if (locationsAvailable.hasChildNodes()) {
while (locationsAvailable.firstChild) {
locationsAvailable.removeChild(locationsAvailable.firstChild);
}
}
}
populateCard = (geoResults) => {
// check if a the container has a child node to force re-render of dom
removeAddressCards();
geoResults.map(geoResult => {
// first create the input div container
const addressCard = document.createElement('div');
// then create the input and label elements
const input = document.createElement('input');
const label = document.createElement('label');
// then add materialize classes to the div and input
addressCard.classList.add("card");
input.classList.add("with-gap");
// add attributes to them
label.setAttribute("for", geoResult.place_id);
label.innerHTML = geoResult.formatted_address;
input.setAttribute("name", "address");
input.setAttribute("type", "radio");
input.setAttribute("value", geoResult.formatted_address);
input.setAttribute("id", geoResult.place_id);
// input.addEventListener('click', e => console.log(123));
input.addEventListener('click', () => inputClicked(geoResult));
// finalResult = input.value;
finalResult = geoResult.formatted_address;
addressCard.appendChild(input);
addressCard.appendChild(label);
// console.log(geoResult.formatted_address)
return (
locationsAvailable.appendChild(addressCard)
);
})
}
inputClicked = (result) => {
result.address_components.map(component => {
const types = component.types
if (types.includes('postal_code')) {
$('postal_code').value = component.long_name
}
if (types.includes('locality')) {
$('locality').value = component.long_name
}
if (types.includes('administrative_area_level_2')) {
$('city').value = component.long_name
}
if (types.includes('administrative_area_level_1')) {
$('state').value = component.long_name
}
if (types.includes('point_of_interest')) {
$('landmark').value = component.long_name
}
});
$('address').value = result.formatted_address;
// to avoid labels overlapping prefilled contents
M.updateTextFields();
removeAddressCards();
}
Also see: Tab Triggers