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.
<main>
<h1>Register Your Cat</h1>
<div id="error-message" tabindex="-1"></div>
<p>* denotes required fields.</p>
<form id="register" action="thank-you.html" method="post">
<div class="container">
<div class="item">
<label for="name">Cat's name *</label>
<p id="error1" class="error"></p>
<input type="text" id="name" name="name" required aria-describedby="error1">
<label for="email">Your email *</label>
<p id="error2" class="error"></p>
<input type="email" id="email" name="email" autocomplete="email" required aria-describedby="error2">
<label for="birthday">Cat's birthday (optional)</label>
<p id="error4" class="help">Birthday format is <em>mm/dd/yyyy</em>.</p>
<input type="text" id="birthday" name="birthday" autocomplete="off" inputmode="numeric" aria-describedby="error4" pattern="^\s*(1[012]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/((?:19|20)\d{2})\s*$" maxlength="10">
<fieldset>
<legend>Is your cat altered? *</legend>
<label for="maybe"> <input name="altered" type="radio" id="maybe" checked>Not sure</label>
<label for="yes"> <input name="altered" type="radio" id="yes">Yes</label>
<label for="no"> <input name="altered" type="radio" id="no">No</label>
</fieldset>
</div>
<div class="item">
<label for="markings">Cat's markings *</label>
<p id="error3" class="error"></p>
<select required name="markings" id="markings" aria-describedby="error3">
<option value=""> select an option </option>
<option value="solid">Solid</option>
<option value="multi">Multi-color</option>
<option value="calico">Calico</option>
<option value="tabby">Tabby</option>
<option value="torbie">Torbie</option>
<option value="tortoiseshell">Tortoiseshell</option>
<option value="tuxedo">Tuxedo</option>
</select>
<fieldset id="colors" aria-describedby="error5">
<legend>Cat's colors *</legend>
<p id="error5" class="help">Select at least 1 color.</p>
<p><label for="black"> <input type="checkbox" id="black" name="black">Black</label></p>
<p><label for="white"> <input type="checkbox" id="white" name="white">White</label></p>
<p><label for="brown"> <input type="checkbox" id="brown" name="brown">Brown</label></p>
<p><label for="orange"> <input type="checkbox" id="orange" name="orange">Orange</label></p>
<p><label for="gray"> <input type="checkbox" id="gray" name="gray">Gray</label></p>
</fieldset>
</div>
</div>
<button type="submit">Send</button>
</form>
</main>
<script src="https://kit.fontawesome.com/127c73a8ed.js" crossorigin="anonymous"></script>
@import url("https://fonts.googleapis.com/css2?family=Libre+Franklin:wght@400;900&display=swap");
:root {
--primary: #fc466b;
--secondary: #fff;
--tertiary: #222;
--error: #fc466b;
--gradient: linear-gradient(
90deg,
rgba(232, 2, 83, 1) 0%,
rgba(132, 21, 161, 1) 82%
);
}
body {
background-color: var(--tertiary);
color: var(--secondary);
font-family: "Libre Franklin", sans-serif;
}
a,
a:visited {
color: var(--primary);
}
a:hover,
a:focus,
a:active {
border: 1px solid var(--primary);
}
main {
margin: 1em auto;
max-width: 50rem;
min-width: 15rem;
padding: 1em;
}
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
flex-flow: row wrap;
justify-content: space-between;
column-gap: 3em;
}
.item {
flex-grow: 2;
flex-basis: 40%;
}
h1 {
border-radius: 0.25em;
padding: 0.25em;
font-size: 3em;
text-align: center;
color: var(--secondary);
margin-top: 0;
background: rgb(232, 2, 83);
background: var(--gradient);
}
label,
legend {
font-size: 1.3em;
font-weight: bold;
display: block;
margin-top: 1em;
}
legend {
color: var(--secondary);
padding: 0.25em;
margin-top: 1em;
text-align: left;
}
fieldset {
background-color: var(--tertiary);
margin: 1em 0;
border: 1px solid var(--secondary);
min-width: 15rem;
border-radius: 0.25em;
}
fieldset label {
font-weight: normal;
display: inline;
white-space: nowrap;
}
input[type="text"],
input[type="email"],
select {
font-size: 1.3em;
width: 97%;
min-width: 15rem;
padding: 0.25em;
border: 1px solid var(--tertiary);
border-radius: 0.25em;
}
input[aria-invalid="true"],
select[aria-invalid="true"] {
border: 0.2em solid var(--error);
}
input[type="checkbox"],
input[type="radio"] {
height: 2em;
width: 2em;
margin-right: 1em;
margin-top: 1em;
}
button {
font-weight: bold;
background-color: var(--secondary);
border: 0.1em solid var(--secondary);
color: var(--tertiary);
font-size: 2em;
padding: 0.5em;
width: 100%;
margin-top: 1em;
border-radius: 0.25em;
}
button:hover,
button:focus {
background-color: #8415a1;
color: var(--secondary);
}
.errors {
padding: 1em;
border: 0.2em solid var(--error);
border-radius: 0.25em;
}
.error,
.help {
margin-bottom: 1em;
font-size: 1.1em;
}
.error {
color: var(--error);
}
.help {
color: #ccc;
}
.fas {
padding-right: 0.5em;
}
.visually-hidden {
border: 0 !important;
clip: rect(0 0 0 0) !important;
height: 1px !important;
margin: -1px !important;
overflow: hidden !important;
padding: 0 !important;
position: absolute !important;
width: 1px !important;
}
const form = document.getElementById("register");
const nameField = document.getElementById("name");
const emailField = document.getElementById("email");
const markingsField = document.getElementById("markings");
const dateField = document.getElementById("birthday");
const colorField = document.getElementById("colors");
const errorContainer = document.getElementById("error-message");
const error1 = document.getElementById("error1");
const error2 = document.getElementById("error2");
const error3 = document.getElementById("error3");
const error4 = document.getElementById("error4");
const error5 = document.getElementById("error5");
const icon =
'<span aria-hidden="true" class="fas fa-exclamation-triangle"></span><span class="visually-hidden">Error</span> ';
form.setAttribute("novalidate", true);
function clearErrors() {
let errors = document.getElementById("errors");
if (errors) {
errors.remove();
}
error1.innerHTML = "";
error2.innerHTML = "";
error3.innerHTML = "";
error4.innerHTML = "Birthday format is <em>mm/dd/yyyy</em>.";
error5.innerHTML = "Select at least 1 color.";
nameField.removeAttribute("aria-invalid");
emailField.removeAttribute("aria-invalid");
markingsField.removeAttribute("aria-invalid");
dateField.removeAttribute("aria-invalid");
colorField.classList.remove("errors");
error4.classList.remove("error");
error4.classList.add("help");
error5.classList.remove("error");
error5.classList.add("help");
}
function validateForm() {
clearErrors();
let hasErrors = false;
let errors = document.createElement("ul");
errors.id = "errors";
if (nameField.value === "") {
const error = document.createElement("li");
error.innerHTML = '<a href="#name">Cat\'s name</a> is required.';
errors.appendChild(error);
nameField.setAttribute("aria-invalid", "true");
error1.innerHTML = `${icon} Enter cat's name.`;
hasErrors = true;
}
if (emailField.value === "") {
const error = document.createElement("li");
error.innerHTML = '<a href="#email">Your email</a> is required.';
errors.appendChild(error);
emailField.setAttribute("aria-invalid", "true");
error2.innerHTML = `${icon} Enter your email.`;
hasErrors = true;
} else if (
!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailField.value)
) {
const error = document.createElement("li");
error.innerHTML =
'<a href="#email">Your email</a> address is not formatted correctly.';
errors.appendChild(error);
emailField.setAttribute("aria-invalid", "true");
error2.innerHTML = `${icon} Email format is <em>address@domain.com</em>.`;
hasErrors = true;
}
if (dateField.value !== "") {
if (
!/^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/.test(
dateField.value
)
) {
const error = document.createElement("li");
error.innerHTML =
"<a href='#birthday'>Cat's birthday</a> is not in the correct format.";
errors.appendChild(error);
dateField.setAttribute("aria-invalid", "true");
error4.classList.remove("help");
error4.classList.add("error");
error4.innerHTML = `${icon} Birthday format is <em>mm/dd/yyyy</em>.`;
hasErrors = true;
}
}
if (markingsField.value === "") {
const error = document.createElement("li");
error.innerHTML = '<a href="#markings">Cat\'s markings</a> is required.';
errors.appendChild(error);
markingsField.setAttribute("aria-invalid", "true");
error3.innerHTML = `${icon} Select cat's markings.`;
hasErrors = true;
}
let checkboxes = document.querySelectorAll("#colors input[type='checkbox']");
let checked = false;
checkboxes.forEach(function (checkbox) {
if (checkbox.checked) {
checked = true;
}
});
if (!checked) {
const error = document.createElement("li");
error.innerHTML = "<a href='#colors'>Cat's colors</a> is required.";
errors.appendChild(error);
document.getElementById("colors").classList.add("errors");
error5.innerHTML = `${icon} Select at least 1 color.`;
error5.classList.remove("help");
error5.classList.add("error");
hasErrors = true;
}
if (hasErrors) {
errorContainer.classList.add("errors");
if (!errorContainer.querySelector("h2")) {
const errorHeading = document.createElement("h2");
errorHeading.innerHTML = "Fix these errors to continue:";
errorContainer.appendChild(errorHeading);
}
errorContainer.appendChild(errors);
errorContainer.focus();
return false;
}
return true;
}
form.addEventListener("submit", function (e) {
if (!validateForm()) {
e.preventDefault();
}
});
// Add slashes to birthday
dateField.addEventListener("keyup", function () {
if (this.value.length === 2 || this.value.length === 5) {
this.value += "/";
}
});
Also see: Tab Triggers