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.
<h1>Prefill Machine</h1>
<p>
<button id="prefill-correct">Prefill Correctly</button>
<button id="prefill-incorrect">Prefill with Errors</button>
</p>
<form action="#0" id="form">
<div>
<label for="name">Name</label>
<input type="text" name="name" id="name" placeholder="Digby Coyier" required>
</div>
<div>
<label for="email">Email</label>
<input type="email" name="email" id="email" placeholder="digby@digby.com" required>
</div>
<div>
<label for="name">Username</label>
<input type="text" name="username" id="username" placeholder="digby2007" required>
</div>
<div>
<label for="name">Password</label>
<input type="password" name="pw" id="pw" required>
</div>
<div>
<label for="name">Repeat</label>
<input type="password" name="pw-repeat" id="pw-repeat" required>
</div>
<fieldset>
<legend>Radio Choice</legend>
<div class="choice-group">
<label class="radio-label" for="radio-choice-1">Choice 1</label>
<input type="radio" name="radio-choice" id="radio-choice-1" value="choice-1" />
<label class="radio-label" for="radio-choice-2">Choice 2</label>
<input type="radio" name="radio-choice" id="radio-choice-2" value="choice-2" />
</div>
</fieldset>
<div>
<label for="select-choice">Select Choice</label>
<div class="choice-group">
<select name="select-choice" id="select-choice">
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
</select>
</div>
</div>
<div>
<label for="message">Message</label>
<textarea cols="40" rows="8" name="message" id="message"></textarea>
</div>
<div>
<label for="cc">Credit Card #</label>
<input type="text" name="cc" id="cc" placeholder="4242 4242 4242 4242" required>
</div>
<div>
<label for="exp-1">Expiration <span class="screen-reader">Month</span></label>
<input class="very-short" type="number" name="exp-1" id="exp-1" placeholder="08" min="1" max="12">
<label for="exp-2" class="screen-reader">Expiration Year</label>
<input type="number" name="exp-2" class="very-short" id="exp-2" placeholder="16" min="14">
</div>
<div>
<label for="exp-1">CVV</label>
<input class="short" type="text" name="cvv" id="cvv" placeholder="123">
</div>
<div>
<label for="name">Address</label>
<input type="text" class="long" name="address" id="address" placeholder="123 Super Street">
</div>
<div>
<label for="city"> <span class="screen-reader">City<span></label>
<input type="text" name="city" id="city" class="medium" placeholder="Milwaukee">
<label for="state" class="screen-reader">State</label>
<input type="text" name="state" class="very-short" id="state" placeholder="WI">
<label for="zip" class="screen-reader">Zip</label>
<input type="text" name="zip" class="short" id="zip" placeholder="55555" pattern="(\d{5}([\-]\d{4})?)" required>
</div>
<div>
<label for="agree-terms">Agree?</label>
<div class="choice-group">
<input type="checkbox" name="agree-terms" id="agree-terms">
</div>
</div>
<div>
<input type="submit" value="Submit" id="submit-button">
</div>
</form>
form {
padding-top: 30px;
min-width: 458px;
> div,
> fieldset {
border: 0;
padding: 0;
margin: 0 0 8px 0;
clear: both;
}
label,
legend {
float: left;
width: 100px;
padding: 7px 10px;
&.radio-label {
float: none;
padding: 0;
}
}
.choice-group {
padding: 7px 10px 0 10px;
}
input[type=checkbox],
input[type=radio] {
margin-right: 10px;
}
input[type=text],
input[type=email],
input[type=password],
input[type=number],
textarea {
width: 200px;
border: 1px solid darken(tan, 20%);
padding: 7px 10px;
border-radius: 4px;
outline: 0;
&:focus {
border-color: black;
}
&.short {
width: 60px;
}
&.medium {
width: 150px;
}
&.very-short {
width: 40px;
}
&.long {
width: 300px;
}
}
}
.screen-reader {
position: absolute;
top: -9999px;
left: -9999px;
}
body {
background: #E27C37;
padding: 20px;
font-size: small;
}
var PrefillMachine = {
init: function() {
this.bindUIActions();
},
bindUIActions: function() {
$("#prefill-correct").on("click", $.proxy(this.prefillCorrectly, this));
$("#prefill-incorrect").on("click", $.proxy(this.prefillIncorrectly, this));
},
prefillCorrectly: function() {
var id = this._makeId();
// Prefixing only because would be easier to find in the database and purge.
$("#name").val("name_" + id);
$("#username").val("username_" + id);
// The "+" syntax here is for GMail, which means I can use something unique but still receive the email for testing.
$("#email").val("chriscoyier+" + id + "@gmail.com");
// Match whatever password requirements you enforce here
$("#pw, #pw-repeat").val("password_" + id);
// Randomly select one or the other
$("input[name='radio-choice']")
.prop("checked", false)
.eq(this._rand(0, 1))
.prop("checked", true);
// SELECT choice.
$("#select-choice")
.find("option")
.prop("selected", false)
.eq(this._rand(0, 2))
.prop("selected", true);
// The backslash-n's are new lines. Might as well test those come across OK.
$("#message").val("\
There was an Old Man with a beard\n\
Who said, It is just as I feared!\n\
Two Owls and a Hen,\n\
Four Larks and a Wren,\n\
Have all built their nests in my beard!");
// This is the Stripe card number for testing
$("#cc").val("4242424242424242");
// Expiration date can be anything as long as it's in the future
$("#exp-1").val("08");
$("#exp-2").val("15");
// Can be anything with Stripe
$("#cvv").val("343");
$("#address").val(this._rand(1000, 9000) + " Super St.");
$("#city").val("Milwaukee");
$("#state").val("WI");
$("#zip").val(this._rand(11111, 99999));
$("#agree-terms").prop("checked", true);
},
prefillIncorrectly: function() {
var id = this._makeId();
// Empty should be invalid
$("#name").val("");
$("#username").val("");
// Space should make invalid
$("#email").val("jim @jim.com");
// Force them not to match AND one is blank.
$("#pw").val("123");
$("#pw-repeat").val("");
// Turn both off.
$("input[name='radio-choice']")
.prop("checked", false);
// Can't really turn a select off but we can try.
$("#select-choice")
.find("option")
.prop("selected", false);
// Testing an empty-but-that's-OK thing.
$("#message").val("");
// Not enough numbers
$("#cc").val("424242424242");
// Values are wrong
$("#exp-1").val("50");
$("#exp-2").val("02");
$("#cvv").val("abc");
$("#address").val("");
$("#city").val("");
$("#state").val("");
$("#zip").val("");
// Left unchecked
$("#agree-terms").prop("checked", false);
// Triggering a submit event on the form seems to submit without validation
// $("#submit-button").click();
},
_makeId: function() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i=0; i < 5; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
},
_rand: function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
}
PrefillMachine.init();
Also see: Tab Triggers