惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News | PayPal Newsroom
Application and Cybersecurity Blog
Application and Cybersecurity Blog
O
OpenAI News
S
Security @ Cisco Blogs
宝玉的分享
宝玉的分享
Hacker News: Ask HN
Hacker News: Ask HN
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
TaoSecurity Blog
TaoSecurity Blog
Help Net Security
Help Net Security
Latest news
Latest news
NISL@THU
NISL@THU
S
Security Affairs
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
AI
AI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
小众软件
小众软件
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
AWS News Blog
AWS News Blog
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
I
InfoQ
Schneier on Security
Schneier on Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Exploit Database - CXSecurity.com
IT之家
IT之家
T
Threatpost
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
News and Events Feed by Topic
L
LINUX DO - 最新话题
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
H
Heimdal Security Blog
S
SegmentFault 最新的问题

CSS Articles by Temani Afif

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks Let’s Play With Gap Decorations! How to Control Infinite CSS Animations (Part 2 of 2) How to Control Infinite CSS Animations (Part 1 of 2) Two Circles, One Arrow, and Anchor Positioning Making a Responsive Pyramidal Grid With Modern CSS | CSS-Tricks How to Create a CSS-only Elastic Text Effect Making Complex CSS Shapes Using shape() | CSS-Tricks Responsive Hexagon Grid Using Modern CSS | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 2) | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 1) | CSS-Tricks Perfectly Pointed Tooltips: To The Corners Perfectly Pointed Tooltips: All Four Sides Perfectly Pointed Tooltips: A Foundation Sequential linear() Animation With N Elements | CSS-Tricks Infinite Marquee Animation using Modern CSS Better CSS Shapes Using shape() — Part 4: Close and Move | CSS-Tricks Drawing CSS Shapes using corner-shape Better CSS Shapes Using shape() — Part 3: Curves | CSS-Tricks Better CSS Shapes Using shape() — Part 2: More on Arcs | CSS-Tricks Better CSS Shapes Using shape() — Part 1: Lines and Arcs | CSS-Tricks Creating Blob Shapes using clip-path: shape() Creating Flower Shapes using clip-path: shape() Custom progress element using the attr() function A CSS-Only Star Rating Component and More! (Part 2) | CSS-Tricks A CSS-Only Star Rating Component and More! (Part 1) | CSS-Tricks How to Create Wavy Boxes Using CSS Full-Bleed Layout with Modern CSS Fancy Menu Navigation Using Anchor Positioning | CSS-Tricks How to Create a Zig-Zag Box Using CSS How to Create Zig-Zag CSS Loaders Using One Element Custom Progress Element Using Anchor Positioning & Scroll-Driven Animations How to Create Filling CSS Loaders Using One Element How to Create Curved-Edge and Rounded-Edge Shapes Using CSS CSS Tricks That Use Only One Gradient | CSS-Tricks How to create Shapes with Inner Curves using CSS Mask Custom Range Slider Using Anchor Positioning & Scroll-Driven Animations How to Get the Width/Height of Any Element in Only CSS How Keyboard Navigation Works in a CSS Game How To Create Cut-Out Shapes using The clip-path property The Modern Guide For Making CSS Shapes — Smashing Magazine css-shape.com: The Ultimate Collection of CSS-only Shapes ⚡️ Sliding 3D Image Frames In CSS — Smashing Magazine CSS Tricks To Master The clip-path Property Creating Wavy Circles with Fancy Animations in CSS Modern CSS Tooltips And Speech Bubbles (Part 2) — Smashing Magazine Do you need a Tooltip or a Speech Bubble? I have created 100 using CSS 😲 Modern CSS Tooltips And Speech Bubbles (Part 1) — Smashing Magazine Creating Flower Shapes using CSS Mask & Trigonometric Functions CSS Shapes: The Triangle The Complex But Awesome CSS border-image Property — Smashing Magazine CSS Tricks to add 3D Effects to your Text CSS Responsive Multi-Line Ribbon Shapes (Part 2) — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 1) — Smashing Magazine I have made 100+ CSS-only Ribbon Shapes | The Perfect Collection 🎀 Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 2) — Smashing Magazine Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 1) — Smashing Magazine How to Create CSS Ribbon Shapes with a Single Element CSS Shapes: The Ribbon Revealing Images With CSS Mask Animations — Smashing Magazine css-loaders.com: The Biggest Collection of Loading Animations (more than 500 🤯) How To Define An Array Of Colors With CSS — Smashing Magazine CSS effects on images II Shines, Perspective, And Rotations: Fancy CSS 3D Effects For Images — Smashing Magazine What's your Lucky CSS Pattern? Check It Now! How to Add a CSS Reveal Animation to Your Images Modern Layouts using CSS Grid I created 100+ unique CSS patterns | The best collection 🤩 How to Create a Custom Range Slider Using CSS CSS Shapes: The Heart CSS Shapes: Polygon & Starburst A Text Reveal Animation using CSS CSS Tip: learn CSS the easy way! How to build a CSS-only accordion How to create Breadcrumb Navigation with CSS Different Ways to Get CSS Gradient Shadows | CSS-Tricks A CSS-only responsive Stepper component A Fancy Hover Effect For Your Avatar | CSS-Tricks How to make a zoom effect using CSS Creating a Custom Cursor using CSS CSS Infinite 3D Sliders | CSS-Tricks CSS Infinite Slider Flipping Through Polaroid Images | CSS-Tricks How to create a responsive sidebar menu using CSS CSS Infinite and Circular Rotating Image Slider | CSS-Tricks Making Static Noise From a Weird CSS Gradient Bug | CSS-Tricks CSS Grid and Custom Shapes, Part 3 | CSS-Tricks Fancy Image Decorations: Outlines and Complex Animations | CSS-Tricks Fancy Image Decorations: Masks and Advanced Hover Effects | CSS-Tricks Fancy Image Decorations: Single Element Magic | CSS-Tricks How to create an infinite image slider using CSS How to Create Wavy Shapes & Patterns in CSS | CSS-Tricks How I Made a Pure CSS Puzzle Game | CSS-Tricks How to create a Tooltip/Speech Bubble using CSS CSS Grid and Custom Shapes, Part 2 | CSS-Tricks CSS Grid and Custom Shapes, Part 1 | CSS-Tricks Zooming Images in a Grid Layout | CSS-Tricks How to create a CSS-only loader with one element Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers | CSS-Tricks How to create background pattern using CSS & conic-gradient Single Element Loaders: The Bars | CSS-Tricks
How to create a CSS-only infinite scroll animation
2023-10-16 · via CSS Articles by Temani Afif

