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.
<!-- @TODO: Move away from custom props before injecting the polyfill, as it can't handle it -->
<input type="checkbox" id="debug" />
<header>
<div>
<h1>Stacking Cards</h1>
<p>π Scroll down to see the effect.</p>
</div>
</header>
<main>
<ul id="cards">
<li class="card" id="card_1">
<div class="card__content">
<div>
<h2>Card One</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
<p><a href="#top" class="btn btn--accent">Read more</a></p>
</div>
<figure>
<img src="https://codyhouse.co/demo-tutorials/stacking-cards/assets/img/img-1.jpg" alt="Image description">
</figure>
</div>
</li>
<li class="card" id="card_2">
<div class="card__content">
<div>
<h2>Card Two</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
<p><a href="#top" class="btn btn--accent">Read more</a></p>
</div>
<figure>
<img src="https://codyhouse.co/demo-tutorials/stacking-cards/assets/img/img-2.jpg" alt="Image description">
</figure>
</div>
</li>
<li class="card" id="card_3">
<div class="card__content">
<div>
<h2>Card Three</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
<p><a href="#top" class="btn btn--accent">Read more</a></p>
</div>
<figure>
<img src="https://codyhouse.co/demo-tutorials/stacking-cards/assets/img/img-3.jpg" alt="Image description">
</figure>
</div>
</li>
<li class="card" id="card_4">
<div class="card__content">
<div>
<h2>Card Four</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
<p><a href="#top" class="btn btn--accent">Read more</a></p>
</div>
<figure>
<img src="https://codyhouse.co/demo-tutorials/stacking-cards/assets/img/img-2.jpg" alt="Image description">
</figure>
</div>
</li>
</ul>
</main>
<aside>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Rem repellat non ratione eum provident quaerat obcaecati dicta autem voluptates sit cum quis distinctio, atque sint commodi reprehenderit perspiciatis iure velit molestiae eos molestias ipsa nihil quia? Porro tempore in fugit, aspernatur et temporibus aliquam magnam adipisci deleniti dicta repellat alias nostrum impedit quidem odit excepturi nam. Itaque numquam unde quae dignissimos exercitationem placeat, ipsa amet tempora vitae ullam animi! Hic asperiores in esse atque expedita dolorem officia illo commodi fugit earum ab, assumenda optio, sed repellat. Maiores laudantium reprehenderit suscipit accusamus assumenda, debitis nemo ut sapiente numquam doloremque alias explicabo minus autem harum illum dicta. Unde obcaecati inventore repellat id eligendi eaque, rerum aliquam maiores aspernatur cumque tenetur quo qui mollitia atque quasi voluptate ipsum neque explicabo voluptatem quibusdam beatae quae iste vero doloribus! Natus libero quibusdam molestias harum atque, assumenda doloribus quis, quam corporis, voluptas suscipit magni! Enim sapiente totam consequatur repellat maxime placeat perferendis iste maiores soluta, obcaecati incidunt expedita eius cumque ipsa quo sed. Minus distinctio culpa cumque labore nulla laborum ex, explicabo magni mollitia eaque aliquam vero blanditiis quae, id necessitatibus error voluptas harum tempora sint porro. Facilis beatae quasi soluta et ipsum suscipit repellendus inventore.</p>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Possimus, ipsam molestias. Fuga similique, dolor tempora rerum perferendis ipsam ullam doloribus beatae esse facilis quae sit excepturi expedita non, sunt accusantium magnam? Animi nesciunt nostrum eligendi iusto voluptatum hic qui. Quod distinctio qui nobis et doloremque. Fuga, itaque? Laborum dignissimos nobis quaerat optio voluptatum similique unde, neque cupiditate, vel, fugiat dolore? Accusantium neque deserunt ducimus enim, illum laborum magni ipsam eos optio aspernatur nam necessitatibus quod natus, dolor laboriosam illo velit.</p>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Rem repellat non ratione eum provident quaerat obcaecati dicta autem voluptates sit cum quis distinctio, atque sint commodi reprehenderit perspiciatis iure velit molestiae eos molestias ipsa nihil quia? Porro tempore in fugit, aspernatur et temporibus aliquam magnam adipisci deleniti dicta repellat alias nostrum impedit quidem odit excepturi nam. Itaque numquam unde quae dignissimos exercitationem placeat, ipsa amet tempora vitae ullam animi! Hic asperiores in esse atque expedita dolorem officia illo commodi fugit earum ab, assumenda optio, sed repellat. Maiores laudantium reprehenderit suscipit accusamus assumenda, debitis nemo ut sapiente numquam doloremque alias explicabo minus autem harum illum dicta. Unde obcaecati inventore repellat id eligendi eaque, rerum aliquam maiores aspernatur cumque tenetur quo qui mollitia atque quasi voluptate ipsum neque explicabo voluptatem quibusdam beatae quae iste vero doloribus! Natus libero quibusdam molestias harum atque, assumenda doloribus quis, quam corporis, voluptas suscipit magni! Enim sapiente totam consequatur repellat maxime placeat perferendis iste maiores soluta, obcaecati incidunt expedita eius cumque ipsa quo sed. Minus distinctio culpa cumque labore nulla laborum ex, explicabo magni mollitia eaque aliquam vero blanditiis quae, id necessitatibus error voluptas harum tempora sint porro. Facilis beatae quasi soluta et ipsum suscipit repellendus inventore.</p>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Possimus, ipsam molestias. Fuga similique, dolor tempora rerum perferendis ipsam ullam doloribus beatae esse facilis quae sit excepturi expedita non, sunt accusantium magnam? Animi nesciunt nostrum eligendi iusto voluptatum hic qui. Quod distinctio qui nobis et doloremque. Fuga, itaque? Laborum dignissimos nobis quaerat optio voluptatum similique unde, neque cupiditate, vel, fugiat dolore? Accusantium neque deserunt ducimus enim, illum laborum magni ipsam eos optio aspernatur nam necessitatibus quod natus, dolor laboriosam illo velit.</p>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Rem repellat non ratione eum provident quaerat obcaecati dicta autem voluptates sit cum quis distinctio, atque sint commodi reprehenderit perspiciatis iure velit molestiae eos molestias ipsa nihil quia? Porro tempore in fugit, aspernatur et temporibus aliquam magnam adipisci deleniti dicta repellat alias nostrum impedit quidem odit excepturi nam. Itaque numquam unde quae dignissimos exercitationem placeat, ipsa amet tempora vitae ullam animi! Hic asperiores in esse atque expedita dolorem officia illo commodi fugit earum ab, assumenda optio, sed repellat. Maiores laudantium reprehenderit suscipit accusamus assumenda, debitis nemo ut sapiente numquam doloremque alias explicabo minus autem harum illum dicta. Unde obcaecati inventore repellat id eligendi eaque, rerum aliquam maiores aspernatur cumque tenetur quo qui mollitia atque quasi voluptate ipsum neque explicabo voluptatem quibusdam beatae quae iste vero doloribus! Natus libero quibusdam molestias harum atque, assumenda doloribus quis, quam corporis, voluptas suscipit magni! Enim sapiente totam consequatur repellat maxime placeat perferendis iste maiores soluta, obcaecati incidunt expedita eius cumque ipsa quo sed. Minus distinctio culpa cumque labore nulla laborum ex, explicabo magni mollitia eaque aliquam vero blanditiis quae, id necessitatibus error voluptas harum tempora sint porro. Facilis beatae quasi soluta et ipsum suscipit repellendus inventore.</p>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Possimus, ipsam molestias. Fuga similique, dolor tempora rerum perferendis ipsam ullam doloribus beatae esse facilis quae sit excepturi expedita non, sunt accusantium magnam? Animi nesciunt nostrum eligendi iusto voluptatum hic qui. Quod distinctio qui nobis et doloremque. Fuga, itaque? Laborum dignissimos nobis quaerat optio voluptatum similique unde, neque cupiditate, vel, fugiat dolore? Accusantium neque deserunt ducimus enim, illum laborum magni ipsam eos optio aspernatur nam necessitatibus quod natus, dolor laboriosam illo velit.</p>
</aside>
<div class="warning">
<p>β οΈ Your browser does not support Scroll-Linked Animations with JS <code>ViewTimeline</code>. To make this demo work in your browser, <a href="https://github.com/flackr/scroll-timeline">a polyfill</a> has been loaded.</p>
<p>π¨ Note that <b>this demo loads a version of the polyfill that diverges from the spec</b>: it calculates the phases differently. Itβs only because of this that this demo actually works.</p>
</div>
:root {
--card-height: 40vw;
--card-margin: 4vw;
--card-top-offset: 1em;
--numcards: 4;
--outline-width: 0px;
}
#cards {
padding-bottom: calc(var(--numcards) * var(--card-top-offset)); /* Make place at bottom, as items will slide to that position*/
margin-bottom: var(--card-margin); /* Don't include the --card-margin in padding, as that will affect the scroll-timeline*/
}
.card {
position: sticky;
top: 0;
padding-top: var(--card-top-offset);
transform-origin: 50% 0%;
will-change: transform;
}
/** DEBUG **/
#debug {
position: fixed;
top: 1em;
left: 1em;
}
#debug::after {
content: " Show Debug";
margin-left: 1.5em;
color: white;
white-space: nowrap;
}
#debug:checked ~ main {
--outline-width: 1px;
}
/** PAGE STYLING **/
* { /* Poor Man's Reset */
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
background: rgb(58 29 43);
color: rgb(255, 255, 255);
text-align: center;
font-size: calc(1em + 0.5vw);
}
header,
main {
width: 80vw;
margin: 0 auto;
}
header {
height: 100vh;
display: grid;
place-items: center;
}
#cards {
list-style: none;
outline: calc(var(--outline-width) * 10) solid blue;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: repeat(var(--numcards), var(--card-height));
gap: var(--card-margin);
}
.card {
outline: var(--outline-width) solid hotpink;
}
.card__content {
box-shadow: 0 0.2em 1em rgba(0, 0, 0, 0.1), 0 1em 2em rgba(0, 0, 0, 0.1);
background: rgb(255, 255, 255);
color: rgb(10, 5, 7);
border-radius: 1em;
overflow: hidden;
display: grid;
grid-template-areas: "text img";
grid-template-columns: 1fr 1fr;
grid-template-rows: auto;
align-items: stretch;
outline: var(--outline-width) solid lime;
}
.card__content > div {
grid-area: text;
width: 80%;
place-self: center;
text-align: left;
display: grid;
gap: 1em;
place-items: start;
}
.card__content > figure {
grid-area: img;
overflow: hidden;
}
.card__content > figure > img {
width: 100%;
height: 100%;
object-fit: cover;
}
h1 {
font-weight: 300;
font-size: 3.5em;
}
h2 {
font-weight: 300;
font-size: 2.5em;
}
p {
font-family: sans-serif;
font-weight: 300;
line-height: 1.42;
}
.btn {
background: rgb(188 87 36);
color: rgb(255 255 255);
text-decoration: none;
display: inline-block;
padding: 0.5em;
border-radius: 0.25em;
}
aside {
width: 50vw;
margin: 0 auto;
text-align: left;
}
aside p {
margin-bottom: 1em;
}
/* Show warning for older browser */
.warning {
position: fixed;
bottom: 1em;
right: 1em;
left: 1em;
padding: 1em;
border: 1px solid black;
z-index: 9999;
text-align: center;
color: black;
background: rgba(255 255 225 / 0.9);
}
.warning a {
color: blue;
}
.warning > :first-child {
margin-top: 0;
}
.warning > :last-child {
margin-bottom: 0;
}
.warning a {
color: blue;
}
.warning--info {
border: 1px solid #123456;
background: rgb(205 230 255 / 0.8);
}
.warning--alarm {
border: 1px solid red;
background: #fff1f1;
}
import { showDialog } from 'https://codepen.io/bramus/pen/ZEqMOLz/cccfe67c2b9cdfbeb5fb59083dbd0a64.js';
showDialog('https://scroll-driven-animations.style/demos/stacking-cards/css/');
// Alternative Versions:
// - Vanilla JS Version: https://codyhouse.co/demo-tutorials/stacking-cards/index.html
// - CSS @scroll-timeline: https://codepen.io/bramus/pen/PobmGme
// - JS ScrollTimeline Polyfill: https://codepen.io/bramus/pen/XWEeGZm π = The version you are currently looking at
// - JS Motion One: https://codepen.io/bramus/pen/rNdzpZK
// This uses an old version of the polyfill, one that calculates the phases wrong (but which makes this demo work)
import "https://rawcdn.githack.com/flackr/scroll-timeline/ca40d920073ab1c3775f6e2aa973c6ea747ae664/dist/scroll-timeline.js";
const $cardsWrapper = document.querySelector('#cards');
const $cards = document.querySelectorAll('.card');
const numCards = $cards.length;
// Each card should only shrink when itβs at the top.
// We canβt use exit on the els for this (as they are sticky)
// but can track $cardsWrapper instead.
const viewTimeline = new ViewTimeline({
subject: $cardsWrapper,
axis: 'block',
});
$cards.forEach(($card, index0) => {
const index = index0 + 1;
const reverseIndex = numCards - index0;
const reverseIndex0 = numCards - index;
// Extra padding per card, so you can see the other stacked cards underneath at the top
$card.style.paddingTop = `calc(${index} * var(--card-top-offset))`;
// Scroll-Linked Animation
$card.animate(
{
// Earlier cards shrink more than later cards
transform: [ `scale(1)`, `scale(${1 - (0.1 * reverseIndex0)}`],
},
{
timeline: viewTimeline,
fill: 'forwards',
// timeRange: `exit ${index0 / numCards * 100}% ${index / numCards * 100}%`,
delay: { phase: "exit", percent: CSS.percent(index0 / numCards * 100) },
endDelay: { phase: "exit", percent: CSS.percent(index / numCards * 100) },
// delay: { phase: "contain", percent: CSS.percent(0) },
// endDelay: { phase: "exit", percent: CSS.percent(100) },
// timeRange: 'exit 0% 100%'
}
);
});
Also see: Tab Triggers