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 class="form" action="post" method="post" autocomplete="off" id="elForm">
<h1 id="elTitle">Sign Up</h1>
<div class="signup__field">
<label for="first_name" class="label">First name</label>
<input type="text" name="first_name" id="first_name" class="input-field" required>
</div>
<div class="signup__field">
<label for="last_name" class="label">Last name</label>
<input type="text" name="last_name" id="last_name" class="input-field" required>
</div>
<div class="signup__field">
<label for="email" class="label">Email address</label>
<input type="email" name="email" id="email" class="input-field" required>
</div>
<div class="signup__field">
<label for="password" class="label">Password</label>
<input type="password" name="password" id="password" class="input-field" minlength="8" maxlength="15" required>
</div>
<fieldset>
<legend>Favorites</legend>
<div class="signup__field">
<label class="label" for="fav_color">What's your favorite color?</label>
<div class="select-field">
<select name="fav_color" id="fav_color" class="select-field__menu">
<option>Please select an option</option>
<option value="Red">Red</option>
<option value="Orange">Orange</option>
<option value="Yellow">Yellow</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Purple">Purple</option>
</select>
</div>
</div>
<div class="signup__field" id="elRadioGrp">
<div class="label">What's your favorite Ninja Turtle?</div>
<input type="radio" value="leo" id="nt_leo" name="ninja_turtle">
<label for="nt_leo">Leo</label>
<input type="radio" value="leo" id="nt_mikey" name="ninja_turtle">
<label for="nt_mikey">Mikey</label>
<input type="radio" value="donnie" id="nt_donnie" name="ninja_turtle">
<label for="nt_donnie">Donnie</label>
<input type="radio" value="raph" id="nt_raph" name="ninja_turtle">
<label for="nt_raph">Raph</label>
</div>
</fieldset>
<div class="signup__field">
<input type="checkbox" id="amazing" name="amazing">
<label for="amazing">I'm amazing</label>
</div>
<div class="signup__button">
<button class="button" type="submit" id="elSubmitBtn">Signup</button>
</div>
</form>
body {
display:flex;
flex-flow:column nowrap;
justify-content:flex-start;
align-items:center;
min-height:100vh;
}
.form {
position:relative; min-width:50em; height:25em;
overflow:hidden;
border:thin solid rgba(0,0,0,0.1);
}
.form > * {
position:absolute; top:0; left:0;
}
.form h1 {
opacity:0.3;
}
input {
border:0 none;
}
input[type="radio"],
input[type="checkbox"] {
align-self:center; /*if stretch, nonconsistent*/
}
:not(fieldset)>.signup__field {
width:50%;
box-shadow:0 0 20px rgba(0,0,0,0.5);
background:white;
display:flex;
}
:not(fieldset)>.signup__field input {
flex:1 1 auto;
padding:0 1ch;
}
:not(fieldset)>.signup__field label {
padding:0.5em;
color:hsl(0,0%,20%);
border-top:thin solid black;
}
/*
fieldset
*/
fieldset {
box-shadow:0 0 20px rgba(0,0,0,0.5);
padding:1em;
}
legend {
position:relative; top:0.5em; left:0;
border-top:thin solid black;
box-sizing:border-box;
}
fieldset > :not(legend) {
margin:1em 0;
}
/*
Signal
*/
input:required:valid {
box-shadow:inset 0 0 10px green;
background:hsla(120,50%,50%,0.5);
}
input:required:invalid {
box-shadow:inset 0 0 10px red;
background:hsla(0,50%,50%,0.5);
}
/*
Submit btn
*/
button {
display:block;
width:5em;
height:5em;
border-radius:2.5em;
background-image:none;
}
/*
checkbox
*/
.form>.signup__field:nth-of-type(5) {
width:25%;
}
.form>.signup__field:nth-of-type(5) label {
flex:1 1 auto;
text-align:center;
padding:1em;
}
#amazing {
flex:0 0 2em;
}
class En {
constructor(el) {
this._el = el;
this._fieldElSet = this._makeFieldElSet();;
this._boundingClientRect = this._el.getBoundingClientRect();
this._matterRect = this._makeMatterRect();
}
get htmlEl() {return this._el; }
get fieldElSet() {return this._fieldElSet;}
get matterRect() {return this._matterRect;}
get cssPosition() {
return {
x: this._matterRect.position.x - this._boundingClientRect.width/2 - this._boundingClientRect.left,
y: this._matterRect.position.y - this._boundingClientRect.height/2 - this._boundingClientRect.top
};
}
get cssRotation() {
return this._matterRect.angle;
}
_makeFieldElSet() {
const els = Array.from(this._el.querySelectorAll("input, select, button"));
const wset = new WeakSet(els);
return wset;
}
_makeMatterRect() {
const rect = this._el.getBoundingClientRect();
const x = rect.left + rect.width/2;
const y = rect.top + rect.height/2;
let body;
if(this._el.nodeName == "FIELDSET") {
body = Matter.Bodies.rectangle(x, y, rect.width, rect.height);
body.friction = 0.1;
body.frictionAir = 0.2;
body.restitution = 0.25;
}
else if (this._el.classList.contains("signup__button")) {
body = Matter.Bodies.circle(x, y, rect.width/2);
body.friction = 0.1;
body.restitution = 0.7;
}
else {
body = Matter.Bodies.rectangle(x, y, rect.width, rect.height);
body.friction = 0.01;
body.restitution = 0.5;
}
return body;
}
}
const MSG_DID_SUBMIT = "Form has been submitted. Thank you.";
const BTN_ANG_VEL = -10; // deg
const BTN_FORCE = {x:-0.1, y:-0.1};
const INPUT_VALID_FORCE = {x:0.2, y:-0.3};
const INPUT_INVALID_FORCE = {x:-0.2, y:0.3};
const query = ":not(fieldset)>.signup__field, fieldset, .signup__button";
const els = Array.from(document.querySelectorAll(query));
const ens = [];
const walls = buildWalls(document.querySelector(".form"));
const engine = Matter.Engine.create();
const runner = Matter.Runner.run(engine);
//
// Create entities for field containers
//
for (const el of els) {
const en = new En(el);
ens.push(en);
Matter.World.add(engine.world, en.matterRect);
}
// Create walls
Matter.World.add(engine.world, [walls.B, walls.L, walls.R, walls.T]);
//
// Tick
//
Matter.Events.on(engine, "tick", e => {
let isAllOOB = true; // exclude walls, just entities
for (const en of ens) {
const {x, y} = en.cssPosition;
en.htmlEl.style.transform = `translate(${x}px, ${y}px)rotate(${en.cssRotation}rad)`;
isAllOOB &= (en.matterRect.position.y > walls.B.position.y);
}
if (isAllOOB)
submitForm();
});
function submitForm() {
Matter.Engine.clear(engine);
elTitle.innerHTML += `<br>${MSG_DID_SUBMIT}`;
Matter.Runner.stop(runner);
}
//
// DOM
//
const fieldEls = Array.from(document.querySelectorAll("input, select"));
for (const fieldEl of fieldEls)
fieldEl.addEventListener("change", onFieldUpdate);
function onFieldUpdate(e) {
const en = findEnByEl(e.target, ens);
if (e.target.checkValidity())
onInputValid({en, e});
else
onInputInvalid({en, e});
}
function onInputValid({e, en}){
Matter.Body.applyForce(en.matterRect, en.matterRect.position, INPUT_VALID_FORCE);
}
function onInputInvalid({e, en}){
Matter.Body.applyForce(en.matterRect, en.matterRect.position, INPUT_INVALID_FORCE);
}
//
// <select>
//
const selectEl = document.querySelector("#fav_color");
function onSelectUpdate(e) {
const i = e.target.selectedIndex;
elSubmitBtn.style.backgroundImage = i ? `linear-gradient(${e.target.children[i].value}, transparent 50%)` : `none`;
const en = findEnByEl(elSubmitBtn, ens);
Matter.Body.setAngularVelocity(en.matterRect, Math.PI/180 * BTN_ANG_VEL);
Matter.Body.applyForce(en.matterRect, en.matterRect.position, BTN_FORCE);
}
let timer = null;
selectEl.addEventListener("change", e => {
clearTimeout(timer);
timer = setTimeout(()=>onSelectUpdate(e), 100);
});
//
// Form submit
//
elForm.addEventListener("submit", e => {
e.preventDefault();
disableAllFields(e.target);
Matter.World.remove(engine.world, [walls.R]);
engine.world.gravity.x = 1;
const radioEls = Array.from(elRadioGrp.querySelectorAll(`[type="radio"]`));
let i = 0;
(function f(){
radioEls[i].checked = true;
i = i + 1 == radioEls.length ? 0 : i + 1;
if (runner.enabled)
setTimeout(f, 1000/24);
}());
});
//
// helpers
//
function findEnByEl(fieldEl, ens) {
for (const en of ens)
if (en.fieldElSet.has(fieldEl))
return en;
return null;
}
function disableAllFields(formEl) {
const fieldEls = Array.from(formEl.querySelectorAll("input, select, button"));
for (const fieldEl of fieldEls)
fieldEl.disabled = true;
}
function buildWalls(container) {
const bbox = container.getBoundingClientRect();
const thickness = 100;
const width = bbox.width;
const height = bbox.height;
const hw = width/2;
const hh = height/2;
const ht = thickness/2;
const T = Matter.Bodies.rectangle(bbox.left+hw,bbox.top-ht,width,thickness, {isStatic:true});
const B = Matter.Bodies.rectangle(bbox.left+hw,bbox.bottom+ht,width,thickness, {isStatic:true});
const L = Matter.Bodies.rectangle(bbox.left-ht,bbox.top+height/2,thickness,height, {isStatic:true});
const R = Matter.Bodies.rectangle(bbox.right+ht,bbox.top+height/2,thickness,height, {isStatic:true});
return {T, B, L, R};
}
Also see: Tab Triggers