TomDS
TomDS

Reputation: 49

CSS: repeated animated background

I'm trying to create a repeated background existing out of two parts. Each part is a gradient and while the one moves up, the other moves down.

The best I got is this:

html {
  background: black;
  color: #4c4c4c;
}

body {
  margin: 30vh auto;
  max-width: 80vw;
}

.wave {
  background: none;
  height: 1rem;
  width: 50%;
  position: absolute;
  z-index: 2;
  animation: move 700ms 0ms steps(2) infinite both;
}

.color::after,
.color::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  height: 100%;
  top: 0;
}

.color {
  background-image: linear-gradient(#fe0000 50%, #6531ff 0 100%);
}

.color::after {
  background: linear-gradient(to bottom, #f4e04d, #3bceac 20%, rgba(22, 22, 22, 0) 100%), linear-gradient(to right, #042a2b 3rem, transparent 3rem, transparent 6rem);
}

.wave,
.color::after,
.color::before {
  background-size: 5rem 1rem;
  background-repeat: repeat-x;
}

@keyframes move {
  0% {
    margin-top: -3rem;
  }
  100% {
    margin-top: -3.25rem;
  }
}
<div class="color wave"></div>

I get why this doesn't work, but not sure how to proceed.

Since it is difficult to describe, here is an image of what I'm looking for: different positions

At first (position 1), all odd blocks are higher than the even blocks. After the first animation, it's the other way around (position 2) and so on.

Upvotes: 2

Views: 1347

Answers (3)

ray
ray

Reputation: 27285

UPDATE: This is an interesting problem. I'm surprised to find that I don't have an obvious or particularly elegant solution to having a gradient running vertically while repeating with horizontal gaps.

Far more elusive than I initially expected.

Best I could come up with is to put one of the gradients in a pseudo element and apply a mask-image. This won't work in IE, but it appears to be supported everywhere else.

See updated demo below.


If I understand what you're trying to do, I think you could accomplish it by animating the background positions:

.demo {  
  height: 200px;
  background-image: 
    linear-gradient(#f4e04d, #3bceac 20%, rgba(22, 22, 22, 0) 100%);
  animation: move 0.7s infinite alternate;
  background-size: 3rem;
  position: relative;
}

.demo::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: linear-gradient(#042a2b, transparent);
  
  /* This is the magic part: using a horizontal repeating-linear-gradient
  to mask out "columns", allowing the container's background gradient to
  show through */
  -webkit-mask-image: repeating-linear-gradient(to right, black 0 3rem, transparent 3rem 6rem);
  
  background-size: 3rem;
  
  /* run the same animation in reverse to animate up instead of down */
  animation: move 0.7s infinite alternate-reverse;
}

@keyframes move {
  from {
    background-position: 0 0;
  }
  to {
    background-position:
      0 200px;
  }
}
<div class="demo"></div>

Upvotes: 2

Temani Afif
Temani Afif

Reputation: 273990

Maybe like below:

.box {
  height:100px;
  background:linear-gradient(red,blue,yellow,red) 0 0/100% 200%;
  animation:y 2s linear infinite;
}
.box::after {
  content:"";
  display:block;
  height:100%;
  background:linear-gradient(green,lightblue,pink,green) 0 0/100% 200%;
  animation:inherit;
  animation-direction: reverse;
  -webkit-mask:linear-gradient(90deg,#fff 50%,transparent 0) 0 0/20% 100%;
}

@keyframes y {
  to {
    background-position:0 -200%;
  }
}
<div class="box"></div>

Upvotes: 4

Taco
Taco

Reputation: 2923

It's difficult to infer exactly what you're trying to do, but here's another sample (very similar to @ray hatfield's answer) that will move the first background down while the second background moves up:

.sample {
    width: 250px;
    height: 50px;
    position: relative;
    background-image: linear-gradient(to bottom, #f4e04d, #3bceac 20%, rgba(22, 22, 22, 0) 100%), linear-gradient(to right, #042a2b 3rem, transparent 3rem, transparent 6rem);
    animation: move 1s infinite linear;
}
@keyframes move {
  0%, 100% {
    background-position: 0 -75px, 0 0;
  }
  50% {
    background-position: 0 0, 0 -75px;
  }
}
<div class="sample"></div>

Upvotes: 1

Related Questions