darkhorse
darkhorse

Reputation: 8722

Excluding a child element from being affected by a CSS animation

I have the following markup:

<div class="container">
  <div class="content">
    ...
    <div class="no-animate">
      ...
    </div>
  </div>
</div>

So I want to apply a CSS animation to the .container, but exclude the .no-animate inside of it. Please note, the .no-animate has the position: fixed property, which is why I want to exclude it.

I have tried the following:

@keyframes my-animation {
    12.5% {
        transform: translateX(-0.5rem);
    }
    37.5% {
        transform: translateX(0.5rem);
    }
    62.5% {
        transform: translateX(-0.2rem);
    }
    87.5% {
        transform: translateX(0.2rem);
    }
}
.container {
    animation: my-animation 0.5s;
    animation-timing-function: linear;
}
.container .no-animate {
    animation: none;
}

Unfortunately, this doesn't seem to have any effect, and the .no-animate moves because of the animation. Any help is greatly appreciated.

Upvotes: 1

Views: 7042

Answers (4)

dbasch
dbasch

Reputation: 1748

I simplified the answer from @ac_mmi to make it easier to understand and to display correctly as a code snippet.

    .container {
      height: 100px;
      width: 100px;
      background: black;
      margin: 10px;
      animation: move_box 1s linear infinite;
    }
    
    .moveChildBox {
      height: 20px;
      width: 20px;
      background: red;
      animation: none;
    }
    
    .antiMoveChildBox {
      height: 20px;
      width: 20px;
      background: red;
      animation: anti_move_box 1s linear infinite;
    }
    
    @keyframes move_box {
      12.5% {
        transform: translateX(-0.5rem);
      }
      37.5% {
        transform: translateX(0.5rem);
      }
      62.5% {
        transform: translateX(-0.2rem);
      }
      87.5% {
        transform: translateX(0.2rem);
      }
    }
    
    @keyframes anti_move_box {
      12.5% {
        transform: translateX(0.5rem);
      }
      37.5% {
        transform: translateX(-0.5rem);
      }
      62.5% {
        transform: translateX(0.2rem);
      }
      87.5% {
        transform: translateX(-0.2rem);
      }
    }
<div class="container">
 <div class="moveChildBox"></div>
</div>
        
<div class="container">
  <div class="antiMoveChildBox"></div>
</div>

Upvotes: 1

BugsNRoses
BugsNRoses

Reputation: 9

You can use an pseudo element to fix the issue as follows

 .div_container{
        width: 100%;
        height: 100%;
 } 
 
 .div_containing_arrow {
        display:flex;
        justify-content:center;
        align-items:center;
    }
 
 .div_button {
        font-size:1.5rem;
 }
 .div_containing_arrow::after {
        content: '';
        position: absolute;
        width: 150px;
        height: 150px;
        border-radius:100%;
        border: 5px solid white !important;
        border-bottom: 5px solid blue !important;
        animation: animateLoading 1.5s linear infinite;
    }


    @keyframes animateLoading {
        0% {
            transform: rotate(0deg);
        }

        100% {
            transform: rotate(360deg);
        }
    }
<div class='div_container'>
    <div class='div_containing_arrow'>
      <button class='div_button'> > </button>
    <div>
<div>

Upvotes: 1

Aman Chadha
Aman Chadha

Reputation: 2496

Just apply the same transform values but in opposite direction to counter the effect of translate of parent container.

Something like this:

* {
  margin: 0px;
  padding: 0px;
}

body {
  display: grid;
  justify-content: center;
  align-items: center;
  grid-template-columns: auto auto;
  height: 100vh;
}

.container {
  height: 500px;
  width: 600px;
  background: black;
  margin: 10px;
  animation: move_box 1s linear infinite;
}

@keyframes move_box {
  12.5% {
    transform: translateX(-0.5rem);
  }
  37.5% {
    transform: translateX(0.5rem);
  }
  62.5% {
    transform: translateX(-0.2rem);
  }
  87.5% {
    transform: translateX(0.2rem);
  }
}

.box,
.box_without {
  height: 100px;
  width: 100px;
  background: red;
  animation: anti_move_box 1s linear infinite;
}

.box_without {
  animation: none;
}

@keyframes anti_move_box {
  12.5% {
    transform: translateX(0.5rem);
  }
  37.5% {
    transform: translateX(-0.5rem);
  }
  62.5% {
    transform: translateX(0.2rem);
  }
  87.5% {
    transform: translateX(-0.2rem);
  }
}
<h1>Red box not moving alongside black box</h1>
<h1>Red box moving alongside black box</h1>
<div class="container">
  <div class="box"></div>
</div>

<div class="container">
  <div class="box_without"></div>
</div>

Upvotes: 3

You cannot exclude a child element from animation. Instead, you can place that child element outside the parent block but, you can apply the position: absolute; property to create a view where the background (parent) is animated but, the child remains still. There are only workarounds to do it. One of which I mentioned above.

There is another similar workaround in this answer

Upvotes: 4

Related Questions