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 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.
<h1>Ugh, Cross-Browser <code>number</code> Validation</h1>
<form>
<label>Try some non-numeric characters here: <input id="number" type="number" step="0.01"></label>
<dl>
<dt><code>input.value</code>:</dt>
<dd id="value"></dd>
<dt><code>validity.valid</code>:</dt>
<dd id="validity"></dd>
</dl>
</form>
<p>Browsers differ subtly on what they allow as <em>valid</em> values for HTML5 <code><input type="number"></code> fields. Not a big deal because you always validate and sanitize your incoming data on the backend too, right? (hopefully you do)</p>
<p>Unfortunately it’s also hard to add custom fallback JavaScript validation because the spec requires that browsers <a href="https://html.spec.whatwg.org/multipage/input.html#number-state-(type%3Dnumber)"><em>empty</em> the number input’s <code>value</code></a> if the input contains a string that can't be converted to a floating point number! This is a problem because some browsers treat commas as <em>numeric</em> while others don’t, and there’s no way to fix up the <code>value</code> with JS before it’s cleared out internally (as far as I can tell)…</p>
<p>If someone knows how to handle these cases more consistently, or has different test results, I’d love to hear it.</p>
<h2>Cross-Browser Test Results (April 2020)</h2>
<p>Update for 2020: there is more consistency now, with Firefox behaving in line with the decisions made by Safari and pre-Chromium Edge. The new Chromium-powered Edge understandably matches Chrome’s <code>number</code> handling. Assuming you no longer support IE11, that leaves only the two divergent paths to test (Chromium vs Non-Chromium), which at least is better than the state of things in 2018.</p>
<table>
<tr>
<th><span hidden>Browser</span></th>
<th>Allows Non-Numeric Input</th>
<th>Empties <code>value</code> when Invalid</th>
<th>Commas are Valid*</th>
<th>Allows trailing . point</th>
<th>Validation Styling</th>
<th>Validation Tooltips</th>
<th><code>validity.valid</code> Set</th>
</tr>
<tr>
<th scope="row">Chrome / Edge (Chromium)</th>
<td>No</td>
<td>Yes</td>
<td>No, can’t be entered</td>
<td>Yes (removes from value)</td>
<td>None</td>
<td>On submit</td>
<td>Yes</td>
</tr>
<tr>
<th scope="row">Firefox</th>
<td>Yes</td>
<td>If any non-numeric characters (including commas)</td>
<td>Not valid, but can be entered</td>
<td>Valid in <code>value</code> but only if 1st .</td>
<td>Red border</td>
<td>On submit</td>
<td>Yes</td>
</tr>
<tr>
<th scope="row">Safari (Desktop / iOS)</th>
<td>Yes</td>
<td>If any non-numeric characters (including commas)</td>
<td>Not valid, but can be entered on desktop</td>
<td>Valid in <code>value</code> but only if 1st .</td>
<td>None</td>
<td>On submit</td>
<td>Yes</td>
</tr>
<tr>
<th scope="row">Edge (Pre-Chromium)</th>
<td>Yes</td>
<td>If any non-numeric characters (including commas)</td>
<td>Not valid, but can be entered</td>
<td>Valid in <code>value</code> but only if 1st .</td>
<td>Red border on submit</td>
<td>On submit</td>
<td>Yes</td>
</tr>
<tr>
<th scope="row">IE11</th>
<td>Yes</td>
<td>No</td>
<td>Yes</td>
<td>Sure, use as many . as you like, <em>almost</em> everything seems to be considered <code>valid</code> input in IE11</td>
<td>Red border on submit</td>
<td>On submit, but only for certain cases</td>
<td>Only <code>false</code> when <code>step</code> is invalidated? Really broken.</td>
</tr>
</table>
<p><strong>* Note:</strong> commas <em>are</em> numeric in certain regions of the world, as they are often used as “thousands” delimiters and sometimes as the decimal separator symbol — here I’m mostly concerned with the lack of consistency in what can be entered between browsers. I haven’t tested for what happens when your page is specified as being in a language where commas are more significant (e.g. do browsers like Chrome allow them to be entered, then?)</p>
/* Nothing relevant to see here, just making this pen more readable */
body {
margin: 2rem;
}
form {
float: right;
margin-left: 1rem;
padding: 2rem;
background-color: #f0f0f0;
}
p {
max-width: 50em;
font-size: 18px;
line-height: 1.5;
}
label {
font-weight: bold;
}
input {
display: block;
margin-top: .5rem;
margin-bottom: 1rem;
width: 100%;
}
dl {
overflow: hidden;
margin-bottom: 0;
}
dt, dd {
float: left;
clear: right;
}
dt {
clear: both;
margin-bottom: .5rem;
}
dd {
margin-left: .5rem;
}
table {
border: 1px solid #000;
}
th, td {
border-bottom: 1px solid #ddd;
padding: .5rem 1rem;
vertical-align: top;
}
tr:last-child th,
tr:last-child td {
border-bottom: 0;
}
th[scope="row"] {
text-align: right;
}
td {
text-align: center;
}
var output = document.getElementById('value');
var validity = document.getElementById('validity');
document.getElementById('number').addEventListener('input', function(e) {
output.innerHTML = e.target.value;
validity.innerHTML = e.target.validity.valid;
});
Also see: Tab Triggers