Skip to content

Instantly share code, notes, and snippets.

@ahdezm
Last active May 17, 2021 11:31
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ahdezm/3561324 to your computer and use it in GitHub Desktop.
Save ahdezm/3561324 to your computer and use it in GitHub Desktop.
CSS Animated Repeating Gradient
/**
* CSS Animated Repeating Gradient
*/
body {
background: repeating-linear-gradient(-45deg,red,red 20px,blue 20px,blue 40px);
background-size:56px 56px;/* This is unique for this background, need to find a pattern and develop a formula */
background-position-x:0%;
-webkit-animation:'slide' 20s infinite linear forwards;
}
@-webkit-keyframes 'slide' {
0%{
background-position-x:0%;
}
100% {
background-position-x:100%;
}
}
@analog-nico
Copy link

Good job! Works like a charm on Safari and Chrome.

However, doesn't work in Firefox although I am taking care of the prefixes...

@analog-nico
Copy link

Firefox didn't work for two reasons:

  • The quotes around 'slide'
  • Missing support for background-position-x but we can use background-position

Here is the version that works in Safari, Chome, and Firefox:

/**
 * CSS Animated Repeating Gradient
 */
body {
    background: repeating-linear-gradient(-45deg,red,red 20px,blue 20px,blue 40px);
    background-size: 56px 56px; /* This is unique for this background, need to find a pattern and develop a formula */
    -webkit-animation: slide 20s infinite linear forwards;
    -moz-animation: slide 20s infinite linear forwards;
    animation: slide 20s infinite linear forwards;
}
@-webkit-keyframes slide {
    0% { background-position: 0% 0; }
    100% { background-position: 100% 0; }
}
@-moz-keyframes slide {
    0% { background-position: 0% 0; }
    100% { background-position: 100% 0; }
}
@keyframes slide {
    0% { background-position: 0% 0; }
    100% { background-position: 100% 0; }
}

@munkhorgil
Copy link

Thanks works cool!

@behnood-eghbali
Copy link

There's a nice website by Ian Forrest that you can create animated gradients with it.
There's also an awesome code in Codepen. Cheers! :)

@mikeevstropov
Copy link

Thank you!

@mrOrlando
Copy link

I'd replace the background property with the background-image because it can cause issues if someone will switch the order of the rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment