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.
<!-- Bootstrap inspired Braintree Hosted Fields example -->
<div class="bootstrap-basic">
<form class="needs-validation" novalidate="">
<div class="row">
<div class="col-sm-6 mb-3">
<label for="cc-name">Cardholder Name</label>
<div class="form-control" id="cc-name"></div>
<small class="text-muted">Full name as displayed on card</small>
<div class="invalid-feedback">
Name on card is required
</div>
</div>
<div class="col-sm-6 mb-3">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" placeholder="you@example.com">
<div class="invalid-feedback">
Please enter a valid email address for shipping updates.
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 mb-3">
<label for="cc-number">Credit card number</label>
<div class="form-control" id="cc-number"></div>
<div class="invalid-feedback">
Credit card number is required
</div>
</div>
<div class="col-sm-3 mb-3">
<label for="cc-expiration">Expiration</label>
<div class="form-control" id="cc-expiration"></div>
<div class="invalid-feedback">
Expiration date required
</div>
</div>
<div class="col-sm-3 mb-3">
<label for="cc-expiration">CVV</label>
<div class="form-control" id="cc-cvv"></div>
<div class="invalid-feedback">
Security code required
</div>
</div>
</div>
<hr class="mb-4">
<div class="text-center">
<button class="btn btn-primary btn-lg" type="submit">Pay with <span id="card-brand">Card</span></button>
</div>
</form>
</div>
<div aria-live="polite" aria-atomic="true" style="position: relative; min-height: 200px;">
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-autohide="false">
<div class="toast-header">
<strong class="mr-auto">Success!</strong>
<small>Just now</small>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="toast-body">
Next, submit the payment method nonce to your server.
</div>
</div>
</div>
/* Uses Bootstrap stylesheets for styling, see linked CSS*/
body {
background-color: #fff;
padding: 15px;
}
.toast {
position: fixed;
top: 15px;
right: 15px;
z-index: 9999;
}
.bootstrap-basic {
background: white;
}
/* Braintree Hosted Fields styling classes*/
.braintree-hosted-fields-focused {
color: #495057;
background-color: #fff;
border-color: #80bdff;
outline: 0;
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
}
.braintree-hosted-fields-focused.is-invalid {
border-color: #dc3545;
box-shadow: 0 0 0 0.2rem rgba(220, 53, 69, 0.25);
}
var form = $('form');
braintree.client.create({
authorization: 'sandbox_g42y39zw_348pk9cgf3bgyw2b'
}, function(err, clientInstance) {
if (err) {
console.error(err);
return;
}
braintree.hostedFields.create({
preventAutofill: false,
client: clientInstance,
styles: {
input: {
// change input styles to match
// bootstrap styles
'font-size': '1rem',
color: '#495057'
}
},
fields: {
cardholderName: {
selector: '#cc-name',
placeholder: 'Name as it appears on your card'
},
number: {
selector: '#cc-number',
placeholder: '4111 1111 1111 1111'
},
cvv: {
selector: '#cc-cvv',
placeholder: '123'
},
expirationDate: {
selector: '#cc-expiration',
placeholder: 'MM / YY'
}
}
}, function(err, hostedFieldsInstance) {
if (err) {
console.error(err);
return;
}
function createInputChangeEventListener(element) {
return function () {
validateInput(element);
}
}
function setValidityClasses(element, validity) {
if (validity) {
element.removeClass('is-invalid');
element.addClass('is-valid');
} else {
element.addClass('is-invalid');
element.removeClass('is-valid');
}
}
function validateInput(element) {
// very basic validation, if the
// fields are empty, mark them
// as invalid, if not, mark them
// as valid
if (!element.val().trim()) {
setValidityClasses(element, false);
return false;
}
setValidityClasses(element, true);
return true;
}
function validateEmail () {
var baseValidity = validateInput(email);
if (!baseValidity) {
return false;
}
if (email.val().indexOf('@') === -1) {
setValidityClasses(email, false);
return false;
}
setValidityClasses(email, true);
return true;
}
var ccName = $('#cc-name');
var email = $('#email');
ccName.on('change', function () {
validateInput(ccName);
});
email.on('change', validateEmail);
hostedFieldsInstance.on('validityChange', function(event) {
var field = event.fields[event.emittedBy];
// Remove any previously applied error or warning classes
$(field.container).removeClass('is-valid');
$(field.container).removeClass('is-invalid');
if (field.isValid) {
$(field.container).addClass('is-valid');
} else if (field.isPotentiallyValid) {
// skip adding classes if the field is
// not valid, but is potentially valid
} else {
$(field.container).addClass('is-invalid');
}
});
hostedFieldsInstance.on('cardTypeChange', function(event) {
var cardBrand = $('#card-brand');
var cvvLabel = $('[for="cc-cvv"]');
if (event.cards.length === 1) {
var card = event.cards[0];
// change pay button to specify the type of card
// being used
cardBrand.text(card.niceType);
// update the security code label
cvvLabel.text(card.code.name);
} else {
// reset to defaults
cardBrand.text('Card');
cvvLabel.text('CVV');
}
});
form.submit(function(event) {
event.preventDefault();
var formIsInvalid = false;
var state = hostedFieldsInstance.getState();
// perform validations on the non-Hosted Fields
// inputs
if (!validateEmail()) {
formIsInvalid = true;
}
// Loop through the Hosted Fields and check
// for validity, apply the is-invalid class
// to the field container if invalid
Object.keys(state.fields).forEach(function(field) {
if (!state.fields[field].isValid) {
$(state.fields[field].container).addClass('is-invalid');
formIsInvalid = true;
}
});
if (formIsInvalid) {
// skip tokenization request if any fields are invalid
return;
}
hostedFieldsInstance.tokenize(function(err, payload) {
if (err) {
console.error(err);
return;
}
// This is where you would submit payload.nonce to your server
$('.toast').toast('show');
// you can either send the form values with the payment
// method nonce via an ajax request to your server,
// or add the payment method nonce to a hidden inpiut
// on your form and submit the form programatically
// $('#payment-method-nonce').val(payload.nonce);
// form.submit()
});
});
});
});
Also see: Tab Triggers