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.
<!--
PROGRESSIVELY ENHANCED
If a user has `prefers-reduced-motion: reduced`, there will be no animation
and the items will wrap, instead of being hidden.
If they have not opted for reduced motion, the items will be duplicated with JS
and the duplicated content will have `aria-hidden="true"` to prevent duplicate content
for screen readers.
If a user has JS disabled or it fails for whatever reason, they will get the same
experience as a user with `prefers-reduced-motion: reduced`, so no content is hidden,
and there is no animation.
=== OPTIONS ===
CONTROL SPEED
If you don't assign anything, it will use a default speed.
To change the speed, on the `.scroller`
you can use `data-speed="fast"` or `data-speed="slow"
CONTROL DIRECTION
By default, it will scroll from right to left.
To change the direction, on the `.scroller`
you can use `data-direction="right"` (`data-direction="left" also works, but it is the default)
-->
<h1 style="text-align: center">Infinite Scroll Animation</h1>
<div class="scroller" data-speed="fast">
<ul class="tag-list scroller__inner">
<li>HTML</li>
<li>CSS</li>
<li>JS</li>
<li>SSG</li>
<li>webdev</li>
<li>animation</li>
<li>UI/UX</li>
</ul>
</div>
<div class="scroller" data-direction="right" data-speed="slow">
<div class="scroller__inner">
<img src="https://i.pravatar.cc/150?img=1" alt="" />
<img src="https://i.pravatar.cc/150?img=2" alt="" />
<img src="https://i.pravatar.cc/150?img=3" alt="" />
<img src="https://i.pravatar.cc/150?img=4" alt="" />
<img src="https://i.pravatar.cc/150?img=5" alt="" />
<img src="https://i.pravatar.cc/150?img=6" alt="" />
</div>
</div>
<a class="yt" href="https://youtu.be/pKHKQwAsZLI">
Watch the tutorial
</a>
.scroller {
max-width: 600px;
}
.scroller__inner {
padding-block: 1rem;
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.scroller[data-animated="true"] {
overflow: hidden;
-webkit-mask: linear-gradient(
90deg,
transparent,
white 20%,
white 80%,
transparent
);
mask: linear-gradient(90deg, transparent, white 20%, white 80%, transparent);
}
.scroller[data-animated="true"] .scroller__inner {
width: max-content;
flex-wrap: nowrap;
animation: scroll var(--_animation-duration, 40s)
var(--_animation-direction, forwards) linear infinite;
}
.scroller[data-direction="right"] {
--_animation-direction: reverse;
}
.scroller[data-direction="left"] {
--_animation-direction: forwards;
}
.scroller[data-speed="fast"] {
--_animation-duration: 20s;
}
.scroller[data-speed="slow"] {
--_animation-duration: 60s;
}
@keyframes scroll {
to {
transform: translate(calc(-50% - 0.5rem));
}
}
/* general styles */
:root {
--clr-neutral-100: hsl(0, 0%, 100%);
--clr-primary-100: hsl(205, 15%, 58%);
--clr-primary-400: hsl(215, 25%, 27%);
--clr-primary-800: hsl(217, 33%, 17%);
--clr-primary-900: hsl(218, 33%, 9%);
}
html {
color-scheme: dark;
}
body {
display: grid;
min-block-size: 100vh;
place-content: center;
font-family: system-ui;
font-size: 1.125rem;
background-color: var(--clr-primary-800);
}
.tag-list {
margin: 0;
padding-inline: 0;
list-style: none;
}
.tag-list li {
padding: 1rem;
background: var(--clr-primary-400);
border-radius: 0.5rem;
box-shadow: 0 0.5rem 1rem -0.25rem var(--clr-primary-900);
}
/* for testing purposed to ensure the animation lined up correctly */
.test {
background: red !important;
}
const scrollers = document.querySelectorAll(".scroller");
// If a user hasn't opted in for recuded motion, then we add the animation
if (!window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
addAnimation();
}
function addAnimation() {
scrollers.forEach((scroller) => {
// add data-animated="true" to every `.scroller` on the page
scroller.setAttribute("data-animated", true);
// Make an array from the elements within `.scroller-inner`
const scrollerInner = scroller.querySelector(".scroller__inner");
const scrollerContent = Array.from(scrollerInner.children);
// For each item in the array, clone it
// add aria-hidden to it
// add it into the `.scroller-inner`
scrollerContent.forEach((item) => {
const duplicatedItem = item.cloneNode(true);
duplicatedItem.setAttribute("aria-hidden", true);
scrollerInner.appendChild(duplicatedItem);
});
});
}
Also see: Tab Triggers