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.
<main class="layout-stack">
<div id="collection-1" class="collection layout-layer">
<scrolly-rail data-control-previous="collection-1-btn-previous" data-control-next="collection-1-btn-next">
<ul class="collection-list" role="list">
<li>
<x-card>
<figure></figure>
<div class="content">
<h2><a href="#">Herbie Carpenter</a></h2>
<dl class="meta">
<div class="role">
<dt>Role</dt>
<dd>Stunt Coordinator</dd>
</div>
<div>
<dt>Team</dt>
<dd>Stunts</dd>
</div>
<div>
<dt>Location</dt>
<dd>Los Angeles, CA</dd>
</div>
</dl>
</div>
</x-card>
</li>
<li>
<x-card>
<figure></figure>
<div class="content">
<h2><a href="#">Felicity Guest</a></h2>
<dl class="meta">
<div class="role">
<dt>Role</dt>
<dd>Production Designer</dd>
</div>
<div>
<dt>Team</dt>
<dd>Production</dd>
</div>
<div>
<dt>Location</dt>
<dd>Burbank, CA</dd>
</div>
</dl>
</div>
</x-card>
</li>
<li>
<x-card>
<figure></figure>
<div class="content">
<h2><a href="#">Doug Hammond</a></h2>
<dl class="meta">
<div class="role">
<dt>Role</dt>
<dd>Assistant Director</dd>
</div>
<div>
<dt>Team</dt>
<dd>Production</dd>
</div>
<div>
<dt>Location</dt>
<dd>Los Angeles, CA</dd>
</div>
</dl>
</div>
</x-card>
</li>
<li>
<x-card>
<figure></figure>
<div class="content">
<h2><a href="#">Deb Blanchard</a></h2>
<dl class="meta">
<div class="role">
<dt>Role</dt>
<dd>Director</dd>
</div>
<div>
<dt>Team</dt>
<dd>Production</dd>
</div>
<div>
<dt>Location</dt>
<dd>New York, NY</dd>
</div>
</dl>
</div>
</x-card>
</li>
<li>
<x-card>
<figure></figure>
<div class="content">
<h2><a href="#">Patty Verona</a></h2>
<dl class="meta">
<div class="role">
<dt>Role</dt>
<dd>Composer</dd>
</div>
<div>
<dt>Team</dt>
<dd>Music</dd>
</div>
<div>
<dt>Location</dt>
<dd>Portland, ME</dd>
</div>
</dl>
</div>
</x-card>
</li>
<li>
<x-card>
<figure></figure>
<div class="content">
<h2><a href="#">Gregory Brown</a></h2>
<dl class="meta">
<div class="role">
<dt>Role</dt>
<dd>Executive Producer</dd>
</div>
<div>
<dt>Team</dt>
<dd>Production</dd>
</div>
<div>
<dt>Location</dt>
<dd>Burbank, CA</dd>
</div>
</dl>
</div>
</x-card>
</li>
<li>
<x-card>
<figure></figure>
<div class="content">
<h2><a href="#">Beverly Mirren-Henderson</a></h2>
<dl class="meta">
<div class="role">
<dt>Role</dt>
<dd>Accounting</dd>
</div>
<div>
<dt>Team</dt>
<dd>Finance</dd>
</div>
<div>
<dt>Location</dt>
<dd>Seattle, WA</dd>
</div>
</dl>
</div>
</x-card>
</li>
<li>
<x-card>
<figure></figure>
<div class="content">
<h2><a href="#">Adam East</a></h2>
<dl class="meta">
<div class="role">
<dt>Role</dt>
<dd>Costume Designer</dd>
</div>
<div>
<dt>Team</dt>
<dd>Design</dd>
</div>
<div>
<dt>Location</dt>
<dd>Burbank, CA</dd>
</div>
</dl>
</div>
</x-card>
</li>
<li>
<x-card>
<figure></figure>
<div class="content">
<h2><a href="#">Ellie Morelli</a></h2>
<dl class="meta">
<div class="role">
<dt>Role</dt>
<dd>Animator</dd>
</div>
<div>
<dt>Team</dt>
<dd>Design</dd>
</div>
<div>
<dt>Location</dt>
<dd>Burbank, CA</dd>
</div>
</dl>
</div>
</x-card>
</li>
<li>
<x-card>
<figure></figure>
<div class="content">
<h2><a href="#">Devon Williams</a></h2>
<dl class="meta">
<div class="role">
<dt>Role</dt>
<dd>Lightning Technician</dd>
</div>
<div>
<dt>Team</dt>
<dd>Production</dd>
</div>
<div>
<dt>Location</dt>
<dd>New York, NY</dd>
</div>
</dl>
</div>
</x-card>
</li>
<li>
<x-card>
<figure></figure>
<div class="content">
<h2><a href="#">Patty Verona</a></h2>
<dl class="meta">
<div class="role">
<dt>Role</dt>
<dd>Composer</dd>
</div>
<div>
<dt>Team</dt>
<dd>Music</dd>
</div>
<div>
<dt>Location</dt>
<dd>Portland, ME</dd>
</div>
</dl>
</div>
</x-card>
</li>
<li>
<x-card>
<figure></figure>
<div class="content">
<h2><a href="#">Gregory Brown</a></h2>
<dl class="meta">
<div class="role">
<dt>Role</dt>
<dd>Executive Producer</dd>
</div>
<div>
<dt>Team</dt>
<dd>Production</dd>
</div>
<div>
<dt>Location</dt>
<dd>Burbank, CA</dd>
</div>
</dl>
</div>
</x-card>
</li>
</ul>
</scrolly-rail>
<button data-bound class="btn-scrolly-rail btn-scrolly-rail--previous animate-appearance" id="collection-1-btn-previous">
<span class="visually-hidden">Scroll previous items into view</span>
<svg class="icon" aria-hidden="true" width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M6.85355 3.14645C7.04882 3.34171 7.04882 3.65829 6.85355 3.85355L3.70711 7H12.5C12.7761 7 13 7.22386 13 7.5C13 7.77614 12.7761 8 12.5 8H3.70711L6.85355 11.1464C7.04882 11.3417 7.04882 11.6583 6.85355 11.8536C6.65829 12.0488 6.34171 12.0488 6.14645 11.8536L2.14645 7.85355C1.95118 7.65829 1.95118 7.34171 2.14645 7.14645L6.14645 3.14645C6.34171 2.95118 6.65829 2.95118 6.85355 3.14645Z" fill="currentColor" fill-rule="evenodd" clip-rule="evenodd"></path>
</svg>
</svg>
</button>
<button class="btn-scrolly-rail btn-scrolly-rail--next animate-appearance" id="collection-1-btn-next">
<span class="visually-hidden">Scroll next items into view</span>
<svg class="icon" aria-hidden="true" width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M8.14645 3.14645C8.34171 2.95118 8.65829 2.95118 8.85355 3.14645L12.8536 7.14645C13.0488 7.34171 13.0488 7.65829 12.8536 7.85355L8.85355 11.8536C8.65829 12.0488 8.34171 12.0488 8.14645 11.8536C7.95118 11.6583 7.95118 11.3417 8.14645 11.1464L11.2929 8H2.5C2.22386 8 2 7.77614 2 7.5C2 7.22386 2.22386 7 2.5 7H11.2929L8.14645 3.85355C7.95118 3.65829 7.95118 3.34171 8.14645 3.14645Z" fill="currentColor" fill-rule="evenodd" clip-rule="evenodd"></path>
</svg>
</button>
</div>
<div id="collection-2" class="collection layout-stack">
<div class="scrolly-rail-wrapper">
<scrolly-rail data-control-previous="collection-2-btn-previous" data-control-next="collection-2-btn-next">
<ol class="collection-list" role="list">
<li class="placeholder-item">1</li>
<li class="placeholder-item">2</li>
<li class="placeholder-item">3</li>
<li class="placeholder-item">4</li>
<li class="placeholder-item">5</li>
<li class="placeholder-item">6</li>
<li class="placeholder-item">7</li>
<li class="placeholder-item">8</li>
<li class="placeholder-item">9</li>
<li class="placeholder-item">10</li>
<li class="placeholder-item">11</li>
<li class="placeholder-item">12</li>
<li class="placeholder-item">13</li>
<li class="placeholder-item">14</li>
<li class="placeholder-item">15</li>
<li class="placeholder-item">16</li>
<li class="placeholder-item">17</li>
<li class="placeholder-item">18</li>
<li class="placeholder-item">19</li>
<li class="placeholder-item">20</li>
<li class="placeholder-item">21</li>
<li class="placeholder-item">22</li>
<li class="placeholder-item">23</li>
<li class="placeholder-item">24</li>
</ol>
</scrolly-rail>
</div>
<div class="scrolly-rail-controls">
<button data-bound class="btn-scrolly-rail btn-scrolly-rail--previous animate-fade" id="collection-2-btn-previous">
<span class="visually-hidden">Scroll previous items into view</span>
<svg class="icon" aria-hidden="true" width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M6.85355 3.14645C7.04882 3.34171 7.04882 3.65829 6.85355 3.85355L3.70711 7H12.5C12.7761 7 13 7.22386 13 7.5C13 7.77614 12.7761 8 12.5 8H3.70711L6.85355 11.1464C7.04882 11.3417 7.04882 11.6583 6.85355 11.8536C6.65829 12.0488 6.34171 12.0488 6.14645 11.8536L2.14645 7.85355C1.95118 7.65829 1.95118 7.34171 2.14645 7.14645L6.14645 3.14645C6.34171 2.95118 6.65829 2.95118 6.85355 3.14645Z" fill="currentColor" fill-rule="evenodd" clip-rule="evenodd"></path>
</svg>
</svg>
</button>
<button class="btn-scrolly-rail btn-scrolly-rail--next animate-fade" id="collection-2-btn-next">
<span class="visually-hidden">Scroll next items into view</span>
<svg class="icon" aria-hidden="true" width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M8.14645 3.14645C8.34171 2.95118 8.65829 2.95118 8.85355 3.14645L12.8536 7.14645C13.0488 7.34171 13.0488 7.65829 12.8536 7.85355L8.85355 11.8536C8.65829 12.0488 8.34171 12.0488 8.14645 11.8536C7.95118 11.6583 7.95118 11.3417 8.14645 11.1464L11.2929 8H2.5C2.22386 8 2 7.77614 2 7.5C2 7.22386 2.22386 7 2.5 7H11.2929L8.14645 3.85355C7.95118 3.65829 7.95118 3.34171 8.14645 3.14645Z" fill="currentColor" fill-rule="evenodd" clip-rule="evenodd"></path>
</svg>
</button>
</div>
</div>
</main>
@layer base, patterns, components, utils;
@layer components {
/* Base scrolly-rail component styles */
scrolly-rail {
display: flex;
overflow-x: auto;
overscroll-behavior-x: contain;
scroll-snap-type: x mandatory;
@media (prefers-reduced-motion: no-preference) {
scroll-behavior: smooth;
}
}
}
@layer base {
:root {
--text-1: #0f172a;
--text-2: #64748b;
--surface-1: #ffffff;
--surface-2: #f8fafc;
--surface-3: #f1f5f9;
--surface-4: #cbd5e1;
--surface-5: #64748b;
--surface-translucent: hsla(210 40% 98% / 0.8);
--border-color: var(--surface-5);
--border-color-subtle: var(--surface-4);
--border-color-highlight: var(--surface-3);
--scrollbar-color: var(--text-2) var(--surface-3);
--space-xs: 0.25rem;
--space-s: 0.5rem;
--space-m: 1rem;
--space-l: 2rem;
--space-xl: 3rem;
--ease-out-quart: cubic-bezier(0.25, 1, 0.5, 1);
--ease-out-back: cubic-bezier(0.34, 1.56, 0.64, 1);
}
* {
box-sizing: border-box;
margin: 0;
}
html,
body {
height: 100%;
}
body {
display: grid;
margin: 0;
font-family: system-ui, sans-serif;
color: var(--text-1);
background-color: var(--surface-2);
}
:where(ul, ol)[role="list"] {
list-style: none;
padding-inline-start: unset;
}
}
@layer utils {
.visually-hidden {
clip: rect(0 0 0 0);
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
}
@layer patterns {
.layout-stack > * + * {
margin-block-start: var(--stack-space, 1rem);
}
.layout-layer {
display: grid;
grid-template-areas: "box";
> * {
grid-area: box;
}
}
}
@layer components {
.icon {
--_size: var(--icon-size, 1em);
width: var(--_size);
height: var(--_size);
}
/* Custom component styles */
scrolly-rail {
--_block-gutter: var(--space-m);
--_inline-gutter: var(--space-l);
scroll-padding-inline-start: var(--_inline-gutter);
scrollbar-color: var(--scrollbar-color);
padding-inline: var(--_inline-gutter);
padding-block: var(--_block-gutter);
margin-block: calc(var(--_block-gutter) * -1);
}
.btn-scrolly-rail {
--icon-size: 1.25rem;
--x-offset-direction: 1;
--_size: calc(var(--icon-size, 1rem) * 2);
--_x-offset: calc(6px * var(--x-offset-direction));
position: relative;
overflow: hidden;
display: grid;
place-items: center;
place-content: center;
inline-size: var(--_size);
block-size: var(--_size);
border-radius: 50%;
color: var(--border-color);
border: 1px solid currentcolor;
background: var(--surface-translucent);
backdrop-filter: blur(2px);
box-shadow: hsla(0 0% 0% / 0.1) 0 4px 8px -3px;
cursor: pointer;
touch-action: manipulation;
.icon {
transition: translate 300ms var(--ease-out-back);
}
&:not([data-bound]):active .icon {
translate: calc(-10% * var(--x-offset-direction)) 0;
}
}
.btn-scrolly-rail[class*="animate"] {
transition: 225ms var(--ease-out-quart);
}
.btn-scrolly-rail.animate-fade {
transition-property: border-color, color, box-shadow;
&[data-bound] {
color: var(--surface-4);
box-shadow: none;
}
}
.btn-scrolly-rail.animate-appearance {
transition-property: display, opacity, translate, scale;
transition-behavior: allow-discrete;
&[data-bound] {
display: none;
opacity: 0;
scale: 0.5;
}
@starting-style {
opacity: 0;
translate: var(--_x-offset) 0;
.icon {
translate: calc(100% * var(--x-offset-direction)) 0;
}
}
}
x-card {
position: relative;
display: grid;
grid-template-columns: auto 1fr;
gap: 1rem;
padding: 1rem;
background: linear-gradient(var(--surface-1), var(--surface-1)) padding-box,
linear-gradient(
135deg,
var(--border-color-subtle),
var(--border-color-highlight) 40% 60%,
var(--border-color-subtle)
)
border-box;
border: 1px solid transparent;
box-shadow: hsla(0 0% 0% / 0.06) 0 4px 6px -3px;
border-radius: 0.65rem;
&:focus-within {
outline: 5px auto Highlight;
outline: 5px auto -webkit-focus-ring-color;
}
.content {
display: grid;
gap: 0.4rem;
}
h2 {
grid-row: 1;
margin: 0;
font-size: 1.2em;
font-weight: 500;
letter-spacing: -0.01em;
a {
color: currentcolor;
text-decoration: none;
outline: none;
}
}
figure {
overflow: hidden;
aspect-ratio: 1;
border-radius: 50%;
inline-size: 4rem;
margin: 0;
background: var(--border-color-highlight);
border: 1px solid var(--border-color-subtle);
}
.meta {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(min(8rem, 100%), 1fr));
justify-items: start;
gap: 0.4rem;
line-height: 1.2;
.role {
grid-column: 1 / -1;
}
dt {
font-size: 0.8em;
margin-block-end: 0.125em;
color: var(--text-2);
}
}
}
}
main {
--stack-space: var(--space-xl);
margin-block: auto;
padding-block: var(--space-xl) var(--space-m);
}
.collection-list {
display: flex;
}
.collection-list > li {
display: grid;
scroll-snap-align: start;
}
#collection-1 {
--control-margin-offset: var(--space-s);
x-card {
inline-size: clamp(20rem, 1rem + 50vw, 25rem);
}
.collection-list {
gap: var(--space-s);
}
.btn-scrolly-rail--previous {
place-self: center start;
margin-inline-start: var(--control-margin-offset);
}
.btn-scrolly-rail--next {
--x-offset-direction: -1;
place-self: center end;
margin-inline-end: var(--control-margin-offset);
}
}
#collection-2 {
position: relative;
inline-size: min(50rem, 100% - var(--space-l) * 2);
margin-inline: auto;
.scrolly-rail-wrapper {
display: grid;
padding-block: var(--space-m);
border-inline: 1px dashed var(--border-color);
}
.collection-list {
gap: var(--space-m);
}
.placeholder-item {
display: grid;
place-items: center;
padding: 1rem;
inline-size: 8rem;
aspect-ratio: 4 / 5;
border: 1px solid var(--border-color);
border-radius: 0.4rem;
}
.scrolly-rail-controls {
--stack-space: var(--space-s);
display: flex;
gap: var(--space-s);
inline-size: fit-content;
margin-inline: auto;
}
.btn-scrolly-rail--previous {
margin-inline-start: var(--control-margin-offset);
}
.btn-scrolly-rail--next {
--x-offset-direction: -1;
margin-inline-end: var(--control-margin-offset);
}
}
class ScrollyRail extends HTMLElement {
static register(tag = "scrolly-rail") {
if ("customElements" in window) {
customElements.define(tag, this);
}
}
get items() {
const els = [...this.children];
let arr;
/**
* Check if direct child is a wrapper element that contains children.
* Otherwise, return direct children.
*/
if (els.length === 1 && els[0].children.length > 0) {
arr = [...els[0].children];
} else {
arr = [...els];
}
return arr;
}
connectedCallback() {
if (!this.items) return;
this.prevBtn = document.getElementById(this.dataset.controlPrevious);
this.nextBtn = document.getElementById(this.dataset.controlNext);
if (this.prevBtn) {
this.observeScrollStart();
this.handleClickPrevious = () => this.goToPrevious();
this.prevBtn.addEventListener("click", this.handleClickPrevious);
}
if (this.nextBtn) {
this.observeScrollEnd();
this.handleClickNext = () => this.goToNext();
this.nextBtn.addEventListener("click", this.handleClickNext);
}
this.resizeObserver = new ResizeObserver(() => this.init());
this.resizeObserver.observe(this);
}
disconnectedCallback() {
this.resizeObserver.disconnect();
if (this.prevBtn) {
this.scrollStartObserver.disconnect();
this.prevBtn.removeEventListener("click", this.handleClickPrevious);
}
if (this.nextBtn) {
this.scrollEndObserver.disconnect();
this.nextBtn.removeEventListener("click", this.handleClickNext);
}
}
init() {
this.bounds = this.getBounds();
this.step = this.getVisibleItemCount();
}
getBounds() {
const { left, right } = this.getBoundingClientRect();
return {
left: Math.round(left),
right: Math.round(right)
};
}
getVisibleItemCount() {
let count = 0;
for (let i = 0; i < this.items.length; i++) {
const { left, right } = this.items[i].getBoundingClientRect();
if (Math.round(right) > this.bounds.right) break;
if (Math.round(left) >= this.bounds.left) count++;
}
return count;
}
handleScrollIntoView(item, sibling) {
/*
* If singular visible item is selected and overflows, navigate to a sibling item.
*/
const target = this.step > 0 ? item : sibling;
target?.scrollIntoView({
block: "nearest",
inline: "start"
});
}
goToPrevious() {
for (let i = this.items.length - 1; i >= 0; i--) {
const { left } = this.items[i].getBoundingClientRect();
if (Math.round(left) < this.bounds.left) {
/*
* Find the target item to align at start position.
* Prevent the item at current index from overflowing.
* Ensure target item index is always 0 or greater.
*/
const target = this.items[Math.max(i - this.step + 1, 0)];
this.handleScrollIntoView(target, target.previousElementSibling);
break;
}
}
}
goToNext() {
for (let i = 0; i < this.items.length; i++) {
const item = this.items[i];
const { right } = item.getBoundingClientRect();
if (Math.round(right) > this.bounds.right) {
this.handleScrollIntoView(item, item.nextElementSibling);
break;
}
}
}
observeScrollStart() {
const firstItem = this.items[0];
this.scrollStartObserver = this.setupObserver(this.prevBtn, firstItem);
this.scrollStartObserver.observe(firstItem);
}
observeScrollEnd() {
const lastItem = this.items[this.items.length - 1];
this.scrollEndObserver = this.setupObserver(this.nextBtn, lastItem);
this.scrollEndObserver.observe(lastItem);
}
/*
* Observe scroll bounds of first and/or last element.
* Toggle attribute on a button control when its respective scroll bound is reached.
*/
setupObserver(btn, item) {
const callback = ([entry]) => {
btn.toggleAttribute(
"data-bound",
entry.target === item && entry.isIntersecting
);
};
const options = {
root: this,
threshold: 1
};
return new IntersectionObserver(callback, options);
}
}
ScrollyRail.register();
Also see: Tab Triggers