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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
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.
If the stylesheet 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 CSS 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.
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.
<main>
<h1>Prevent orphan list item with pure CSS</h1>
<h2 id="code" hidden>CSS code (excerpt)</h2>
<style>ol {
width: calc(4 * var(--item-width));
} /* Columns: 4 ↑ … therefore 4n ↓ */
li:first-child:nth-last-child(4n + 1) {
margin-left: var(--item-width);
}</style>
<h2 id="result">Result</h2>
<div>
<ol>
<li>flex item</li>
<li>flex item</li>
<li>flex item</li>
<li>flex item</li>
<li>flex item</li>
<li>flex item</li>
<li>flex item</li>
<li>flex item</li>
<li>flex item</li>
<li>flex item</li>
<li>flex item</li>
<li>flex item</li>
<li>flex item</li>
</ol>
</div>
<form method="GET" action="playground" aria-labelledby="settings" hidden>
<h2 id="settings">Settings</h2>
<details><summary>Hint</summary><p id="note">The CSS item shift technique lays dormant unless there is more than one row and a single item in the last row. To see the effect, you have to set the number of list items to “Orphan in last row”. The other options are for comparision only.</p></details>
<div>
<label for="columns">No. of columns:</label>
<select id="columns" name="columns">
<option>3</option>
<option selected>4</option>
<option>6</option>
<option>8</option>
</select>
</div>
<div>
<label for="items">No. of list items:</label>
<select id="items" name="items" aria-describedby="note">
<option value="1,-1">Fewer than no. of columns</option>
<option value="3,1" selected>Orphan in last row</option>
<option value="3,2">Two items in last row</option>
<option value="4,-1">Almost filling four rows</option>
<option value="4,0">Exactly four rows</option>
</select>
</div>
<div>
<label for="style">Orphan prevention technique:</label>
<select id="style" name="style">
<option value="t1" selected>Offset first item</option>
<option value="tf">Shorten first row</option>
<option value="tp">Shorten penultimate row</option>
<option value="tc">Custom</option>
</select>
</div>
<div>
<label for="active">Apply selected technique:</label>
<input id="active" name="active" type="checkbox" checked value="on">
</div>
<div>
<button>Update</button>
</div>
</form>
</main>
ol {
--item-width: 2rem;
display: flex;
flex-wrap: wrap;
}
li {
flex: 0 0 var(--item-width);
}
/*
Less important CSS below
*/
*,
*::before,
*::after {
box-sizing: inherit;
}
html {
box-sizing: border-box;
color: #111;
font-family: sans-serif;
line-height: 1.5;
}
body {
margin: 0;
padding: 0;
}
main {
position: relative;
margin: 0 auto;
padding: 1rem 1rem 4.5rem;
max-width: 36rem;
}
@media (min-width: 48em) and (max-width: 71.95em) {
main {
margin-right: 18rem;
margin-left: 1.5rem;
}
}
h1,
form h2 {
margin-top: 0;
}
h1 {
border-bottom: 1px solid;
font-size: 150%;
}
h2 {
margin: 2rem 0 1rem;
font-size: inherit;
}
form {
position: relative;
margin-top: 2rem;
padding: 1rem;
background-color: #ccc;
box-shadow: inset 0 .0625rem .125rem #3331;
}
@media (min-width: 48em) {
form {
position: absolute;
top: 4rem;
left: 100%;
margin: 0 0 0 1.5rem;
width: 14rem;
}
}
form div {
margin-top: .5rem;
}
details,
label {
font-size: .75rem;
}
select {
width: 100%;
}
details {
position: absolute;
top: 1.125rem;
right: 1.125rem;
}
#note {
position: absolute;
right: -.5rem;
width: 14rem;
margin: 0;
padding: 1rem;
box-shadow: 0 .125rem .25rem -1px #3338;
background-color: #111;
color: #fff;
text-align: justify;
}
#result + div {
display: flex;
place-items: center;
min-height: 12rem;
border: 1px dashed;
}
h2[hidden],
body style {
display: block !important;
}
body style {
overflow-x: auto;
overflow-y: visible;
padding: 1.5rem 1rem;
-moz-tab-size: 2;
tab-size: 2;
}
body style,
li {
background-color: #111;
color: #fff;
font-family: monospace;
line-height: 1.75;
white-space: pre;
}
ol {
margin: 0 auto;
padding: 0;
list-style-position: inside;
}
li {
overflow: hidden;
margin: 0;
padding: .25rem 1rem;
box-shadow: inset 0 0 0 .0625rem #fff;
}
li:nth-child(9) ~ li {
text-indent: -1ch;
}
/*
JavaScript is not required for the actual example to work,
only to play with different the settings.
*/
(function (d) {
'use strict;'
if (!String.prototype.repeat) return;
var update = function (event) {
if (event) {
event.preventDefault();
if (event.target.id === 'style') {
d.querySelector('#active').checked = true;
}
}
var list = d.querySelector('ol');
list.innerHTML = '';
var columns = parseInt(d.querySelector('#columns').value, 10);
var calc = d.querySelector('#items').value.split(',');
var items = parseInt(calc[0], 10) * columns + parseInt(calc[1], 10);
var code = {
ol: `ol {\n\twidth: calc(${columns} * var(--item-width));\n} /* Columns: ${columns} ↑ `,
tx: `… therefore ${columns}n ↓ */\nli:first-child:nth-last-child(${columns}n + 1) `,
t1: `{\n\tmargin-left: var(--item-width);\n}`,
tf: `~ li:nth-child(${columns - 1}) {\n\tmargin-right: var(--item-width);\n}`,
tp: `~ li:nth-last-child(3) {\n\tmargin-right: var(--item-width);\n}`,
tc: `~ li:nth-child(${columns + 2}) {\n\tmargin-left: var(--item-width);\n} /* Be creative and apply margins however you like */`,
e0: '\n/* no orphan element: no item shift */',
e1: '\n/* list with orphan element: offset is visible */'
};
var effect = d.querySelector('#active').checked && d.querySelector('#style').value;
d.querySelector('body style').innerHTML = [code.ol].concat(effect ? [code.tx, code[effect]] : '*/\n\n\n\n').join('');
list.innerHTML = '<li>flex item</li>'.repeat(items);
};
d.querySelector('form').addEventListener('change', update);
update();
d.querySelector('form div:last-child').setAttribute('hidden', true);
d.querySelector('form').removeAttribute('hidden');
})(document)
Also see: Tab Triggers