Creating a loading bar through the animation of a linear-gradient

Creating a loading bar with a single colored element moving along a grey bar is my current project.

Unfortunately, I struggle with animating gradients in CSS. I found a solution that seems promising based on this answer: Make some gradient move endlessly in a progress bar like in Windows 7

foo {
  background-color: $cGray300;
  height: 10px;
  background: linear-gradient(to right,  $cGray300 0%, $cGray300 30%, #fed0d0 30%, #fed0d0 40%, $cGray300 40%, $cGray300 100%) repeat;
  background-size: 50% 100%;
  animation-name: moving-gradient;
  animation-duration: 1s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}

@-webkit-keyframes moving-gradient {
  0% { background-position: left bottom; }
  100% { background-position: right bottom; }
}

This is the outcome of my current implementation: https://i.sstatic.net/Qlp0Z.gif

However, I aim to have a larger red element that loops from the left side once it disappears on the right.

Answer №1

This code snippet was inspired by @Paulie_D's answer and has been modified to be responsive for any screen sizes, along with some additional tweaks.

The goal is to create a material preloader effect.

.state-changed{
position:relative;
width:400px;
height:20px;
border:1px solid silver;
}

.state-changed::after {
        content: '';
        position: absolute;
        height: 2px; 
        background: red;
        animation: progress 1.5s infinite ease-in-out ;
    }
    
    @keyframes progress {
    0% {
        left: 0;
        width: 0;
    }
    50% {
        width: 100%;
    }
    100% {
        right: 0;
        width: 0;
    }
}
<div class="state-changed"></div>

Answer №2

To achieve this effect, all you need is a pseudo-element along with some clever positioning and transformation techniques.

.bar {
  width: 50%;
  height: 10px;
  background: lightgrey;
  margin: 2em auto;
  position: relative;
  overflow: hidden;
}
.bar::after {
  content: '';
  position: absolute;
  height: 100%;
  width: 30%;
  background: red;
  animation: progress 2s infinite linear;
}
@-webkit-keyframes progress {
  0% {
    left: 0;
    transform: translateX(-100%);
  }
  100% {
    left: 100%;
    transform: translateX(0%);
  }
}
<div class="bar"></div>

Answer №3

Check out this CodePen for a functional example

Here is the implementation using only CSS:

HTML:

<div class="loader8"></div>

CSS:

@-webkit-keyframes greenglow {
    from {
        left:-120px;
    }
    to {
        left:100%;
    }
}
@-moz-keyframes greenglow {
    from {
        left:-120px;
    }
    to {
        left:100%;
    }
}
@-o-keyframes greenglow {
    from {
        left:-120px;
    }
    to {
        left:100%;
    }
}
@keyframes greenglow {
    from {
        left:-120px;
    }
    to {
        left:100%;
    }
}

.loader8 {
    width:250px;
    height:16px;
    border:1px solid #bbb;
    border-radius:0px;
    position:relative;
    overflow:hidden;
    background: #E6E6E6;
    margin:5px;
}

.loader8:after {
    content: " ";
    display:block;
    width:120px;
    top:-50%;
    height:250%;
    position:absolute;
    animation: greenglow 3s linear infinite;
    -webkit-animation: greenglow 3s linear infinite;
    z-index:2;
    background: #1CAE30;
}

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

The Bootstrap 5 navigation bar does not extend to the full width of its parent

While working on a Bootstrap navigation inside a container, I encountered an issue where the navigation bar was not expanding to match the width of the container. It seemed like the padding had to be manually adjusted to fit properly. Below is the code sn ...

Resolved the issue of the background image flickering during the transition of the slideshow at the top of

I'm experiencing a unique issue in Chrome only. On my webpage, I have a div with a fixed background image placed below a slider at the top of the page. However, when the slider advances to the next slide, the fixed background image almost completely d ...

Toggle visibility of div element with a button press

Here is the div I have: <div class="margin-bottom hidden-xs"></div> This specific div is initially hidden on a small window size due to the bootstrap 'hidden-xs' class. However, I want it to be shown or hidden when the user clicks o ...

What is the best method to customize CSS in ExtJS 5.0 by overriding a function?

Is there a way to dynamically add a rule to existing CSS using code, particularly in overrides? For example: var sheets = document.styleSheets; // Some code omitted for simplicity var sheet = sheets[sheets.length - 1]; sheet.insertRule('.overlay {flo ...

Is there a distinction between em and percentage measurements and do their defaults have specified definitions?

Both em and percentage are defined in relation to other elements or properties. For example, with text-indent, it is related to the width of the paragraph, while with font size, it is related to the browser's default size. The question arises - wh ...

Creating a stylish layout with Bootstrap 4: equal height divs filled with color and a sleek gutter in between

I am struggling to create a bootstrap 4 layout as per my design screenshot. I have researched other Stack Overflow posts without finding a solution. The .container-fluid layout has a grey fill color, achieved using my custom class .bg-grey. As per the d ...

Customize your Google Translate experience by choosing your own option values

Is there a way to trigger the same JavaScript calls by clicking on an option value in an HTML select element as with text-based links in the Google Translate widget? Check out this jsfiddle for more information <html> <body> <select i ...

Using local file paths in v-lazy-image does not function properly

When trying to use the v-lazy-image plugin for my page banner with local image files, it does not seem to work. <v-lazy-image srcset="../../../assets/images/banners/Small-Banner.svg 320w, ../../../assets/images/banners/Big-Banner.svg 480w&quo ...

Display a fancy slideshow that transitions to five new images when the last one is reached

Here is a screenshot of my issue: https://i.stack.imgur.com/duhzn.png Incorporating Bootstrap 3 and FancyBox, I am facing an issue with displaying images in rows. When I reach the last image, clicking on the right arrow should result in sliding to anothe ...

Could someone offer some assistance in grasping the order of operations in jQuery syntax

Until now, my approach has been deciphering and analyzing various codes and techniques for effectively utilizing jQuery, but I often find myself overwhelmed by the choices presented. This often leads to unnecessary complexity in my code. What I aim to ach ...

Place the div directly beside the input field on the right side

I am attempting to align a div directly beside the text being entered in a text input field. It seems logical to me that this could be achieved by measuring the length of the input value and positioning the div accordingly. However, the issue I am facing i ...

Styling with CSS: Enhancing list items with separators

Is there a way to include a separator image in my list using CSS? li { list-style: none; background-image: url("SlicingImage/button_unselect.png"); height: 53px; width: 180px; } This is the code for the separator image: url("SlicingImage ...

Exploring the differences between a fixed-top navbar and dark theme in Bootstrap 5.3

I've been struggling to find a solution for having a fixed top navigation bar that remains solid without being transparent in both dark and light themes. Switching between dark and light themes results in the fixed-top navigation bar appearing transp ...

What is the best way to add a CSS style to any element that has the .btn:hover pseudo-class, except for those elements with the class '.dropdown-toggle'?

Is there a way to apply a style to all my .btn elements when hovering, except for those with the .dropdown-toggle class? I've tried a couple of methods but ran into some issues: attempt 1: .btn:not(.dropdown-toggle):hover { background-color: inher ...

Tips for crafting a perpetually looping animated shape using canvas

Lately, I've been experimenting with canvas and have encountered a challenge that I can't seem to solve. I've mastered creating shapes, animating them, and looping them across the canvas. However, I'm struggling to figure out how to spa ...

Footer remains fixed to the bottom of the page even when there is not enough content to

It seems like there are already many discussions on this topic, so I apologize if this is a duplicate. I searched for it but couldn't find anything similar. I am currently working on an angular application using the Ionic framework for testing purpos ...

Is the CSS parser malfunctioning?

My program is designed to scan css files using the jar cssparser-0.9.5.jar and perform various operations on the data. public static Map<String, CSSStyleRule> parseCSS(String FileName) throws IOException { Map<String, CSSStyleRule> rul ...

What is the best way to horizontally align an inline div using CSS?

Is it possible to apply CSS to center an inline div that has an unknown width? Note: The width of the div cannot be specified beforehand. ...

Is there a way to retrieve the height of an image and adjust its parent container height to be automatic?

Is there a way to achieve the following using pure JavaScript instead of jQuery? I want to find the height of an image within a figure element and if the image's height is less than 200 pixels, I need to set its container's (parent's) heigh ...

Achieving unique horizontal and vertical spacing in Material UI Grid

In my current Material UI setup, I have set spacing in the Grid Container to space Grid Items vertically. While this works well on larger screens, it causes awkward horizontal spacing between Grid Items on mobile devices. <Grid container spacing={24}> ...