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 URL's 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 it's URL and the proper URL extention.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" type="text/css" />
<div class="holder">
<div class="generic_notification">
<i class="icon"></i>
<span class="txt"></span>
</div>
</div>
<form id="form1" class="go-top">
<h2>Form</h2>
<div class="form_row">
<input id="name" name="name" type="text" required data-msg="Please enter Your Name">
<label for="name">Your Name</label>
</div>
<div class="form_row">
<input id="email" name="email" type="email" required data-msg="Please enter a valid Email">
<label for="email">Email</label>
</div>
<div class="form_row">
<textarea id="message" name="message" required data-msg="Please a Message"></textarea>
<label for="message">Message</label>
</div>
<a href="Javascript: validate();">Submit</a>
</form>
<form id="form2" class="go-right">
<h2>Form 2 (to show that validation is locked to form1)</h2>
<div class="form_row">
<input id="name" name="name" type="text" required>
<label for="name">Your Name</label>
</div>
<div class="form_row">
<input id="phone" name="phone" type="tel" required>
<label for="phone">Primary Phone</label>
</div>
<div class="form_row">
<textarea id="message" name="phone" required></textarea>
<label for="message">Message</label>
</div>
</form>
@import "compass/css3";
* {
box-sizing: border-box;
}
html {
font: 14px/1.4 Sans-Serif;
}
form {
width: 320px;
float: left;
margin: 20px;
> div {
margin-top:20px;
position: relative;
}
input, textarea {
width: 100%;
border: 2px solid gray;
background: none;
position: relative;
top: 0;
left: 0;
z-index: 1;
padding: 8px 12px;
outline: 0;
&:valid {
// Hides the label
background: white;
}
&:focus {
border-color: blue;
}
&:focus + label {
color: blue;
font-size: 70%;
padding: 1px 6px;
z-index: 2;
text-transform: uppercase;
}
}
label {
transition: // not padding
background 0.2s,
color 0.2s,
top 0.2s,
bottom 0.2s,
right 0.2s,
left 0.2s;
position: absolute;
color: #999;
padding: 7px 6px;
}
textarea {
display: block;
resize: vertical;
}
}
form.go-top {
input, textarea {
padding: 12px 12px 12px 12px;
}
label {
top: 0;
bottom: 0;
left: 0;
width: 100%;
}
input:focus, textarea:focus {
}
input:focus + label, textarea:focus + label {
top: -15px;
}
}
form.go-right {
label {
top: 2px;
right: 100%;
width: 100%;
margin-right: -100%;
bottom: 2px;
}
input:focus + label, textarea:focus + label {
right: 0;
margin-right: 0;
width: 40%;
padding-top: 5px;
}
}
.form_row > label.incomplete{color:red;}
.form_row.incomplete {
background:#FDE19F;
}
/* ------------------------------------------------------------+
Notifications
+--------------------------------------------------------------*/
.holder {margin:10px;max-width:700px;}
.generic_notification {height:0;overflow:hidden;position:relative;}
.generic_notification .icon {margin-right:10px;font-size:1.5em;}
.generic_notification .txt {display:inline-block;vertical-align:top;line-height:1.5em;}
.generic_notification.active {overflow:show;height:auto;border-radius:7px;border:1px solid; margin:10px 0px; padding:15px 10px 15px 10px; background-repeat:no-repeat; background-position:10px center;}
.generic_notification.info {color:#00529B;background-color:#4CC1BD;color:#fff;}
.generic_notification.success {color:#4F8A10;background-color:#DFF2BF;}
.generic_notification.warning {color:#9F6000;background-color:#FEEFB3;}
.generic_notification.error {color:#D8000C;background-color:#FFBABA;}
$(document).ready(function(){
markupValidation("form1");
});
/*
This is called when we hit Submit we call the validation function with the formID, and provide a call back function to
take the error messages (data-msg). If the form is not valid then we use the notification box.
we could easily put the erDesc string into the notification box instead which would list the missing fields based on each
elements data-msg attribute but we are not using this in the example.
*/
function validate(){
validateDynamicForm("form1",function(isValid,erDesc){
if(isValid){
alert("form valid");
}else{
// here i show a notification box but you could
// easily alert the erDesc variable for a list of
// data-msg values for missing form fields
if($('.generic_notification').length){
resetNotification('generic_notification');
setNotification('generic_notification','active warning','Required fields missing, please see below<br>' + erDesc,'fa fa-exclamation-circle');
$('html, body').animate({
scrollTop: $(".generic_notification").offset().top
}, 1000);
}else{
// a fall back
alert('Required fields missing, missing fields have been marked in red');
}
}
});
}
// here we have the function that adds the * to required fields.
function markupValidation(formID){
var fields = $('#' + formID + ' INPUT, #' + formID + ' SELECT, #' + formID + ' TEXTAREA');
for(var i=0; i < fields.length; i++) {
var $fieldID = fields[i].id;
var $label = $('#' + formID + ' LABEL[for=' + $fieldID + ']');
if(fields[i].hasAttribute('required')) {
$label.text($label.text().replace('*',''));
$label.text($label.text()+'*');
}
if(fields[i].hasAttribute('data-lbl')) {
var $dataLbl = $('#' + $fieldID).attr('data-lbl');
if($dataLbl !== undefined){
$label.text($label.text().replace($dataLbl,''));
$label.text($label.text()+$dataLbl);
}
}
}
}
/* --------------------------- Notifications --------------------------- */
function resetNotification(notificationClass){
$('.' + notificationClass).removeClass('active warning info success error');
$('.' + notificationClass).find('.icon').removeClass().addClass('icon');
}
function setNotification(notificationClass,classNames,textHtml,iconClass){
$('.' + notificationClass).addClass(classNames).find('.txt').html(textHtml);
$('.' + notificationClass).find('.icon').addClass(iconClass);
}
/* --------------------------- //Notifications --------------------------- */
function validateDynamicForm(formID,callback){
/*
-----------------------------------------------------------------------------------
Validation Routine, loops through items with a "required" attribute.
-----------------------------------------------------------------------------------
Passed Values:
formID - locks validation to within a certain fromID.
callback - a function that gets form valid and the error description passed back.
Form Attributes / Usage:
data-lenth - for text areas that need restricted character length
data-msg - return message if field fails validation
-----------------------------------------------------------------------------------
*/
var $formIsValid = true;
var $formErMsgs = "";
var fields = $('#' + formID + ' INPUT, #' + formID + ' SELECT, #' + formID + ' TEXTAREA');
for(var i=0; i < fields.length; i++) {
if(fields[i].hasAttribute('required') && !fields[i].hasAttribute('disabled')) {
var $fieldID = fields[i].id;
var $fieldName = fields[i].name;
var $fieldType = fields[i].type;
var $fieldLimit = "";
var $fieldErrorDesc = "";
var $fieldLabel = $('#' + formID + ' LABEL[for=' + $fieldID + ']').text();
var $fieldLabelError = $('#' + formID + ' LABEL[for=' + $fieldID + ']');
var $fieldRowError = $('#' + formID + ' LABEL[for=' + $fieldID + ']').closest('.form_row');
$fieldLabelError.removeClass('incomplete');
$fieldRowError.removeClass('incomplete');
$fieldLimit = (fields[i].hasAttribute('data-length')) ? $('#' + formID + ' #' + $fieldID).attr("data-length") : "";
$fieldErrorDesc =(fields[i].hasAttribute('data-msg')) ? $('#' + formID + ' #' + $fieldID).attr("data-msg") : "";
switch($fieldType){
case "text":
if ($('#' + formID + ' #' + $fieldID).val() === '' && $('#' + formID + ' #' + $fieldID).is(':visible')){
$formIsValid = false;
$fieldLabelError.addClass('incomplete');
$fieldRowError.addClass('incomplete');
$formErMsgs = $formErMsgs + ' - ' + $fieldErrorDesc + ' <br/>';
}
break;
case "password":
if ($('#' + formID + ' #' + $fieldID).val() === '' && $('#' + formID + ' #' + $fieldID).is(':visible')){
$formIsValid = false;
$fieldLabelError.addClass('incomplete');
$fieldRowError.addClass('incomplete');
$formErMsgs = $formErMsgs + ' - ' + $fieldErrorDesc + ' <br/>';
}
break;
case "email":
if (validateEmail($('#' + formID + ' #' + $fieldID)) == -1){
$formIsValid = false;
$fieldLabelError.addClass('incomplete');
$fieldRowError.addClass('incomplete');
$formErMsgs = $formErMsgs + ' - ' + $fieldErrorDesc + ' <br/>';
}
break;
case "textarea":
if ($('#' + formID + ' #' + $fieldID).val() === ''){
$formIsValid = false;
$fieldLabelError.addClass('incomplete');
$fieldRowError.addClass('incomplete');
$formErMsgs = $formErMsgs + ' - ' + $fieldErrorDesc + ' <br/>';
}
break;
case "select-one":
if ($('#' + formID + ' #' + $fieldID + ' :selected').index() === 0){
$formIsValid = false;
$fieldLabelError.addClass('incomplete');
$fieldRowError.addClass('incomplete');
$formErMsgs = $formErMsgs + ' - ' + $fieldErrorDesc + ' <br/>';
}
break;
default:
//nothing
}
}
}
if (callback && typeof(callback) === "function") {
callback($formIsValid,$formErMsgs);
}
}
function validateEmail(email) {
var emailVal = email.val();
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
};
}
if (emailVal !== '') {
//return email.search(/.+@.+\..+/);
emailVal = emailVal.trim();
email.val(emailVal);
return emailVal.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
} else {
return -1;
}
}
Also see: Tab Triggers