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.
<input id="play" type="checkbox" /><label for="play">Set Scroll Position to Bottom</label>
<input id="reversal" type="checkbox" checked="checked" /><label for="reversal">Revert items to original position when scrolling up again</label>
<input id="overflow" type="checkbox" /><label for="overflow">Hide overflowing content (Firefox fix)</label>
<label for="play" class="controlbutton" id="forwardbutton"></label>
<label for="play" class="controlbutton" id="backwardbutton"></label>
<div id="browser">
<div id="progress"></div>
<main>
<h1>Lorem ipsum dolor</h1>
<p>Sit amet, consectetur adipiscing elit. Nunc lectus eros, dapibus quis egestas eget, commodo et leo.</p>
<p>Nulla est ipsum, elementum a neque a, maximus ultricies justo. Nulla tincidunt nec nulla eu malesuada. Etiam maximus massa nec auctor facilisis.</p>
<p>Henean sed commodo nibh, porttitor efficitur nisl. Integer porta, libero at facilisis consectetur, velit lorem posuere enim, vulputate vulputate massa ante sed diam.</p>
<h2>Morbi aliquam efficitur finibus.</h2>
<p>Phasellus ornare ultricies purus vel hendrerit. Suspendisse id ultricies nisi. Pellentesque rutrum ante ut rhoncus gravida.</p>
<p>Henean sed commodo nibh, porttitor efficitur nisl. Integer porta, libero at facilisis consectetur, velit lorem posuere enim, vulputate vulputate massa ante sed diam.</p>
<p>Nulla est ipsum, elementum a neque a, maximus ultricies justo. Nulla tincidunt nec nulla eu malesuada. Etiam maximus massa nec auctor facilisis.</p>
<h2>Morbi aliquam.</h2>
<p>Henean sed commodo nibh, porttitor efficitur nisl. Integer porta, libero at facilisis consectetur, velit lorem posuere enim, vulputate vulputate massa ante sed diam.</p>
<h3>Morbi aliquam efficitur finibus.</h3>
<p>Phasellus ornare ultricies purus vel hendrerit.</p>
</main>
<aside id="scrollbar">
<div id="thumb"></div>
</aside>
</div>
<details>
<summary>ℹ️</summary>
<dialog open>
<h1>About this Pen</h1>
<p>ℹ️ Scroll-Triggered Animations are animations that are triggered when scrolling past a certain position. Once triggered, these animations start and finish on their own, independent of any future scroll position.</p>
<p>👉 A typical example is content that flies/fades in as you scroll across the page. Once it's in view the animation starts and there's no stopping it.</p>
<p>☑️ In this demo you can't scroll yourself, but can fake a scroll using the “Set Scroll Position to Bottom” control at the top left.</p>
<p>🤔 Want to learn more about Scroll-Linked Animations / Scroll-Timeline? Check out these two articles:</p>
<ul>
<li><a href="https://brm.us/scroll-linked-animations-pt1" target="_top">Scroll-Linked Animations with <code>@scroll-timeline</code> (Part 1) — Introduction</a></li>
<li><a href="https://brm.us/scroll-linked-animations-pt2" target="_top">Scroll-Linked Animations with <code>@scroll-timeline</code> (Part 2) — Element-Based Offsets</a></li>
</ul>
<p>👀 More animations/visualizations: <a href="https://codepen.io/collection/AYoego" target="_top">https://codepen.io/collection/AYoego</a></p>
<p>👋 Built by Bramus! — <a href="https://www.twitter.com/bramus" target="_top">@bramus</a> - <a href="https://www.bram.us/" target="_top">https://www.bram.us/</a> </p>
</dialog>
</details>
:root {
--browser-size--ul: 35; /* in vmin */
--content-size--ul: calc(var(--browser-size--ul) * 3.1);
--thumb-size--ul: calc(
var(--browser-size--ul) / var(--content-size--ul) * var(--browser-size--ul)
);
--browser-size: calc(var(--browser-size--ul) * 1vmin);
--content-size: calc(var(--content-size--ul) * 1vmin);
--thumb-size: calc(var(--thumb-size--ul) * 1vmin);
--scrollbar-width: 1vmin;
}
html,
body {
width: 100vw;
height: 100vh;
display: grid;
place-items: center;
overflow: hidden;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
}
#browser {
height: var(--browser-size);
width: calc(var(--browser-size) * 4 / 3);
outline: 10px solid lightblue;
display: grid;
grid-template-columns: minmax(0, 1fr) var(--scrollbar-width);
grid-template-rows: minmax(0, 1fr);
grid-template-areas: "content scrollbar";
position: relative;
}
#browser main, #thumb {
transition: transform 3s linear, margin-top 3s linear;
}
main {
font-family: "BLOKK";
font-size: calc(2vmin + .2em);
padding: 1rem;
color: #999;
grid-area: content;
height: var(--content-size);
overflow: hidden;
outline: 1px solid grey;
z-index: -1;
will-change: transform;
}
main > * {
opacity: 0;
transition: all 0.2s ease-in-out;
transform: translateX(calc(var(--browser-size) * 4 / 3 * -0.8));
}
main > .in-view {
opacity: 1;
transform: translateX(0);
}
#scrollbar {
grid-area: scrollbar;
height: var(--browser-size);
align-self: start;
background: #ccc;
}
#scrollbar #thumb {
top: 0;
height: var(--thumb-size);
width: 100%;
background: #333;
border-radius: 9999px;
will-change: transform;
}
/* Controls */
.controlbutton {
position: fixed;
top: 50%;
left: 50%;
z-index: 2;
transform: translateX(calc(-50% + var(--offset))) translateY(calc(-4rem + (var(--browser-size) / -2)));
}
#forwardbutton {
--offset: 75%;
}
#backwardbutton {
--offset: -75%;
}
.controlbutton::after {
font-size: 2em;
cursor: pointer;
}
#forwardbutton::after {
content: '⏭';
}
#backwardbutton::after {
content: '⏮';
}
#play ~ #forwardbutton,
#play:checked ~ #backwardbutton {
opacity: 1;
pointer-events: auto;
}
#play:checked ~ #forwardbutton,
#play ~ #backwardbutton{
opacity: 0.5;
pointer-events: none;
}
/* Checkboxes */
#play,
#reversal,
#overflow,
#play + label,
#reversal + label,
#overflow + label {
position: fixed;
left: 1rem;
margin: 0;
}
#play + label,
#reversal + label,
#overflow + label {
left: 2.25rem;
white-space: nowrap;
}
#play,
#play + label {
top: 1rem;
}
#reversal,
#reversal + label {
top: 2.5rem;
}
#overflow,
#overflow + label {
top: 4rem;
}
/* Respond too checkboxes */
#play:checked ~ #browser main {
transform: translateY(calc(-100% + var(--browser-size)));
}
#play:checked ~ #browser #thumb {
/* 🪓 Hack: we animate margin-top to force repaints so that
our IntersectionObserver will kick into action …
*/
margin-top: calc(var(--browser-size) - var(--thumb-size));
}
#overflow:checked ~ #browser {
overflow: hidden;
}
/** Poor man's infobox */
details {
position: fixed;
bottom: 1em;
right: 1em;
font-size: clamp(1.25rem, 2vw, 2rem);
z-index: 9999;
}
dialog[open] {
position: fixed;
top: 10vmin;
right: 10vmin;
bottom: 10vmin;
left: 10vmin;
width: auto;
height: auto;
/* Extra CSS for Safari … */
background: #eee;
border: 0.25em solid lightblue;
padding: 1em;
overflow: auto;
}
details[open] > summary::after {
content: '';
position: fixed;
top: 0; right: 0; bottom: 0; left: 0;
background: rgba(0 0 0 / 0.5);
display: block;
}
/* Hide summary arrow …*/
details summary::-webkit-details-marker {
display: none;
}
details summary {
display: inline-block;
}
details summary::marker {
display: none;
}
window.addEventListener('DOMContentLoaded', () => {
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('in-view');
} else {
if (document.getElementById('reversal').checked) {
entry.target.classList.remove('in-view');
}
}
});
}, {
root: document.querySelector('#browser'),
rootMargin: '1000% 0% -20% 0%', // 🪓 Hack: Set top margin to 1000% so that elements which slide out from the top are considered to be intersecting. That way their .in-view class is retained.
});
// Track all sections that have an `id` applied
document.querySelectorAll('main > *').forEach((section) => {
observer.observe(section);
});
});
Also see: Tab Triggers