skip to Main Content

How can I get my animation to work without affecting the surrounding elements? In this case, without moving the text around it.

The JSFiddle

<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="icon" href="" />
        <style type="text/css">
            html, body {
                margin: 0;
                padding: 0;
                color: #131313;
                font-family: sans-serif;
            }

            #content {
                margin: 2em;
            }

            #test {
                margin: 2em 2em 0.25em 2em;
                display: flex;
                flex-direction: row;
                align-items: center;
            }

            #test > div:last-child {
                margin: 0 2em;
                display: flex;
                flex-direction: column;
                align-items: flex-start;
            }

            .highlight {
                color: #00FFFF;
            }

            .throb {
                animation-name: throb;
                animation-duration: 1500ms;
                animation-iteration-count: infinite;
                animation-timing-function: ease-in;
            }

            @keyframes throb {
                0% {
                    width: 100px;
                    transform: rotate(0deg);
                }
                12.5% {
                    transform: rotate(45deg);
                }
                25% {
                    transform: rotate(90deg);
                }
                37.5% {
                    transform: rotate(135deg);
                }
                50% {
                    width: 105px;
                    transform: rotate(180deg);
                }
                62.5% {
                    transform: rotate(225deg);
                }
                75% {
                    transform: rotate(270deg);
                }
                87.5% {
                    transform: rotate(315deg);
                }
                100% {
                    width: 100px;
                    transform: rotate(360deg);
                }
            }
        </style>
        <title></title>
    </head>
    <body>
        <div id="test">
            <img src="wheel.png" width="100px" />
            <div>
                <div>the quick brown fox jumps over the lazy dog</div>
                <div>THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG</div>
            </div>
        </div>
        <script type="text/javascript">
            const RECORD = document.querySelector('img');
            RECORD.classList.add('throb');
        </script>
    </body>
</html>

3

Answers


  1. it’s the width property in your animation that is affecting the layout.
    to solve this keeping your animation key frames as is. wrap the image in a div give it the same width and height as the image and set position: absolute on the image.

    the absolute positioning detaches the element from the layout

    Login or Signup to reply.
  2. Your animation changes the image width, causing the text to shift. I fixed this by wrapping the image in a div and making the div as big as the image gets in the animation (105px). I then centered the image in the div.

    <style>
    ...
    
      .image-container{
        display: flex;
        justify-content: center;
        align-items: center;
        width: 105px;
        height: 105px;
      }
    
    ...
    }
    
    ...
    
    <div class='image-container'>
      <img src="..." width="100px" />
    </div>
    
    ...
    

    Hope this helps!

    Login or Signup to reply.
  3. The shift occurs due to change in width during animation. You could achieve a similar effect using transform:scale and removing width property

    const RECORD = document.querySelector("img");
    RECORD.classList.add("throb");
    html,
    body {
      margin: 0;
      padding: 0;
      color: #131313;
      font-family: sans-serif;
    }
    
    #content {
      margin: 2em;
    }
    
    #test {
      margin: 2em 2em 0.25em 2em;
      display: flex;
      flex-direction: row;
      align-items: center;
    }
    
    #test>div:last-child {
      margin: 0 2em;
      display: flex;
      flex-direction: column;
      align-items: flex-start;
    }
    
    .highlight {
      color: #00ffff;
    }
    
    .throb {
      animation-name: throb;
      animation-duration: 1500ms;
      animation-iteration-count: infinite;
      animation-timing-function: ease-in;
    }
    
    @keyframes throb {
      0% {
        transform: rotate(0deg) scale(1);
      }
      12.5% {
        transform: rotate(45deg) scale(1.01);
      }
      25% {
        transform: rotate(90deg) scale(1.02);
      }
      37.5% {
        transform: rotate(135deg) scale(1.03);
      }
      50% {
        transform: rotate(180deg) scale(1.04);
      }
      62.5% {
        transform: rotate(225deg) scale(1.03);
      }
      75% {
        transform: rotate(270deg) scale(1.02);
      }
      87.5% {
        transform: rotate(315deg) scale(1.01);
      }
      100% {
        transform: rotate(360deg) scale(1);
      }
    }
    <div id="test">
      <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9TpSIVEYuIOGSoThaKiuimVShChVArtOpgcv2EJg1Jiouj4Fpw8GOx6uDirKuDqyAIfoC4ujgpukiJ/0sKLWI8OO7Hu3uPu3eAUC8z1eyIAqpmGcl4TExnVsXAKwQMoA9RzMjM1OckKQHP8XUPH1/vIjzL+9yfoyebMxngE4lnmW5YxBvEU5uWznmfOMSKcpb4nHjMoAsSP3JdcfmNc8FhgWeGjFRynjhELBbaWGlj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GDYDMp0JwOp0YHBzEixcvNjscjof19fWyOw+DwYD09PQtqampj5KTk5GYmMgnPA2ALISxsTG4XC64XC44nU68evVqx/Pnz+02m+1ffX19ETuulJQUHDhw4NO6desyV61adT8xMRFxcXGIi4tT3vbZNAAiRXw+37i8Xu9ERXk8Hr3H4yn2eDx6t9sNt9uNN2/eYHR0FCMjI3j79i2WLVuGlStXYsWKFVi+fDliY2MRGxsLnU7XoNPpLDqdrkGr1fq1Wi0CiomJGRehARBCZMr/A3RsFk/4E8FHAAAAAElFTkSuQmCC"
        width="100px" />
      <div>
        <div>the quick brown fox jumps over the lazy dog</div>
        <div>THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG</div>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search