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.
<h1>Accessible, drag & drop, touch-friendly, sortable list</h1>
<p>Mouse and touch: drag and drop to reorder. Keyboard and text-based: change the number input and hit enter or the update button.</p>
<form id="sort-it">
<ol>
<li>This is item #1
<label for="custom-number-1">New order:</label>
<input id="custom-number-1" name="custom-number-1" type="number" min="1">
</li>
<li>This is item #2
<label for="custom-number-2">New order:</label>
<input id="custom-number-2" name="custom-number-2" type="number" min="1">
</li>
<li>This is item #3
<label for="custom-number-3">New order:</label>
<input id="custom-number-3" name="custom-number-3" type="number" min="1">
</li>
<li>This is item #4
<label for="custom-number-4">New order:</label>
<input id="custom-number-4" name="custom-number-4" type="number" min="1">
</li>
<li>This is item #5
<label for="custom-number-5">New order:</label>
<input id="custom-number-5" name="custom-number-5" type="number" min="1">
</li>
</ol>
<input type="submit" id="manual-sort" name="manual-sort" value="Update">
</form>
<p>Requires jQuery and this plugin: https://johnny.github.io/jquery-sortable/js/jquery-sortable-min.js</p>
body {font-family: sans-serif;}
input { font-size: 1em; } /* prevent zoom in mobile */
ol {
/* list style is faked with number inputs */
list-style: none;
padding: 0;
}
li {
position: relative;
min-height: 1em;
cursor: move;
padding: .5em .5em .5em 2.5em;
background: #eee;
border: 1px solid #ccc;
margin: .25em 0;
border-radius: .25em;
max-width: 14em;
}
li input {
/* Move these to visually fake the ol numbers */
position: absolute;
width: 1.75em;
left: .25em;
top: .25em;
border: 0;
text-align: center;
background: transparent
}
li label {
/* visually hidden offscreen so it still benefits screen readers */
position: absolute;
left: -9999px;
}
/* sortable plugin styles when dragged */
.dragged {
position: absolute;
opacity: 0.5;
z-index: 2000;
}
li.placeholder {
position: relative;
background: purple;
}
$(function(){
// uses https://johnny.github.io/jquery-sortable/js/jquery-sortable-min.js
$('#sort-it ol').sortable({
onDrop: function(item) {
$(item).removeClass("dragged").removeAttr("style");
$("body").removeClass("dragging");
getInitialOrder('#sort-it li');
}
});
getInitialOrder('#sort-it li');
//bind stuff to number inputs
$('#sort-it ol input[type="number"]').focus(function(){
$(this).select();
}).change(function(){
updateAllNumbers($(this), '#sort-it input');
}).keyup(function(){
updateAllNumbers($(this), '#sort-it input');
});
//bind to form submission
$('#sort-it').submit(function(e){
reorderItems('#sort-it li', '#sort-it ol');
e.preventDefault();
})
}); // end doc ready
function getInitialOrder(obj){
var num = 1;
$(obj).each(function(){
//set object initial order data based on order in DOM
$(this).find('input[type="number"]').val(num).attr('data-initial-value', num);
num++;
});
$(obj).find('input[type="number"]').attr('max', $(obj).length); //give it an html5 max attr based on num of objects
}
function updateAllNumbers(currObj, targets){
var delta = currObj.val() - currObj.attr('data-initial-value'), //if positive, the object went down in order. If negative, it went up.
c = parseInt(currObj.val(), 10), //value just entered by user
cI = parseInt(currObj.attr('data-initial-value'), 10), //original object val before change
top = $(targets).length;
//if the user enters a number too high or low, cap it
if(c > top){
currObj.val(top);
}else if(c < 1){
currObj.val(1);
}
$(targets).not($(currObj)).each(function(){ //change all the other objects
var v = parseInt($(this).val(), 10); //value of object changed
if (v >= c && v < cI && delta < 0){ //object going up in order pushes same-numbered and in-between objects down
$(this).val(v + 1);
} else if (v <= c && v > cI && delta > 0){ //object going down in order pushes same-numbered and in-between objects up
$(this).val(v - 1);
}
}).promise().done(function(){
//after all the fields update based on new val, set their data element so further changes can be tracked
//(but ignore if no value given yet)
$(targets).each(function(){
if($(this).val() !== ""){
$(this).attr('data-initial-value', $(this).val());
}
});
});
}
function reorderItems(things, parent){
for(var i = 1; i <= $(things).length; i++){
$(things).each(function(){
var x = parseInt($(this).find('input').val(), 10);
if(x === i){
$(this).appendTo(parent);
}
});
}
}
Also see: Tab Triggers