flmueller flmueller -4 years ago 116
CSS Question

How to avoid cascading animation (@keyframe) on children in css?

Do you know how I can avoid that children divs do not animate with the parent?



@keyframes rotate
{
from {
transform: rotate(0deg)
}
to {
transform: rotate(360deg)
}
}

.gradient {
width: 200px;
height: 200px;
position: relative;
top: 100px;
margin: 0 auto;
border: 20px solid rgba(0, 0, 0, 0);
border-radius: 50%;
-webkit-border-radius: 50%;
background-image: linear-gradient(white, white), linear-gradient(#ff0000, #0000ff);
background-origin: border-box;
background-clip: content-box, border-box;
animation: rotate 5s infinite linear;
}

.gradientitem {
position: absolute;
top: 50%;
left: 50%;
width: 30px;
height: 30px;
border-radius: 50%;
-webkit-border-radius: 50%;
margin: -15px;
background-color: #999999;
}

<div class="gradient">
<div class="gradientitem" style="transform: rotate(180deg) translate(100px) rotate(180deg);"></div>
<div class="gradientitem" style="transform: rotate(0deg) translate(100px) rotate(-0deg);"></div>
</div>





So here you can see an example of my problem that the two little circles are rotating with the big circle. I tried different things like changing the position or "animation: none" for the children...

Answer Source

Create a new element for the animation inside gradient div

<div class="gradient_animation"></div>

add animation css to this class. it works.!

@keyframes rotate
{
	from {
		transform: rotate(0deg)
	}
	to {
		transform: rotate(360deg)
	}
}

.gradient {
	width: 200px;
	height: 200px;
  position: relative;
margin: 100px;
}

.gradient_animation{
  position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: -1;
  border: 20px solid rgba(0, 0, 0, 0);
  border-radius: 50%;
  -webkit-border-radius: 50%;
  background-image: linear-gradient(white, white), linear-gradient(#ff0000, #0000ff);
  background-origin: border-box;
  background-clip: content-box, border-box;
  animation: rotate 5s infinite linear;
}

.gradientitem {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  -webkit-border-radius: 50%;
  margin: -15px; 
  background-color: #999999;
}
<div class="gradient">
<div class="gradient_animation"></div>
  <div class="gradientitem" style="transform: rotate(180deg) translate(100px) rotate(180deg);"></div>
  <div class="gradientitem" style="transform: rotate(0deg) translate(100px) rotate(-0deg);"></div>
</div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download