Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>

              
            
!

CSS

              
                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;
}

              
            
!

JS

              
                /*
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)

              
            
!
999px

Console