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.
<div class="warning">Your browser doesn't support scroll animations. This demo will not work.</div>
<header class="site-header">
<h1>Visibility <span>Demo</span></h1>
<label class="mobile-menu-button hamburger-icon">
<input type="radio" name="toggle-navigation" class="open-navigation navigation-closed" autocomplete="off">
<span class="visually-hidden">Open Navigation</span>
</label>
<nav class="site-navigation">
<div class="navigation-animation-wrapper">
<label class="mobile-menu-button close-icon">
<input type="radio" name="toggle-navigation" class="close-navigation navigation-opened" autocomplete="off">
<span class="visually-hidden">Close navigation</span>
</label>
<ul class="nav-list">
<li style="--index: 0"><a href="#">Home</a></li>
<li style="--index: 1"><a href="#">Portfolio</a></li>
<li style="--index: 2"><a href="#">Projects</a></li>
<li style="--index: 3"><a href="#">About</a></li>
<li style="--index: 4"><a href="#">Blog</a></li>
<li style="--index: 5"><a href="#" class="button">Contact</a></li>
</ul>
</div>
</nav>
</header>
<main>
<div class="left-content">
<h2>Using CSS <span>scroll animations</span> to detect visibility changes</h2>
</div>
<div class="right-content">
<p>Resize the window to see the layout change automatically without media queries or JavaScript.</p>
<p>See “<a href="https://cdpn.io/pen/debug/gOEQmVW/#tracking" target="_top">Content-aware collapsing menu using CSS scroll animations</a>” for an explanation of what's happening here.</p>
</div>
</main>
<footer>
<p>Unstable. <a href="https://cdpn.io/pen/debug/gOEQmVW/#probably-bug" target="_top">Do not use in production</a>.</p>
</footer>
/*
The rest of the page styling has been moved to an external pen
https://codepen.io/giana/pen/XWGyRbL/
The styling here demonstrates the scroll animation features
*/
/* These are specified down below
Hoisted the `visibility` up here for ease of debugging */
.left-content::after,
.site-navigation::after {
background-color: red;
z-index: 10000;
/* Comment out to debug */
visibility: hidden;
}
/*
Content swaps between one to two columns based on content-width
*/
:root {
--min-text-width: 34rem;
--content-min-width: calc(var(--image-width) + var(--min-text-width) * 2);
}
main {
/* already a flexbox container */
/* Attach animations and scope */
timeline-scope: --content-width;
animation: expand-content;
animation-timeline: --content-width;
/* Attach animation timeline to all children */
& > *,
&::before,
&::after {
animation-timeline: --content-width;
}
/* Now attach individual animation */
&::before {
animation-name: wall;
}
&::after {
animation-name: kitten;
}
}
/* Swaps to a two-column layout when marker is in-view */
@keyframes expand-content {
0%, 100% {
flex-direction: row;
align-items: center;
max-height: 30rem;
}
}
@keyframes wall {
0%, 100% {
width: 50%;
}
}
@keyframes kitten {
0%, 100% {
background-image: url("https://assets.codepen.io/197359/kitten-peek-transparent.png");
inset-inline-start: calc(50vw - calc(var(--image-width) * var(--image-clip-offset)));
transform: none;
width: 50%;
}
}
.left-content {
animation-name: main-content;
/* Content width marker */
/* When this is in-view, the animations will trigger */
&::after {
content: '';
position: fixed;
top: 50%;
inset-inline-start: var(--content-min-width);
width: 1px;
height: 50px;
view-timeline: --content-width inline;
}
}
.right-content {
animation-name: main-content, right-content;
}
@keyframes main-content {
0%, 100% {
flex: 1;
margin-inline-end: 0;
margin-block-end: calc(var(--spacing) * 2);
max-width: calc(50% - var(--spacing) / 2);
}
}
@keyframes right-content {
0%, 100% {
align-self: flex-end;
padding-inline-start: calc(var(--image-width) - var(--image-width) * var(--image-clip-offset));
}
}
/*
Navigation swaps between hidden to expanded when there's enough space
*/
:root {
timeline-scope: --expanded-navigation;
}
.site-header {
overflow: hidden;
}
/* Navigation */
.site-navigation {
flex-shrink: 0;
position: relative;
width: max-content;
visibility: hidden;
animation-name: expand-navigation;
animation-timeline: --expanded-navigation;
/* Marker */
&::after {
content: '';
display: block;
position: absolute;
inset-block-start: 0;
inset-inline-end: 0;
width: 1px;
height: 100%;
view-timeline: --expanded-navigation inline;
}
}
@keyframes expand-navigation {
0%,
100% {
/* Setting custom props for children */
--nav-wrapper-display: block;
--nav-wrapper-transform: none;
--nav-wrapper-background: transparent;
visibility: visible;
}
}
/* Navigation list */
.nav-list {
display: flex;
}
/* Menu button, display:none on large screens */
.hamburger-icon {
animation-name: hide-menu-button;
animation-timeline: --expanded-navigation;
}
@keyframes hide-menu-button {
0%,
100% {
display: none;
}
}
/* Menu close button, display:none unless menu opened */
.close-icon {
display: none;
position: fixed;
z-index: 100;
}
/* Open/close animation */
/* Menu styling */
/* Since the custom props are set inside the animation (which triggers on large screens), the defaults will be used on small screens */
.navigation-animation-wrapper {
background-color: var(--nav-wrapper-background, var(--color-background));
display: var(--nav-wrapper-display, flex);
align-items: center;
justify-content: center;
inset: 0;
transform: var(--nav-wrapper-transform, translateX(100%));
z-index: 100;
}
/* On open */
html:has(.open-navigation:checked) {
overflow: hidden;
/*
expand-navigation is automatically triggered and the element becomes visible
due to the properties we are applying that move the marker within the viewport
*/
.navigation-animation-wrapper {
background-color: var(--color-background);
display: flex;
position: fixed;
transition: transform 0.25s ease-out;
transform: none;
width: 100%;
}
.nav-list {
flex-direction: column;
& li {
margin: 0;
}
}
.close-icon {
display: block;
}
}
/* On close */
html:has(.close-navigation:checked) {
.navigation-animation-wrapper {
animation: slide-out 0.25s ease-out;
}
.nav-list {
animation: opacity-out 0.25s ease-out;
}
}
/*
Because we're swapping non-animatable properties, we'll use a keyframe animation
to first animate the animatable properties while preserving the layout
and then, at the last moment, allow the non-animatable properties to swap out
*/
@keyframes slide-out {
0% {
transform: none;
}
100% {
transform: translateX(100%);
}
/* Preserve properties until right before the end */
0%, 99% {
background-color: var(--color-background);
display: flex;
position: fixed;
width: 100%;
}
}
@keyframes opacity-out {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
/* Preserve properties until right before the end */
0%, 99% {
flex-direction: column;
}
}
@supports not (animation-timeline: view(inline)) {
/* Show warning on non-supporting browsers */
.warning {
display: block;
font-size: 0.8em;
text-align: center;
padding-inline: var(--image-aware-padding);
}
/* Smooth menu transition on non-supporting browsers */
/* Just because it doesn't work doesn't mean it can't be pretty #me */
.site-navigation {
transition: 0.25s ease-in-out;
visibility: visible;
}
}
Also see: Tab Triggers