How to create a CSS-only infinite scroll animation

Written on by Temani AfifWeb Developer

Estimated read time 5 minutes

In a previous article, we made an image slider with an infinite loop animation. The challenge was to use minimal HTML code, only CSS, and without any image duplication. Here is a demo of the slider

All you have to do is add as many images as you want and update one variable to match the number of images.

In this article, we will improve that slider to add more features. In the above demo, we see only one image at a time so why not show more? We will introduce another variable that controls the number of visible images. We will also see how to animate other elements and not only images.

The Code Structure

The HTML code is the same as the one in the previous article. A list of images within a container.

<div class="gallery">
  <img src="" alt="">
  <img src="" alt="">
  <img src="" alt="">
  <img src="" alt="">
  <img src="" alt="">
</div>

For the CSS, we are going to rely on a grid to place the elements and to define the number of visible images. Here is an illustration to better understand.

Overview of the grid configuration

We have 5 images in total and only 3 are showing inside the container. Imagine the images sliding from left to right (or the opposite). The ones outside the container will be hidden.

The container is a grid of 3 columns and the CSS code will look like below:

$n:5; /* number of images*/

.gallery  {
  display: grid;
  grid-template-columns: repeat(3,200px); /* number of visible images + width */
  overflow: hidden;
}
.gallery > img {
  grid-area: 1/1;
  width: 100%;
}

You are probably wondering why I am using grid-area: 1/1 to place the images above each other. It’s because I am going to rely on the same animation technique used in the previous article so I highly recommend you read that article before this one. In this article, I will focus on adding new features while reusing the code of the old slider.

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED HOSTING PLANS FOR WORDPRESS

with the discount code

SERVERS-SALE

The Animation

Like the previous slider, we will define one animation for all the images then we will use a different animation-delay for each one.

$n:5; /* number of images*/

