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="">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div class="primary"></div>
</main>
<form>
<button type="button" class="active" data-step="0">1</button>
<button type="button" data-step="1">2</button>
<button type="button" data-step="2">3</button>
</form>
<p class="step1">Add six frames. For each frame overlay a repeating gradient of 1px transparent and 5px white. Offset this gradient by 1px for each frame.</p>
<p class="step2">Apply a mix-blend-mode of multiply to each frame, letting the underlying frames to show instead of the white blocks from the gradient.</p>
<p class="step3">Add a cover on top that is 1px transparent and 5px black. Translate it 5px infinitely. The underlying frames will show through one at a time.</p>
:root {
--pixel: 1px;
--cover: #212123;
--duration: 1000ms;
--covered: calc(var(--pixel) * 5); /*6 frames, 5 covered at a time */
--space: white; /* will cause transparency when mix blend mode multiply */
--w: 100vw;
--h: 100vh;
--depth-base: 0px;
--direction: alternate;
--blend: none;
}
main *, main *::after {
opacity: 0;
}
main div {
--offset: calc(var(--pixel) * 1);
--background-image: radial-gradient(circle, cyan 0%, cyan);
--background-cover-yes: linear-gradient(to right, transparent 0, transparent calc(var(--pixel)), var(--space) var(--pixel), var(--space) calc(var(--covered)));
--background-cover-no: linear-gradient(to right, transparent, transparent);
--background-cover: var(--background-cover-no);
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow: hidden;
background-color: #16161c;
background-image: var(--background-cover),
var(--background-image);
mix-blend-mode: var(--blend);
background-position: calc(var(--offset) - calc(var(--pixel) * 1)) 0, center center;
background-size: calc(var(--pixel) * 6) 100%, cover;
background-repeat: repeat, no-repeat;
transition: background 750ms ease-in-out;
filter: saturate(200%);
}
main div:nth-of-type(2) {
--offset: calc(var(--pixel) * 2);
--background-image: radial-gradient(circle, hotpink 0%, cyan 20%);
}
main div:nth-of-type(3) {
--offset: calc(var(--pixel) * 3);
--background-image: radial-gradient(circle, hotpink 20%, cyan 40%);
}
main div:nth-of-type(4) {
--offset: calc(var(--pixel) * 4);
--background-image: radial-gradient(circle, hotpink 40%, cyan 60%);
}
main div:nth-of-type(5) {
--offset: calc(var(--pixel) * 5);
--background-image: radial-gradient(circle, hotpink 60%, cyan 75%);
}
main div:nth-of-type(6) {
--offset: calc(var(--pixel) * 6);
--background-image: radial-gradient(circle, hotpink 80%, cyan);
}
/*want to try using your own frames? insert them here, with either a gradient or an image with url() */
/**/
main div:nth-of-type(1) {
--background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/61811/h1.jpg);
}
main div:nth-of-type(2) {
--background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/61811/h2.jpg);
}
main div:nth-of-type(3) {
--background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/61811/h3.jpg);
}
main div:nth-of-type(4) {
--background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/61811/h4.jpg);
}
main div:nth-of-type(5) {
--background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/61811/h5.jpg);
}
main div:nth-of-type(6) {
--background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/61811/h6.jpg);
}
/**/
/*the sliding cover that triggers the motion illusion */
.primary::after {
content: '';
position: absolute;
top: 0;
right: calc(var(--pixel) * -12);
bottom: 0;
left: calc(var(--pixel) * -12);
background: linear-gradient(to right, transparent 0px, transparent calc(var(--pixel) * 1), var(--cover) calc(var(--pixel) * 1), var(--cover) calc(var(--pixel) * 6));
will-change: transform, opacity;
transition: opacity var(--duration) linear;
mix-blend-mode: normal;
opacity: var(--show, 0);
transform: translateX(var(--slide-amount, 0));
background-size: calc(var(--pixel) * 6) 100%;
animation: var(--animation, none) var(--duration) infinite var(--direction) linear;
}
@keyframes cover {
100% {
transform: translateX(calc(var(--pixel) * 5))
}
}
main {
width: var(--w);
height: var(--h);
position: relative;
transition: transform var(--duration) ease-in-out;
background: white;
transform: scale(1);
}
*, *::after {
box-sizing: border-box;
}
body {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: hsl(203, 50%, 4%);
margin: 0;
overflow: hidden;
touch-action: none;
font-family: system-ui, -apple-system, 'Segoe UI', sans-serif
}
form {
position: absolute;
top: 0;
left: 0;
width: 100%;
padding: .2rem 1rem;
text-align: center;
background: rgba(245,245,255,.86);
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
--form-color-alpha: rgba(150, 250, 255, .1);
overflow: auto;
}
form div {
padding: .5rem .5rem;
font-size: 12px;
transform: translateX(1rem);
}
form label {
transform: translateX(-1.5rem)
}
p {
position: absolute;
line-height: 1.334;
bottom: 0;
left:0;
right:0;
padding: .5rem 1rem;
text-align: center;
background: rgba(245,245,255,1);
display: flex;
align-items: center;
justify-content: center;
opacity: 0;
}
code {
display: inline;
font-family: monospace;
}
button {
appearance: none;
border: 0;
border-radius: 50%;
font-size: 1rem;
margin: .5rem 1rem;
width: 2rem;
height: 2rem;
background-color: hsl(193, 50%, 90%);
transition: background-color 350ms ease-in-out;
}
button.active {
background-color: hsl(193, 50%, 50%);
color: #fafaff;
}
var frames = Array.from(document.querySelectorAll('main div'));
var explanations = Array.from(document.querySelectorAll('p[class*=step]'));
var steps = document.querySelectorAll('button');
var duration = 1000;
buildFrames();
function buildFrames(finish) {
document.documentElement.style.setProperty('--show', 0);
changeExplanation(0);
var dur = finish === true ? 0 : duration;
for (let i = 0, l = frames.length; i < l; ++i) {
let frame = frames[i];
var framer = frame.animate({opacity: [0,1]}, {
duration: dur,
fill: 'both',
delay: dur * 2 * i
});
framer.onfinish = function() {
var masker = frame.animate({backgroundImage: ['var(--background-cover-no),var(--background-image)','var(--background-cover-yes),var(--background-image)']}, {
duration: dur,
fill: 'both',
delay: 0
});
if (i === l-1) {
masker.onfinish = startBlendModes;
}
};
}
}
function startBlendModes(finish) {
document.documentElement.style.setProperty('--show', 0);
changeExplanation(1);
var dur = finish === true ? 0 : duration;
for (let i = frames.length - 1, count = 0; i >= 0; --i) {
let frame = frames[i];
var framer = frame.animate({mixBlendMode: ['normal','multiply']}, {
duration: dur * 1.5,
fill: 'both',
delay: dur * (count++) * 1.5
});
if (i === 0) {
framer.onfinish = startCover;
}
}
}
function startCover() {
document.documentElement.style.setProperty('--show', 1);
changeExplanation(2);
setTimeout(() => {
document.documentElement.style.setProperty('--animation', 'cover');
}, duration * 3)
}
function changeExplanation(index) {
explanations[index].animate({opacity:[0,1]}, {
duration: 250,
fill: 'both'
});
document.querySelector('.active').classList.remove('active');
steps[index].classList.add('active');
}
function clearAnimations() {
if (document.getAnimations) {
document.getAnimations().forEach(function(anim) {
anim.cancel();
});
}
}
// for (let i = 0; i < steps.length; ++i) {
// steps[i].addEventListener('click', changeStep);
// }
function changeStep(e) {
var index = parseInt(e.currentTarget.getAttribute('data-step'), 10);
clearAnimations()
if (index === 0) {
buildFrames();
} else if (index === 1) {
buildFrames(true);
startBlendModes();
} else {
buildFrames(true);
startBlendModes(true);
startCover();
}
}
Also see: Tab Triggers