Animate star ratings individually using CSS

I am faced with the following HTML structure:

<div class="rating">
  <div class="star active"></div>
  <div class="star active"></div>
  <div class="star active"></div>
  <div class="star"></div>
  <div class="star"></div>
</div>

The generated output is as follows:

★★★☆☆

My query is regarding how to animate the active stars individually using CSS?

I attempted this method:

.star {
   transition: background-color 0.5s ease;
   background-color: #eee;
}

.star.active { 
   background-color: #000;
}

However, this approach causes all the active stars to change color simultaneously. How can I achieve a sequential transition for the active stars, moving from left to right?

I am seeking a solution that does not involve JavaScript. It should be noted that the rating system could have any number of stars, ranging from 3 to potentially an unlimited amount.

Answer №1

Given that the active class is already in place, it would be more suitable to utilize animation instead of transition.

.star {
  background-color: #eee;
}

.star.active {
  animation: star-pop 0.5s ease forwards;
}

@keyframes star-pop {
   to {
      background-color: #000;
   }
}

Subsequently, distinct animation-delay values can be assigned based on the nth-child:

.star.active:nth-child(1) { 
   animation-delay: 0s;
}

.star.active:nth-child(2) { 
   animation-delay: 0.5s;
}

.star.active:nth-child(3) { 
   animation-delay: 1s;
}

.star.active:nth-child(4) { 
   animation-delay: 1.5s;
}

.star.active:nth-child(5) { 
   animation-delay: 2s;
}

/* ... */

If you are utilizing a css precompiler(such as sass), this can be achieved through a loop:

.star.active {
   // alter the number 5 to reflect the maximum stars possible(8, 10, etc.)
   @for $i from 1 through 5 {
      &:nth-child(#{$i}) {
         animation-delay: #{($i - 1) * 0.5}s;
      }
   }
}

.star {
  display: inline-block;
  width: 40px;
  height: 40px;
  border: solid 1px #000;
  background-color: #eee;
}

.star.active {
  animation: star-pop 0.5s ease forwards;
}


.star.active:nth-child(1) { 
   animation-delay: 0s;
}

.star.active:nth-child(2) { 
   animation-delay: 0.5s;
}

.star.active:nth-child(3) { 
   animation-delay: 1s;
}

.star.active:nth-child(4) { 
   animation-delay: 1.5s;
}

.star.active:nth-child(5) { 
   animation-delay: 2s;
}

@keyframes star-pop {
   to {
      background-color: #000;
   }
}
<div class="rating">
  <div class="star active"></div>
  <div class="star active"></div>
  <div class="star active"></div>
  <div class="star"></div>
  <div class="star"></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

My website includes a <div> section that features both an image and a <figcaption>. However, when I attempt to apply padding to the <figcaption>, it does not seem to take effect

I've been trying to troubleshoot this code, but the padding just won't cooperate and I can't seem to figure out why. .img-caption { visibility:hidden; width:22%; height:435px; background-color:#f9f4e3; ...

Title element in CSS adjusts in width but is not full width

I have a straightforward website designed for phone, tablet, and desktop screens using media queries to adjust the CSS. The challenge arises from the site being PHP-based, dynamic, and not utilizing the full width of the screen. It features two 500px-wide ...

CSS code to create a single content bar flanked by two sidebars

I have been working on a beginner-friendly webpage and have managed to style it using CSS to some extent. My goal is to have the content centered with a white background, flanked by two sidebars styled in blue. To work on this page, please visit: The ...

Stop iPad mobile Safari from truncating content with CSS

Encountering an issue with content being cut off on iPad devices. Even when the width exceeds the iPad's resolution, it seems strange that horizontal scrolling isn't automatically added instead of cutting off the content. If anyone has any sugg ...

What are the disadvantages of not incorporating the main CSS file directly into the web page?

Optimizing the loading time of my webpages is a priority for me, especially when it comes to first-time loading and from cache. Despite that, I still prefer to have the browser re-validate all resources to avoid any strange displays or update issues caused ...

Using AngularJS filter to refine results based on specific values within JSON data

