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="container">
<div class="progresscar">
<span class="wheel"></span>
<span class="wheel"></span>
</div>
<label class="debug-toggle-container" for="debug-toggle">
<input type="checkbox" id="debug-toggle" /> debug
</label>
<header class="heading">
<h1>Scroll down</h1>
</header>
<main class="content">
<article class="article">
<h2>An article</h2>
<p>Lorem quasi magni placeat voluptates deserunt, eos ex, eum Quod molestiae odio fugit ad ullam Hic suscipit obcaecati nulla non maiores, qui. Ratione optio obcaecati error nihil deleniti Sit odit? Dolor tempora harum cum rerum sunt magnam! Est id iure repellat necessitatibus earum voluptates Quaerat libero rem cupiditate corporis repudiandae? Repellendus eaque quas laborum error labore Facilis sunt exercitationem dolor</p>
<p>Amet ipsa beatae harum voluptatem laudantium delectus? Suscipit in rerum culpa rerum harum totam illum! Similique nam officiis sit recusandae temporibus. Id officia reiciendis iusto neque iure hic. Delectus exercitationem? Consectetur delectus dolorem doloribus quisquam officiis earum Doloribus fuga cumque ipsa sint voluptatibus, similique Praesentium itaque quisquam officiis labore id? Architecto hic odit perspiciatis facilis officiis expedita, dolorum Dolore veritatis</p>
<p>Lorem quasi magni placeat voluptates deserunt, eos ex, eum Quod molestiae odio fugit ad ullam Hic suscipit obcaecati nulla non maiores, qui. Ratione optio obcaecati error nihil deleniti Sit odit?</p>
<p>Lorem quasi magni placeat voluptates deserunt, eos ex, eum Quod molestiae odio fugit ad ullam Hic suscipit obcaecati nulla non maiores, qui. Ratione optio obcaecati error nihil deleniti Sit odit? Dolor tempora harum cum rerum sunt magnam! Est id iure repellat necessitatibus earum voluptates Quaerat libero rem cupiditate corporis repudiandae? Repellendus eaque quas laborum error labore Facilis sunt exercitationem dolor</p>
<p>Amet ipsa beatae harum voluptatem laudantium delectus? Suscipit in rerum culpa rerum harum totam illum! Similique nam officiis sit recusandae temporibus. Id officia reiciendis iusto neque iure hic. Delectus exercitationem? Consectetur delectus dolorem doloribus quisquam officiis earum Doloribus fuga cumque ipsa sint voluptatibus, similique Praesentium itaque quisquam officiis labore id? Architecto hic odit perspiciatis facilis officiis expedita, dolorum Dolore veritatis Sit nostrum sunt eligendi delectus quis? Facilis quos sapiente libero repellat rem. Culpa similique minima corporis laudantium est, quis Natus velit cumque eligendi amet rem. Similique voluptas rerum dignissimos laborum?</p>
<p>Lorem quasi magni placeat voluptates deserunt, eos ex, eum Quod molestiae odio fugit ad ullam Hic suscipit obcaecati nulla non maiores, qui. Ratione optio obcaecati error nihil deleniti Sit odit?</p>
<p>Consectetur delectus dolorem doloribus quisquam officiis earum Doloribus fuga cumque ipsa sint voluptatibus, similique Praesentium itaque quisquam officiis labore id? Architecto hic odit perspiciatis facilis officiis expedita, dolorum Dolore veritatis</p>
<p>Sit nostrum sunt eligendi delectus quis? Facilis quos sapiente libero repellat rem. Culpa similique minima corporis laudantium est, quis Natus velit cumque eligendi amet rem. Similique voluptas rerum dignissimos laborum? Lorem quasi magni placeat voluptates deserunt, eos ex, eum Quod molestiae odio fugit ad ullam Hic suscipit obcaecati nulla non maiores, qui. Ratione optio obcaecati error nihil deleniti Sit odit?</p>
<p>Dolor tempora harum cum rerum sunt magnam! Est id iure repellat necessitatibus earum voluptates Quaerat libero rem cupiditate corporis repudiandae? Repellendus eaque quas laborum error labore Facilis sunt exercitationem dolor Amet ipsa beatae harum voluptatem laudantium delectus? Suscipit in rerum culpa rerum harum totam illum! Similique nam officiis sit recusandae temporibus. Id officia reiciendis iusto neque iure hic. Delectus exercitationem?</p>
<p>Consectetur delectus dolorem doloribus quisquam officiis earum Doloribus fuga cumque ipsa sint voluptatibus, similique Praesentium itaque quisquam officiis labore id? Architecto hic odit perspiciatis facilis officiis expedita, dolorum Dolore veritatis Amet ipsa beatae harum voluptatem laudantium delectus? Suscipit in rerum culpa rerum harum totam illum! Similique nam officiis sit recusandae temporibus. Id officia reiciendis iusto neque iure hic. Delectus exercitationem?</p>
<p>Consectetur delectus dolorem doloribus quisquam officiis earum Doloribus fuga cumque ipsa sint voluptatibus, similique Praesentium itaque quisquam officiis labore id? Architecto hic odit perspiciatis facilis officiis expedita, dolorum Dolore veritatis</p>
<p>Sit nostrum sunt eligendi delectus quis? Facilis quos sapiente libero repellat rem. Culpa similique minima corporis laudantium est, quis Natus velit cumque eligendi amet rem. Similique voluptas rerum dignissimos laborum?</p>
<p>Lorem quasi magni placeat voluptates deserunt, eos ex, eum Quod molestiae odio fugit ad ullam Hic suscipit obcaecati nulla non maiores, qui. Ratione optio obcaecati error nihil deleniti Sit odit? Dolor tempora harum cum rerum sunt magnam! Est id iure repellat necessitatibus earum voluptates Quaerat libero rem cupiditate corporis repudiandae? Repellendus eaque quas laborum error labore Facilis sunt exercitationem dolor</p>
<p>Amet ipsa beatae harum voluptatem laudantium delectus? Suscipit in rerum culpa rerum harum totam illum! Similique nam officiis sit recusandae temporibus. Id officia reiciendis iusto neque iure hic. Delectus exercitationem? Consectetur delectus dolorem doloribus quisquam officiis earum Doloribus fuga cumque ipsa sint voluptatibus, similique Praesentium itaque quisquam officiis labore id? Architecto hic odit perspiciatis facilis officiis expedita, dolorum Dolore veritatis</p>
<p>Lorem quasi magni placeat voluptates deserunt, eos ex, eum Quod molestiae odio fugit ad ullam Hic suscipit obcaecati nulla non maiores, qui. Ratione optio obcaecati error nihil deleniti Sit odit?</p>
<p>Lorem quasi magni placeat voluptates deserunt, eos ex, eum Quod molestiae odio fugit ad ullam Hic suscipit obcaecati nulla non maiores, qui. Ratione optio obcaecati error nihil deleniti Sit odit? Dolor tempora harum cum rerum sunt magnam! Est id iure repellat necessitatibus earum voluptates Quaerat libero rem cupiditate corporis repudiandae? Repellendus eaque quas laborum error labore Facilis sunt exercitationem dolor</p>
<p>Amet ipsa beatae harum voluptatem laudantium delectus? Suscipit in rerum culpa rerum harum totam illum! Similique nam officiis sit recusandae temporibus. Id officia reiciendis iusto neque iure hic. Delectus exercitationem? Consectetur delectus dolorem doloribus quisquam officiis earum Doloribus fuga cumque ipsa sint voluptatibus, similique Praesentium itaque quisquam officiis labore id? Architecto hic odit perspiciatis facilis officiis expedita, dolorum Dolore veritatis Sit nostrum sunt eligendi delectus quis? Facilis quos sapiente libero repellat rem. Culpa similique minima corporis laudantium est, quis Natus velit cumque eligendi amet rem. Similique voluptas rerum dignissimos laborum?</p>
<p>Lorem quasi magni placeat voluptates deserunt, eos ex, eum Quod molestiae odio fugit ad ullam Hic suscipit obcaecati nulla non maiores, qui. Ratione optio obcaecati error nihil deleniti Sit odit?</p>
<p>Consectetur delectus dolorem doloribus quisquam officiis earum Doloribus fuga cumque ipsa sint voluptatibus, similique Praesentium itaque quisquam officiis labore id? Architecto hic odit perspiciatis facilis officiis expedita, dolorum Dolore veritatis</p>
<p>Sit nostrum sunt eligendi delectus quis? Facilis quos sapiente libero repellat rem. Culpa similique minima corporis laudantium est, quis Natus velit cumque eligendi amet rem. Similique voluptas rerum dignissimos laborum? Lorem quasi magni placeat voluptates deserunt, eos ex, eum Quod molestiae odio fugit ad ullam Hic suscipit obcaecati nulla non maiores, qui. Ratione optio obcaecati error nihil deleniti Sit odit?</p>
<p>Dolor tempora harum cum rerum sunt magnam! Est id iure repellat necessitatibus earum voluptates Quaerat libero rem cupiditate corporis repudiandae? Repellendus eaque quas laborum error labore Facilis sunt exercitationem dolor Amet ipsa beatae harum voluptatem laudantium delectus? Suscipit in rerum culpa rerum harum totam illum! Similique nam officiis sit recusandae temporibus. Id officia reiciendis iusto neque iure hic. Delectus exercitationem?</p>
<p>Consectetur delectus dolorem doloribus quisquam officiis earum Doloribus fuga cumque ipsa sint voluptatibus, similique Praesentium itaque quisquam officiis labore id? Architecto hic odit perspiciatis facilis officiis expedita, dolorum Dolore veritatis Amet ipsa beatae harum voluptatem laudantium delectus? Suscipit in rerum culpa rerum harum totam illum! Similique nam officiis sit recusandae temporibus. Id officia reiciendis iusto neque iure hic. Delectus exercitationem?</p>
<p>Consectetur delectus dolorem doloribus quisquam officiis earum Doloribus fuga cumque ipsa sint voluptatibus, similique Praesentium itaque quisquam officiis labore id? Architecto hic odit perspiciatis facilis officiis expedita, dolorum Dolore veritatis</p>
<p>Sit nostrum sunt eligendi delectus quis? Facilis quos sapiente libero repellat rem. Culpa similique minima corporis laudantium est, quis Natus velit cumque eligendi amet rem. Similique voluptas rerum dignissimos laborum?</p>
<p>Car picture is taken from <a href="https://www.carolenash.com/news/classic-car-news/detail/bullitt-mustang-to-be-restored-and-put-on-the-road-again">this website</a></p>
</article>
</main>
<div class="ruler">
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</div>
/* [number] means there's an explanation for that at the very bottom. */
:root {
--π: 3.14159;
}
.progresscar {
--car-width-px: calc(var(--car-width) * 1px);
position: fixed;
bottom: 0;
left: 0;
width: var(--car-width-px);
height: calc(var(--car-width-px) / 3.2);
background: url('progresscar.png') no-repeat center center;
background-size: 100%;
mix-blend-mode: soft-light;
/* [1] */
--road-length: calc(100vw - var(--car-width-px));
transform: translateX(
calc(var(--scroll-y-1) * var(--road-length))
);
}
.wheel {
--size: calc(var(--car-width) / 14);
--size-px: calc(var(--size) * 1px);
position: absolute;
bottom: calc(var(--size-px) / 2);
display: inline-block;
width: var(--size-px);
height: var(--size-px);
background: url('progresscar-rim.png') no-repeat center center;
background-size: 100%;
/* [2] */
--circumference: calc(var(--π) * var(--size));
--road-length: calc(var(--vw) - var(--car-width));
--rotations: calc(var(--road-length) / var(--circumference));
transform: rotate(
calc(1turn * var(--rotations) * var(--scroll-y-1))
);
}
/* [3] */
.wheel:first-child {
left: calc(var(--car-width-px) / 5.4);
}
/* [3] */
.wheel:last-child {
right: calc(var(--car-width-px) / 7);
}
/* boilerplate */
* {
box-sizing: border-box;
}
body {
margin: 0;
font: normal 100%/1.5 "Operator Mono", menlo, monaco, monospace;
}
.container {
padding: 1em;
transition: all .3s;
}
@media (min-width: 670px) {
.container {
width: 640px;
margin: 0 auto;
padding: 5em 0;
}
}
/* debug mode */
.debug-toggle-container {
position: fixed;
top: 1em;
right: 1em;
padding: .5em;
background: #FFFFFFEE;
cursor: pointer;
}
.debug {
width: 100%;
padding: 5em;
background-image: repeating-linear-gradient(
90deg,
transparent,
transparent 1.8vw,
rgba(0, 0, 0, .2) 2vw
);
}
.debug .heading {
font-size: 2em;
}
.debug .article {
opacity: 0;
}
.debug .ruler {
position: fixed;
z-index: 0;
bottom: calc(30% - 50px);
left: 0;
width: 100%;
height: 50px;
}
.ruler span {
--wheel-size: calc(var(--car-width) / 14);
display: block;
width: calc(var(--wheel-size) * var(--π) * 1px);
height: 100%;
float: left;
}
.ruler span:nth-child(even) {
background: pink;
}
.ruler span:nth-child(odd) {
background: black;
}
.debug .progresscar {
bottom: 30%;
mix-blend-mode: initial;
z-index: 1;
}
.debug .wheel::after {
content: "";
position: absolute;
top: 50%;
left: 50%;
width: 3px;
height: 80px;
transform: translateX(-50%);
background: lime;
}
/*
* Explanations
*
* [1] (L:17):
* --scroll-y-1 property is provided by kicss.js
* It's 0 when the page scroll is 0, and it's 1 when the page scroll reached the bottom.
* Here we map scroll-y-1 to translateX.
*
*
* [2] (L:34):
* Rotation of each wheel
* - wheel circumference: π * size (size = 2r)
* - road length: viewport-width - car-width
* - scroll-y-1: value between 0 and 1
*
* We need to use a custom absolute number for viewport-width, instead of 100vw,
* because otherwise it can't be multiplied by 1turn
*
* To calculate how much rotation is needed, this formula is used:
* 1turn * scroll-y-1 * (road-length / circumference)
*
* To put in human language:
* For one full wheel rotation to happen, the car must move exactly as much
* as the circumference of the wheel. This means page should be scrolled just
* enough for the car to move exactly one-wheel-circumference forward.
*
*
* [3] (L:43, L:48):
* arbitrary positioning based on the car image used.
*
*
*
* Credits:
* Car picture source: https://www.carolenash.com/news/classic-car-news/detail/bullitt-mustang-to-be-restored-and-put-on-the-road-again
*/
/*
* An experimental mini library that I authored, called KICSS, is used
* for communicating a few dynamic variables to CSS using custom properties.
*
* For more info, check:
* - https://github.com/scriptype/kicss
* - https://enes.in/kicss/
*
* Note that it's very premature and probably buggy at the moment.
*/
import { reportGlobals, reportVariable } from "https://enes.in/kicss/kicss.js"
reportGlobals({
scroll: true
})
const { min } = Math
const innerWidth = () => window.innerWidth
reportVariable('--car-width', () => min(innerWidth() / 3, 600))
reportVariable('--vw', innerWidth)
/*
* Debug controls
*/
const debugToggle = document.querySelector('#debug-toggle')
debugToggle.addEventListener('click', () => {
document.body.classList.toggle('debug')
})
Also see: Tab Triggers