.gallery  {
  --d: 10s; /* duration */
}
.gallery > img {
  animation: r var(--d) linear infinite;
}
@for $i from 2 to ($n + 1) {
  .gallery > img:nth-child(#{$i}) {animation-delay: calc(#{(1 - $i)/$n}*var(--d))}
}

I used the code of the previous slider. The only difference will be the definition of the animation (the @keyframes).

Overview of the CSS animation

The @keyframes will have two parts:

  1. Initially, the image is placed on the first column. We slide it to the left until it’s completely hidden. We will use transform: translate(-100%)

  2. When the image is completely hidden, we instantly move it to the end. Meanwhile, the other images have moved as well. We will use transform: translate(($n - 1)*100%). From there it will move back to its initial position (transform :translate(0))

The code will be as follow:

@keyframes r {
  X% {transform: translate(-100%)}
  (X + .01)% {transform: translate(($n - 1)*100%)}
}

Now, we need to identify the X value. In the first part of the animation, the image is moving by one time its size while in the second part, it’s moving by (n-1) time its size. We get the following equation:

T1 + T2 = 100% 
T2 = (n - 1)*T1

Both parts together define the whole @keyframes hence their sum is equal to 100%. From this we get

T1 = 100%/n

Which is also the value of X

@keyframes r {
  #{100/$n}% {transform: translate(-100%)}
  #{100/$n + .01}% {transform: translate(($n - 1)*100%)}
}

Our new slider is done and its code is very similar to the previous one. We only updated the grid configuration to show more images and we adjusted the animation.

90%

💰 90% OFF YOUR FIRST MONTH WITH ALL VERPEX SHARED HOSTING PLANS

with the discount code

AWESOME

Adding The Gap

Since we are showing more images, controlling the gap between them would be a good idea. The only issue is that adding gaps will break our animation since this one relies only on the image sizes to perform the translation. Now, it will be different because we need to account for the gap when doing the translation.

To make things easier I will define the gap as a percentage of the image width.

$n:5; /* number of images*/
$w:200px; /* width */
$g:.2; /* gap as percentage of width */

.gallery  {
  display: grid;
  grid-template-columns: repeat(3,$w); /* number of visible images */
  gap: $g*$w;
}

The code should be self-explanatory but you are probably wondering why I am using Sass variables and not CSS variables. That’s because the percentage of the @keyframes will also depend on those variables and CSS variables cannot be used there.

For the animation, we need to update the second translation from transform: translate(($n - 1)*100%) to transform: translate(($n - 1 + ($n - 2)*$g)*100%). Between n - 1 images we have n - 2 gaps that we need to account for.

Same for the percentages. Our new equation will be equal to:

T1 + T2 = 100% 
T2 = (n - 1 + (n - 2)*g)*T1

And the new @keyframes will be

@keyframes r {
  #{100/($n + ($n - 2)*$g)}% {transform: translate(-100%)}
  #{100/($n + ($n - 2)*$g) + .01}% {transform: translate(($n - 1 + ($n - 2)*$g)*100%)}
}

When the gap is equal to 0 we get back to the previous slider which is nothing but a particular case of this one.

See the Pen by Temani Afif () on .

We are done! We have a nice infinite scroll animation where we can easily control the total number of images, the number of visible images, and the gap. Worth noting, that the number of visible images needs to be smaller than the total number of images. Since we are not duplicating the images we cannot see the same one twice.

What about a left-to-right animation? Will we need a new code?

No, the same code can be used to have the opposite direction. All you need to do is to add the keywords reverse inside the animation

More examples

Now that we have built our slider using images, let’s animate more elements. As long as you keep the same code structure you can create an infinite scroll animation with any elements. The only restriction is “equal width elements”.

Let’s animate some text content

In the above, I am not defining any explicit width. I am using 1fr to tell the browser to consider the width of the longest word. Don’t forget that all the elements are placed above each other in the first column so the width of that column needs to fit all the elements (in particular the longest one) and by using 1fr we make all the columns equal.

I am also not defining the gap like previously. Instead, I am using a transparent border. This is another way to introduce gaps while using the code of the slider without gaps. It may sound confusing but in reality, we don’t have gaps between the elements if we consider the grid structure but by making some part of the elements transparent (border in this case) we simulate gaps between elements.

You may think that using a transparent border is an easier way to add gaps. True, but in some cases, you may not be enable to do it and you have to rely on the other method. It’s always good to know different methods.

We animated images, we animated text and we can also animate complex components combining images and texts.

See the Pen by Temani Afif () on .

90%

💰 90% OFF YOUR FIRST MONTH WITH ALL VERPEX SHARED HOSTING PLANS

with the discount code

AWESOME

Conclusion

In the previous article, we built a simple slider of images that we enhanced in this article to consider more features. Now you can control the number of visible elements, the gap, and the direction of the animation. You can also animate any kind of content, the only restriction is to use equal width elements.

And we did this using a CSS-only solution. No need for any JavaScript and no need to duplicate elements.

Frequently Asked Questions

Temani Afif

Temani Afif is an expert web developer, a content creator, and a CSS addict. He is the mastermind behind CSS Loaders, CSS Generators, CSS Tip and many other CSS-related websites.

View more posts by Temani Afif