I am looking to implement a custom free text filter with just one input field in a table. The standard filter currently applies to all values, but I specifically need it to focus on col1 and col2 from this JSON array: myJsonArray = [{col1: "xxx", col2: "y ...

OrbiterControls malfunctioning in a three.js application

I am attempting to set up a basic scene with an orbitercontrols feature that allows for rotation around the center, always facing (0,0,0). I would like it to function exactly like this . However, in my version, when I click and drag left or right, the ca ...

Unable to display scrollbar when generating dynamic content with jquery ajax

I have a jQuery report where I am generating dynamic HTML content (nested divs, span, label) using JSON. The report utilizes jQuery, mCustomScrollbar, commons, and jQueryUI. When I have a <div>...//some static code </div>, everything works per ...

Enhancing Plotly Graphs with Custom Node Values

Encountering an issue while attempting to assign values to nodes on a plotly graph. Code: <html> <head> <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> <style> .graph-container { ...

"Provide information, then open a new webpage by clicking on a button. Perform various actions with the form by using type

Is there a way to quickly submit form entries to my database and then automatically redirect to a new page? My current situation requires that submitted information is stored in the DB before directing users to a thank you page, all in one seamless click! ...

Rotational transformation in CSS not displaying in Webkit / iPhone browsers, yet functioning properly in Chrome Devtools device toolbar

tl;dr: My CSS transform rotate is not rendering on Webkit / iPhone, but it works in Chrome Devtools device toolbar. Update: I've also opened a separate GitHub issue that is more concise and easier to read, without code excerpts, here. Giving some co ...

Creating a horizontal navigation bar using localscroll.js in your website is a great way

In my portfolio, I want to create a horizontal navigation with local scroll to showcase a gallery of various pictures. For this, I have a (div id="projects") with links structured like this: <div id="projects"> <ul id="content-slider-inside ...

Encountering issues when attempting to retrieve localized images within .vue files using a custom webpack configuration

Currently, I am deep into learning webpack and attempting to craft my own vue template. However, I have encountered an issue that does not arise in webpack-simple or other templates. Despite having a configuration for images that closely resembles what is ...

Preserve the scrollbar location when the page is refreshed

Is there a way to ensure that when the page is refreshed, the table content returns to its previous position rather than going back to the top? For example, if a user scrolls down and then triggers a page reload with a button, I would like the table conten ...

What is the best way to refresh a webpage in Vue.js following a specific event or a button click?

Is there a way to reload a webpage only once after clicking a button, without the need for auto-refreshing at set intervals? I've written my code in Vue.js. Can anyone offer guidance on how to accomplish this? Any assistance would be greatly apprecia ...

Adjust text to fit within div as browser is resized

Can text inside a div adjust to fit the size of the DIV while the browser window is resized? I came across this and this and several other resources about fitting text to a DIV, but none of them provide a definitive answer for resizing text when the brows ...

How can I align the kendoMenu in the center when the menu width is unpredictable?

Presenting a modified menu structure: <html class="k-webkit k-webkit40"> <head> <title>Site Title</title> <!-- js and css files referenced here --> </head> <body> <link h ...

Animate the jQuery menu to slide both upwards and downwards, as well as shift the inner divs to the right

I'm facing a jquery issue and could use some assistance. I am trying to achieve the following: Add a class "active" to style tags when they are active. Slide up/down vertically within a div (#information - containing "About" and "Contact"). Slide le ...

I am attempting to extract data from a JSON object and showcase it in an HTML format using AngularJS

I am attempting to extract the value of the streets from the JSON object below: Accessing business profile data: { "_id": "56dd1bd4d0561b403f683bfd", "UserId": "56b301516b9064189049acb5", "WebURL": "2", "PhoneNumber": "3" ...

Ways to conceal an HTML element using CSS

I am working with two images, but I only want to display one at a time. When I hover over the image, I want it to change. .sidenav { height: 100%; /* 100% Full-height */ width: 100px; position: fixed; /* Stay in place */ z-index: 2; top: 0 ...