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.
<body>
<div class="form-container">
<div class="success-message" id="successMessage">
Thank you! Your form has been submitted successfully.
</div>
<h1>Contact Form</h1>
<form id="gdprForm" novalidate>
<div class="form-group">
<label for="name">Full Name *</label>
<input type="text" id="name" name="name" required>
<span class="validation-icon">✓</span>
<div class="error-message" id="nameError">Please enter your full name</div>
</div>
<div class="form-group">
<label for="email">Email Address *</label>
<input type="email" id="email" name="email" required>
<span class="validation-icon">✓</span>
<div class="error-message" id="emailError">Please enter a valid email address</div>
</div>
<div class="form-group">
<label for="message">Message *</label>
<textarea id="message" name="message" rows="4" required></textarea>
<span class="validation-icon">✓</span>
<div class="error-message" id="messageError">Please enter your message</div>
</div>
<div class="checkbox-group">
<input type="checkbox" id="dataConsent" name="dataConsent" required>
<label for="dataConsent">
I consent to having this website store my submitted information so they can respond to my inquiry. See our privacy policy to learn more about how we protect and manage your submitted data. *
</label>
</div>
<div class="error-message" id="consentError">You must accept the privacy policy</div>
<div class="checkbox-group">
<input type="checkbox" id="marketing" name="marketing">
<label for="marketing">
I would like to receive marketing communications about your products and services
</label>
</div>
<button type="submit">Submit Form</button>
<div class="privacy-notice">
<p>* Required fields</p>
<p>Your personal data will be used to support your experience throughout this website, to manage access to your account, and for other purposes described in our privacy policy.</p>
</div>
</form>
</div>
</body>
:root {
--primary: #4f46e5;
--primary-dark: #4338ca;
--gray: #6b7280;
--error: #ef4444;
--success: #22c55e;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: system-ui, -apple-system, sans-serif;
background: #f3f4f6;
color: #1f2937;
line-height: 1.5;
padding: 2rem;
}
.form-container {
max-width: 600px;
margin: 0 auto;
background: white;
padding: 2rem;
border-radius: 1rem;
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
}
h1 {
font-size: 1.5rem;
margin-bottom: 1.5rem;
color: #111827;
}
.form-group {
margin-bottom: 1.5rem;
position: relative;
}
label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
}
input, textarea {
width: 100%;
padding: 0.75rem;
border: 1px solid #d1d5db;
border-radius: 0.5rem;
font-size: 1rem;
transition: all 0.15s ease;
}
input:focus, textarea:focus {
outline: none;
border-color: var(--primary);
box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}
.error-message {
color: var(--error);
font-size: 0.875rem;
margin-top: 0.25rem;
display: none;
}
.validation-icon {
position: absolute;
right: 1rem;
top: 2.5rem;
display: none;
}
.validation-icon.success {
color: var(--success);
display: block;
}
.validation-icon.error {
color: var(--error);
display: block;
}
input.success, textarea.success {
border-color: var(--success);
}
input.error, textarea.error {
border-color: var(--error);
}
.checkbox-group {
display: flex;
align-items: start;
gap: 0.75rem;
margin-top: 1rem;
}
.checkbox-group input[type="checkbox"] {
width: auto;
margin-top: 0.25rem;
}
.checkbox-group label {
font-size: 0.875rem;
color: var(--gray);
}
button {
background: var(--primary);
color: white;
padding: 0.75rem 1.5rem;
border: none;
border-radius: 0.5rem;
font-size: 1rem;
font-weight: 500;
cursor: pointer;
transition: background-color 0.15s ease;
}
button:hover {
background: var(--primary-dark);
}
.privacy-notice {
font-size: 0.875rem;
color: var(--gray);
margin-top: 2rem;
padding-top: 1rem;
border-top: 1px solid #e5e7eb;
}
.success-message {
display: none;
background: #dcfce7;
color: #166534;
padding: 1rem;
border-radius: 0.5rem;
margin-bottom: 1rem;
}
// Validation functions
const validators = {
name: (value) => value.trim().length >= 2,
email: (value) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value),
message: (value) => value.trim().length >= 10
};
// Debounce function to limit validation frequency
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Function to update field status
function updateFieldStatus(field, isValid) {
const errorElement = document.getElementById(`${field.id}Error`);
const validationIcon = field.parentElement.querySelector('.validation-icon');
field.classList.remove('success', 'error');
field.classList.add(isValid ? 'success' : 'error');
if (validationIcon) {
validationIcon.textContent = isValid ? '✓' : '✗';
validationIcon.classList.remove('success', 'error');
validationIcon.classList.add(isValid ? 'success' : 'error');
}
if (errorElement) {
errorElement.style.display = isValid ? 'none' : 'block';
}
}
// Progressive validation handler
const validateField = debounce((field) => {
if (field.type === 'checkbox') return;
const validator = validators[field.id];
if (!validator) return;
const isValid = validator(field.value);
updateFieldStatus(field, isValid);
}, 300);
// Add validation listeners to form fields
document.querySelectorAll('input[type="text"], input[type="email"], textarea').forEach(field => {
field.addEventListener('input', () => validateField(field));
field.addEventListener('blur', () => validateField(field));
});
// Form submission handler
document.getElementById('gdprForm').addEventListener('submit', function(e) {
e.preventDefault();
let hasErrors = false;
// Validate all fields
Object.entries(validators).forEach(([fieldName, validator]) => {
const field = document.getElementById(fieldName);
const isValid = validator(field.value);
updateFieldStatus(field, isValid);
if (!isValid) hasErrors = true;
});
// Validate consent
const consent = document.getElementById('dataConsent');
if (!consent.checked) {
document.getElementById('consentError').style.display = 'block';
hasErrors = true;
} else {
document.getElementById('consentError').style.display = 'none';
}
if (!hasErrors) {
// Collect form data
const formData = {
name: document.getElementById('name').value,
email: document.getElementById('email').value,
message: document.getElementById('message').value,
dataConsent: consent.checked,
marketing: document.getElementById('marketing').checked
};
// Here you would typically send the data to your server
console.log('Form data:', formData);
// Show success message
document.getElementById('successMessage').style.display = 'block';
// Reset form and validation states
this.reset();
document.querySelectorAll('.validation-icon').forEach(icon => {
icon.classList.remove('success', 'error');
icon.style.display = 'none';
});
document.querySelectorAll('input, textarea').forEach(field => {
field.classList.remove('success', 'error');
});
// Hide success message after 5 seconds
setTimeout(() => {
document.getElementById('successMessage').style.display = 'none';
}, 5000);
}
});
Also see: Tab Triggers