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.
<header class="site-header">
<h1 class="sr-only">Scrollnapping animations</h1>
<!-- Because Chrome crashes if an overflow is set on the fieldset while a named timeline is on a child element -->
<!-- ¯\_(ツ)_/¯ -->
<div class="fieldset-wrapper">
<fieldset>
<legend class="sr-only">Effects</legend>
<!-- radio buttons for each of the @keyframes above -->
<input type="radio" id="blink-effect" name="effect" value="blink" checked class="sr-only">
<label for="blink-effect">Blink</label>
<input type="radio" id="horizontal-scroll-effect" name="effect" value="horizontal-scroll" class="sr-only">
<label for="horizontal-scroll-effect">Horizontal scroll</label>
<input type="radio" id="backwards-scroll-effect" name="effect" value="backwards-scroll" class="sr-only">
<label for="backwards-scroll-effect">Backwards scroll</label>
<input type="radio" id="zoom-scroll-effect" name="effect" value="zoom-scroll" class="sr-only">
<label for="zoom-scroll-effect">Zoom scroll</label>
</fieldset>
</div>
<nav>
<ul class="indicator">
<li><a href="#snapping"><span class="sr-only">Snapping</span></a></li>
<li><a href="#scrolling"><span class="sr-only">Scrolling</span></a></li>
<li><a href="#layout"><span class="sr-only">Layout</span></a></li>
<li><a href="#transition"><span class="sr-only">Transition</span></a></li>
<li><a href="#caveats"><span class="sr-only">Caveats</span></a></li>
</ul>
</nav>
</header>
<main>
<section id="snapping" class="section">
<div class="content">
<h2><strong>First</strong>, we set up the <em>snapping</em> points</h2>
<div class="text">
<img src="https://assets.codepen.io/197359/flower-white.png" alt="">
<p>We set the scrolling element, in this case our <code class="selector">HTML</code> element, to forcibly snap to the Y axis by using <code class="property">scroll-snap-type: y mandatory</code>.</p>
<p>And then we set <code class="selector">section</code> as the snapping elements by using <code class="property">scroll-snap-align: start</code>.</p>
</div>
</div>
</section>
<section id="scrolling" class="section">
<div class="content">
<h2><strong>Next</strong>, we set up the <em>scrolling</em> animation</h2>
<div class="text">
<img src="https://assets.codepen.io/197359/flower-yellow.png" alt="">
<p>We track the <code class="property">view()</code> position of the <code class="selector">section</code> elements using the named timeline <code class="property">view-timeline: --section;</code>. We had previously set the <code class="property">timeline-scope: --section</code> up in our <code class="selector">HTML</code> element, so we can access it from anywhere in the document.</p>
<p>We animate the <code class="selector">.content</code> children using <code class="property">animation-timeline: --section;</code>. The <code class="selector">.content</code> element will now animate based on its parent <code class="selector">section</code>'s position. This is important due to how we're handling the layout in the next section.</p>
</div>
</div>
</section>
<section id="layout" class="section">
<div class="content">
<h2><strong>Then</strong>, we position a <em>fixed</em> layout</h2>
<div class="text">
<img src="https://assets.codepen.io/197359/flower-blue.png" alt="">
<p>We set the <code class="selector">.content</code> elements to <code class="property">position: fixed</code>, so they're removed from the normal document flow and stack on top of each other, giving them a solid background, so only one is visible at a time.</p>
<p>Their parent <code class="selector">section</code>s are positioned as normal in the layer below, taking up space, scroll-snapping, and powering the <code class="property">animation-timeline</code>.</p>
</div>
</div>
</section>
<section id="transition" class="section">
<div class="content">
<h2><strong>Finally</strong>, we create the <em>transition</em> effects</h2>
<div class="text">
<img src="https://assets.codepen.io/197359/flower-red.png" alt="">
<p>By setting the <code class="selector">.content</code> elements to <code class="property">position: fixed</code>, we can now transition between them without a visible scrolling movement.</p>
<p>We create a normal <code class="selector">@keyframe</code> animation to our liking to transition between them. Check the navigation menu to see different effects.</p>
</div>
</div>
</section>
<section id="caveats" class="section">
<div class="content">
<h2><strong>Caveats</strong></h2>
<div class="text">
<img src="https://assets.codepen.io/197359/flower-purple.png" alt="">
<ul>
<li>Scrolling animations are not currently available in Firefox. This demo is using a polyfill.</li>
<li>This layout is fragile due to the use of <code class="property">position: fixed</code>. You need to carefully manage stacking contexts.</li>
<li>Snapping points have their own caveats, such as content taller than the viewport becoming inaccessible, along with the general annoyance of scrolljacking.</li>
<li>The <code class="selector">blink</code> effect uses the <code class="property">contrast()</code> filter, which modifies the colors of the entire section. Thus, the background is set to black (or white), ensuring that it appears unchanged during transitions due to already being at maximum contrast.</li>
</ul>
</div>
</div>
</section>
</main>
<footer>
<p>That's it <span class="emoji">🌸</span></p>
</footer>
@layer base, rhythm, layout, components, default, overwrites;
html {
/* Create a snapping rule on the html element */
scroll-snap-type: y mandatory;
/* Create a timeline scope, so we can target any element on the page */
timeline-scope: --section, --main, --site-header;
}
/* We'll want to match these colors for the best melty effect */
/* But play around with them (and add a color to section) to see happens */
body,
.content {
background-color: var(--color-background, black);
}
main {
view-timeline: --main;
}
.section {
/* Creating a snapping rule on the section element */
scroll-snap-align: start;
scroll-snap-stop: always;
/* Attach the timeline to the section element*/
view-timeline: --section;
/* Set each section to the full dynamic height of the viewport */
height: 100dvh;
}
.content {
/* Fix the content, so it doesn't scroll with the section */
overflow: hidden;
position: fixed;
inset: 0;
/* Animate the content based on the section scrolling */
--contrast: 4;
--blur: 0.5rem;
animation: blink ease-in-out both;
animation-timeline: --section;
}
@keyframes blink {
0%,
100% {
filter: blur(var(--blur)) contrast(var(--contrast));
opacity: 0;
visibility: hidden;
}
50% {
filter: blur(0) contrast(1);
opacity: 1;
visibility: visible;
}
}
/*
This is a bit of a hack to get the indicator to work because I'm lazy.
We're translating the dot from the top to the bottom of its parent,
using the browser scroll position as the animation timeline.
It's not really matched up to the scrolling sections, only appears to be.
*/
.indicator::before {
animation: indicate linear both;
animation-timeline: --main;
animation-range: contain;
}
/* And we're manually setting the colors because see: lazy comment above */
@keyframes indicate {
0% {
--color-indicator: var(--color-primary);
transform: translateY(0);
}
25% {
--color-indicator: var(--color-yellow);
}
50% {
--color-indicator: var(--color-secondary);
}
75% {
--color-indicator: var(--color-red);
}
100% {
--color-indicator: var(--color-purple);
transform: translateY(
calc(var(--indicator-total-height) - var(--indicator-size))
);
}
}
/* Remove gradient indicator on scroll-to-end (visible on small screens */
.site-header label:last-of-type {
view-timeline: --site-header inline;
}
.site-header::after {
animation: fade-scroll ease-in-out both;
animation-timeline: --site-header;
animation-range: entry-crossing;
}
@keyframes fade-scroll {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
/* Change animation based on radio checked */
body:has([value="horizontal-scroll"]:checked) .content {
/*
The only reason we're repeat these two properties instead of simply
setting `animation-name` is so the polyfill will pick them up
They're flaky though and tend to get stuck. You might need to
refresh the page and select an option before scrolling
*/
animation: horizontal-scroll ease-in-out both;
animation-timeline: --section;
}
body:has([value="backwards-scroll"]:checked) .content {
animation: backwards-scroll ease-in-out both;
animation-timeline: --section;
}
body:has([value="zoom-scroll"]:checked) .content {
animation: zoom-scroll ease-in-out both;
animation-timeline: --section;
}
/* Alternative animations */
/* Very cool, try it */
@keyframes horizontal-scroll {
0% {
transform: translate3d(100%, 0%, 0);
}
50% {
transform: none;
}
100% {
transform: translate3d(-100%, 0%, 0);
}
}
/* Befuddling, try it */
@keyframes backwards-scroll {
0% {
transform: translate3d(0%, -100%, 0);
}
50% {
transform: none;
}
100% {
transform: translate3d(0%, 100%, 0);
}
}
/* WIP */
@keyframes zoom-scroll {
0% {
filter: blur(5rem);
transform: scale(0);
opacity: 0;
visibility: hidden;
}
50% {
filter: blur(0);
transform: none;
opacity: 1;
visibility: visible;
}
100% {
filter: blur(3rem);
transform: scale(1.5);
opacity: 0;
visibility: hidden;
}
}
/*
The actual page styling is in a different stylesheet to not clutter
this pen with irrelevant or confusing code
https://codepen.io/giana/pen/rNRzgRj
*/
Also see: Tab Triggers