Guide on applying a CSS shimmer effect to a group of HTML elements simultaneously

I am looking to add a shimmer animation to a group of HTML elements, making it appear as though one continuous shimmer is passing through all the elements. I have observed this effect achieved with text animations using background-clip: text; in CSS and applying the animation to the parent element.

Is there a way to apply a similar animation to a collection of elements simultaneously, clipping to the HTML elements instead of just text like using background-clip: text?

Link to fiddle

.shimmer-box {
  width: 300px;
  height: 75px;
  margin: 20px 20px;
}

.animation {
  background: linear-gradient(-45deg, #eee 40%, #fafafa 50%, #eee 60%);
  background-size: 300%;
  animation-name: shimmer;
  animation-duration: 1000ms;
  animation-timing-function: linear;
  animation-delay: 0;
  animation-iteration-count: infinite;
  animation-direction: normal;
  animation-fill-mode: none;
  animation-play-state: running;  
}

@keyframes shimmer {
  0% {
    background-position-x: 100%;
  }

  100% {
    background-position-x: 0%;
  }
}

.gray {
  background-color: #e2e2e2;
}

.flex-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.circle {
  width: 30px;
  height: 30px;
  border-radius: 50%;
}

.short-row {
  height: 30px;
  flex: 1;
  border-radius: 8px;
}

.body-box {
  margin-top: 12px;
  width: 342px;
  height: 100px;
  border-radius: 8px;
}

.container {
  display: inline-block;
}
<h4>This animation works on a single element</h4>
<div class="shimmer-box animation"></div>

<h4>If I apply the animation to each element they are disjointed</h4>
<div class="container">
  <div class="flex-row">
    <div class="circle gray animation"></div>
    <div class="short-row gray animation"></div>
    <div class="short-row gray animation"></div>
  </div>
  <div class="body-box gray animation"></div>
</div>

<div>
<h4>I would like this animation to run in unison on all the elements here</h4>
<paragraph>Ideally it looks like one shimmer running accross all the elements in unison</paragraph>
</div>
<div class="container">
  <div class="flex-row">
    <div class="circle gray"></div>
    <div class="short-row gray"></div>
  </div>
  <div class="body-box gray"></div>
</div>

Answer №1

To enhance the visual appeal of your elements with the .animation class, simply insert the following line into the existing ruleset:

background-attachment: fixed;

By doing so, you will create a synchronized effect where the background of all elements assigned to that class will be fixed to the viewport.

.shimmer-box {
  width: 300px;
  height: 75px;
  margin: 20px 20px;
}

.animation {
  background: linear-gradient(-45deg, #eee 40%, #fafafa 50%, #eee 60%);
  /* add this line for synchronization: */
  background-attachment: fixed;
  background-size: 300%;
  animation-name: shimmer;
  animation-duration: 1000ms;
  animation-timing-function: linear;
  animation-delay: 0;
  animation-iteration-count: infinite;
  animation-direction: normal;
  animation-fill-mode: none;
  animation-play-state: running;
}

@keyframes shimmer {
  0% {
    background-position-x: 100%;
  }
  100% {
    background-position-x: 0%;
  }
}

.gray {
  background-color: #e2e2e2;
}

.flex-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.circle {
  width: 30px;
  height: 30px;
  border-radius: 50%;
}

.short-row {
  height: 30px;
  flex: 1;
  border-radius: 8px;
}

.body-box {
  margin-top: 12px;
  width: 342px;
  height: 100px;
  border-radius: 8px;
}

.container {
  display: inline-block;
}
<h4>Witness the synchronization on a single element</h4>
<div class="shimmer-box animation"></div>

<h4>Experiencing disjointed animations when applied individually</h4>
<div class="container">
  <div class="flex-row">
    <div class="circle gray animation"></div>
    <div class="short-row gray animation"></div>
    <div class="short-row gray animation"></div>
  </div>
  <div class="body-box gray animation"></div>
</div>

<div>
  <h4>Envisioning a unified shimmer across all elements here</h4>
  <paragraph>The ultimate goal is a cohesive shimmer effect flowing through all elements uniformly</paragraph>
</div>
<div class="container">
  <div class="flex-row">
    <div class="circle gray"></div>
    <div class="short-row gray"></div>
  </div>
  <div class="body-box gray"></div>
</div>

Experience the magic on JSFiddle.

References:

Answer №2

One creative approach to controlling animations involves using two classes. The first is a wrapper class, which is applied to the container where all elements should animate together. The second is the animation class, added to individual elements that need to be animated. It's possible to use both classes on the same element if only one item requires animation.

.wrapper {
  position: relative; 
  z-index: 0;
}
.animation {
  mask: linear-gradient(#000 0 0); /* clip the pseudo-element to the element boundary (no, overflow: hidden won't work) */
}
/*
  the pseudo element is positioned relatively to the wrapper 
  making all the animations synchronized 
*/
.animation:before {
  content:"";
  position: absolute;
  z-index: -1;
  inset: 0;
  background: 
    linear-gradient(-45deg, #eee 40%, #fafafa 50%, #eee 60%)
    right/300% 100%;
  animation: shimmer 1s linear infinite;
}
@keyframes shimmer {
  to { background-position: left;}
}


/* irrelevant code*/
.gray {
  background-color: #e2e2e2;
}
.shimmer-box {
  width: 300px;
  height: 75px;
  margin: 20px 20px;
}
.flex-row {
  display: flex;
  align-items: center;
  gap: 12px;
}
.circle {
  width: 30px;
  height: 30px;
  border-radius: 50%;
}
.short-row {
  height: 30px;
  flex: 1;
  border-radius: 8px;
}
.body-box {
  margin-top: 12px;
  width: 342px;
  height: 100px;
  border-radius: 8px;
}
.container {
  display: inline-block;
}
<div class="wrapper shimmer-box animation"></div>
<hr>
<div class="container wrapper">
  <div class="flex-row">
    <div class="circle gray animation"></div>
    <div class="short-row gray animation"></div>
    <div class="short-row gray animation"></div>
  </div>
  <div class="body-box gray animation"></div>
</div>
<hr>
<div class="container wrapper" style="margin-left: 200px">
  <div class="flex-row">
    <div class="circle gray animation"></div>
    <div class="short-row gray animation"></div>
    <div class="short-row gray animation"></div>
  </div>
  <div class="body-box gray animation"></div>
  <div class="body-box gray animation"></div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it frowned upon to modify Jquery Validation for the purpose of achieving a customized style?

When it comes to adding an icon after an input field when a required field is not filled up, my friend and I have different approaches. My idea is to modify the highlight function in Jquery.Validation by adding a class to the parent element of the input ( ...

Executing a JQuery function when the webpage is loaded

I am attempting to execute some jQuery code when the page loads, but it doesn't seem to be working properly. The goal is to check if a link has already been saved and, if so, display a table with that information. Is this achievable? It should apply t ...

Setting the Default Value for Dropdown Options in Angular 2

Back in the Angular 1 days, I had a trick up my sleeve for setting the default option on a dropdown menu: <select data-ng-model="carSelection" data-ng-options = "x.make for x in cars" data-ng-selected="$first"> </select> But now, in ...

Issue with AddToAny plugin not functioning properly on FireFox

I’m having issues with AddToAny for social media sharing on my website. It seems like FireFox is blocking it because of tracking prevention measures. Error Message in Console: The resource at “https://static.addtoany.com/menu/page.js” was blocked d ...

Is there a way to launch iframe links in the parent window of an Android native app?

I'm currently working on developing a directory-style application, and I am using iframes to embed other websites into the app. I would like the embedded site links to open within the parent window. In the Progressive Web App version, everything is w ...

The malfunctioning Bootstrap navbar dropdown menu is causing a disruption in user experience by unexpectedly logging the user

I'm having an issue with the dropdown menu on my angular app. Every time I click on the Profile link to access the Account and Logout options, it automatically logs me out. <nav class="navbar navbar-expand-lg bg-dark navbar-fixed-top" ...

Leveraging bootstrap's grid system to prioritize content layout based on the

Currently working on a compact React Js project with Bootstrap for styling. One obstacle I've encountered is the need to display the last element as the first one on the page. To achieve this, I plan to utilize the order-1 and order-12 classes from Bo ...

Ensure that all elements with the :hover event have a text color of #fff in CSS

 Troubleshooting problems with block's child elements during the :hover event. Specifically, I have a pricing block where I want all texts to be of color #fff when hovered over. Currently, when hovering over the <h3> and <p> elements ...

Setting a specific height for images in Bootstrap 4 and keeping it

I'm currently utilizing Bootstrap's 4 grid system to showcase images of varying sizes from unsplash.com on a webpage. However, I'm facing an issue in figuring out how to create a fixed-sized thumbnail for all images that can fit into their ...

tips for stopping links from affecting their descendent styles

During my website redevelopment, I want to include simple links in the menu for users to easily open them in new tabs. Currently, a menu item looks like this: <li class='menu-left-row' id='messages' onclick=\"javascript:showscr ...

I am having issues displaying my background image

I'm experiencing an issue where my background image isn't displaying correctly. Here is the CSS code I'm using: body { background-image: url('img/bg.png'); background-repeat: no-repeat; } ...

Attempting to pass HTML content from a Laravel controller to display in data tables

Issue Description I am trying to send HTML tags to a table using editColumn but it is not working. AddColumn works fine for adding HTML code to a column, but editColumn does not display the HTML elements as expected. My question is how can I successfully ...

Load the div using ajax upon submitting the form

I have a basic HTML form with one input field and a submit button. The information entered will be sent to a PHP script which then stores it in a database and displays it within a DIV using Smarty. Without using Ajax, this process works fine as the data ...

Align the text at the center within a relative div without specifying its size

In the structure of my HTML, I have the following simplified layout: <table> <tr> <td> <div class="PromptContainer"> <span class="Prompt">Prompt text</span> </div> <input type="t ...

Optimal method for streaming and viewing an mkv video captured with ffmpeg in a web browser

Is there a way to stream a video recorded with ffmpeg in a web browser using the HTML5 video tag? I currently have this code on my page, with the URL pointing to a video file (example.mkv) being recorded by ffmpeg. <video class="video-player" controls ...

Can a webpage be redirected to another page while passing along the id from the original page?

https://i.sstatic.net/3LhYJ.png I have a page that displays shop names and addresses along with an edit function in views.py: def update_shop(request, id): context = {} # * fetch the object related to passed id obj_shop = get_object_or_404(VideoL ...

How to implement multiple dependent select boxes in Rails?

I'm currently working on developing a car application where each car is assigned to a specific make and model. However, not all makes have every model available. My goal is to create a series of select boxes that update dynamically based on the selec ...

Hide the 1, 2, 3 page buttons in Datatables pagination and instead display only the Next and Previous buttons for navigation

I recently implemented datadables.net pagination for my HTML table. I am looking to customize the pagination buttons by hiding or removing the 1, 2, 3 ... buttons and only display Next and Previous Buttons. Is there a way to achieve this by setting paging ...

The hover effect on MUI TableRows is being overshadowed by the background color of the TableCell

I'm currently using @mui/material version ^5.10.1. My challenge is applying the TableRow hover behavior as outlined in the documentation. However, I have also set a background color for the first TableCell in each row, which ends up overriding the ho ...

What is the best way to anchor an image to the bottom right corner while also adjusting its position as

Hey there! I have a challenge where I want to anchor an image at the bottom right corner of a webpage. Initially, it works perfectly using this CSS: .logo img{ position: absolute; bottom: 0; right: 0; } However, when I resize the window, the ...