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">
<h1>About You</h1>
<form name="userRegForm" onsubmit="return validateForm(this);">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" name="email" id="email" class="form-control" autocomplete="false" required pattern="^.{0,64}$" title="Enter an email address with a maximum of 64 characters." oninput="validateEmail(this);" onchange="validateEmail(this);">
<i class="input-glyph glyphicon glyphicon-ok-circle"></i>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" autocomplete="false" required pattern="^((?=.*[0-9])(?=.*[a-zA-Z])).{6,12}$" title="Enter a password between 6 and 12 characters, containing at least one letter and one number." oninput="validatePassword(this);" onchange="validatePassword(this);">
<i class="input-glyph glyphicon glyphicon-ok-circle"></i>
</div>
<div class="form-group">
<label for="passwordConfirm">Confirm Password</label>
<input type="password" name="passwordConfirm" id="passwordConfirm" class="form-control" autocomplete="false" required pattern="^((?=.*[0-9])(?=.*[a-zA-Z])).{6,12}$" title="Passwords must match." oninput="validatePassword(this);" onchange="validatePassword(this);">
<i class="input-glyph glyphicon glyphicon-ok-circle"></i>
<p class="help-block">Passwords must be between 6 and 12 characters long, containing at least one letter and one number. Please note, passwords are case sensitive.
</p>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="firstName">First Name</label>
<input type="text" name="firstName" id="firstName" class="form-control" autocomplete="false" required title="First name is required." oninput="return validateRequired(this);" onchange="return validateRequired(this);">
<i class="input-glyph glyphicon glyphicon-ok-circle"></i>
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input type="text" name="lastName" id="lastName" class="form-control" autocomplete="false" required title="Last name is required." oninput="return validateRequired(this);" onchange="return validateRequired(this);">
<i class="input-glyph glyphicon glyphicon-ok-circle"></i>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<button type="submit" class="btn btn-primary pull-right">Next</button>
</div>
</div>
</form>
</div>
.form-group {
position: relative;
}
.form-control {
padding-right: 30px;
}
.input-glyph {
position: absolute;
color: green;
right: 10px;
pointer-events: none;
top: 10px;
display: none;
form:not(.form-inline) label ~ & {
top: 34px; // input height
}
}
/*
Duplicate the selector for classes and pseudo-classes because the entire selector will fail for browsers that don't support `:invalid`.
*/
/**
* THIS WILL FAIL
*/
.form-control.valid ~ .input-glyph,
.form-control.valid:invalid ~ .input-glyph {
display: block;
}
.form-control.valid ~ .input-glyph {
display: block;
}
.form-control.valid:invalid ~ .input-glyph {
display: none;
}
.form-control:valid ~ .input-glyph {
display: block;
}
/*
Email regex pattern, same as the HTML5 form validation.
https://www.w3.org/TR/html-markup/datatypes.html#form.data.emailaddress
*/
var emailPattern = new RegExp('^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$');
/*
For the following email and password regex patterns, instead of duplicating the strings, just use the `pattern` attribute in the input field.
Unfortunately, we have to duplicate the value in the password and password confirm form fields themselves. If this form is created in a server page environment, we can create a single variable with that value and output it in both pattern attributes, like this:
JSP with JSTL:
<c:set var="passwordPattern" value="^((?=.*[0-9])(?=.*[a-zA-Z])).{6,12}$" />
<input pattern="${passwordPattern}">
*/
var email = document.forms['userRegForm'].email;
var password = document.forms['userRegForm'].password;
var passwordConfirm = document.forms[0].passwordConfirm;
var firstName = document.forms['userRegForm'].firstName;
var lastName = document.forms['userRegForm'].lastName;
// Email length regex pattern.
var emailLengthPattern = new RegExp($(email).attr('pattern'));
// Password regex pattern.
var passwordPattern = new RegExp($(password).attr('pattern'));
// Initialize popover for all required inputs
$('input[required]').popover({
placement: 'bottom',
container: 'body',
trigger: 'manual',
selector: 'true',
content: function() {
return $(this).attr('title');
},
template: '<div class="popover" role="tooltip"><div class="arrow"></div><div class="popover-content"></div></div>'
}).focus(function() {
$(this).popover('hide');
});
function validateForm(form) {
// Email validation.
if (!emailPattern.test(email.value) || !emailLengthPattern.test(email.value)) {
$(email).popover('show');
return false;
} else {
$(email).addClass('valid')
}
// Password validation.
if (!passwordPattern.test(password.value)) {
$(password).popover('show');
return false;
}
// Password confirm validation.
if (!passwordPattern.test(passwordConfirm.value)) {
$(passwordConfirm).popover('show');
return false;
}
// Password match validation.
if (password.value !== passwordConfirm.value) {
$(passwordConfirm).popover('show');
return false;
}
// First name validation.
if (firstName.value.length === 0) {
$(firstName).popover('show');
return false;
}
// Last name validation.
if (lastName.value.length === 0) {
$(lastName).popover('show');
return false;
}
// No validation errors.
alert('Submitted successfully');
// In this demo, prevent the form from submitting.
return false;
}
function validateEmail(input) {
if (emailPattern.test(input.value) && emailLengthPattern.test(input.value)) {
$(input).addClass('valid')
} else {
$(input).removeClass('valid');
}
}
/*
Sets a custom validation to require both password fields to match each other
*/
function validatePassword(input) {
if (input.setCustomValidity) {
input.setCustomValidity('');
if (input.validity && !input.validity.valid) {
input.setCustomValidity(input.title);
}
}
if (passwordConfirm.setCustomValidity) {
if (password.value !== passwordConfirm.value) {
passwordConfirm.setCustomValidity(passwordConfirm.title);
} else {
passwordConfirm.setCustomValidity('');
}
} else {
if (passwordPattern.test(input.value)) {
$(input).addClass('valid');
if (password.value === passwordConfirm.value) {
$(passwordConfirm).addClass('valid');
} else {
$(passwordConfirm).removeClass('valid');
}
} else {
$(input).removeClass('valid');
}
}
}
function validateRequired(input) {
if (input.setCustomValidity) {
input.setCustomValidity('');
if (input.validity && !input.validity.valid) {
input.setCustomValidity(input.title);
}
}
if (input.value.length > 0) {
$(input).addClass('valid');
} else {
$(input).removeClass('valid');
}
}
Also see: Tab Triggers