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.
<form>
<label for="Name">Name</label>
<input id="Name" type="text" />
<label for="Address">Address</label>
<textarea id="Address" cols="40" rows="5"></textarea>
<input id="Submit" value="Submit" type="button" />
</form>
body {
font: 12px verdana;
background-color: #5C87B2;
}
form {
max-width: 400px;
padding: 15px;
background-color: white;
}
label, input[type="text"], textarea {
display:block;
width: 100%;
margin-bottom: 6px;
}
input[type="button"] {
display:block;
width: 80px;
padding:3px;
margin-top:15px;
}
label.error {
color: #CC0000;
}
// The following code show execute only after the page is fully loaded
$(document).ready(function () {
if ($('form').exists()) {
// Enable jQuery Validation for the form
$('form').validate({ onkeyup: false });
// Add validation rules to the Address field
$("#Address").rules("add", {
fulladdress: true,
required: true,
messages: {
fulladdress: "Google cannot locate this address."
}
});
// This function will be executed when the form is submitted
function FormSubmit() {
$.submitForm = true;
if (!$('form').valid()) {
return false;
} else {
if ($("#Address").data("IsChecking") == true) {
$("#Address").data("SubmitForm", true);
return false;
}
alert('Form is valid!');
// return true; // Uncomment to submit the form.
return false; // Supress the form submission for test purpose.
}
}
// Attach the FormSubmit function to the Submit button
if ($('#Submit').exists()) {
$("#Submit").click(FormSubmit);
}
// Execute the ForumSubmit function when the form is submitted
$('form').submit(FormSubmit);
}
});
// Create a jQuery exists method
jQuery.fn.exists = function () { return jQuery(this).length > 0; }
// Address jQuery Validator
function AddressValidator(value, element, paras) {
// Convert the value variable into something a bit more descriptive
var CurrentAddress = value;
/* If the address is blank, then this is for the required validator to deal with. */
if (value.length == 0) {
return true;
}
/* If we've already validated this address, then just return the previous result */
if ($(element).data("LastAddressValidated") == CurrentAddress) {
return $(element).data("IsValid");
}
/* We have a new address to validate, set the IsChecking flag to true and set the LastAddressValidated to the CurrentAddress */
$(element).data("IsChecking", true);
$(element).data("LastAddressValidated", CurrentAddress);
/* Google Maps doesn't like line-breaks, remove them */
CurrentAddress = CurrentAddress.replace(/\n/g, "");
/* Create a new Google geocoder */
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': CurrentAddress }, function (results, status) {
/* The code below only gets run after a successful Google service call has completed. Because this is an asynchronous call, the validator has already returned a 'true' result to supress an error message and then cancelled the form submission. The code below needs to fetch the true validation from the Google service and then re-execute the jQuery form validator to display the error message. Futhermore, if the form was
being submitted, the code below needs to resume that submit. */
// Google reported a valid geocoded address
if (status == google.maps.GeocoderStatus.OK) {
// Get the formatted Google result
var address = results[0].formatted_address;
/* Count the commas in the fomatted address. This doesn't look great, but it helps us understand how specific the geocoded address is. For example, "CA" will geocde to "California, USA". */
numCommas = address.match(/,/g).length;
/* A full street address will have at least 3 commas. Alternate techniques involve fetching the address_components returned by Google Maps. That code looked even more ugly. */
if (numCommas >= 3) {
/* Replace the first comma found with a line-break */
address = address.replace(/, /, "\n");
/* Remove USA from the address (remove this, if this is important to you) */
address = address.replace(/, USA$/, "");
// Set the textarea value to the geocoded address
$(element).val(address);
// Cache this latest result
$(element).data("LastAddressValidated", address);
// We have a valid geocoded address
$(element).data("IsValid", true);
} else {
/* Google Maps was able to geocode the address, but it wasn't specific enough (not enough commas) to be a valid street address. */
$(element).data("IsValid", false);
}
// Otherwise the address is invalid
} else {
$(element).data("IsValid", false);
}
// We're no longer in the midst of validating
$(element).data("IsChecking", false);
// Get the parent form element for this address field
var form = $(element).parents('form:first');
/* This code is being run after the validation for this field, if the form was being submitted before this validtor was called then we need to re-submit the form. */
if ($(element).data("SubmitForm") == true) {
form.submit();
} else {
// Re-validate this property so we can return the result.
form.validate().element(element);
}
});
/* The Address validator always returns 'true' when initially called. The true result will be return later by the geocode function (above) */
return true;
}
// Define a new jQuery Validator method
$.validator.addMethod("fulladdress", AddressValidator);
Also see: Tab Triggers