Warm tip: This article is reproduced from serverfault.com, please click

Animate CSS Uncentering Div

发布于 2020-11-28 05:58:33

I have a div that is at the center of the screen, but animate CSS keeps uncentering it. In this snippet the div is centered until I add the animate classes. How would I fix this so it stays at the center while retaining the animation?

#main {
  font-size: 48px;
  color: black;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" integrity="sha512-c42qTSw/wPZ3/5LBzD+Bw5f7bSF2oxou6wEb+I/lqeaKV5FDIfMvvRp772y4jcJLKuGUOpbJMdg/BTl50fJYAw==" crossorigin="anonymous" />

<div id="main" class="animate__animated animate__backInDown">
      Hello There
</div>
Questioner
theag
Viewed
0
huan feng 2020-11-28 14:10:57

You should wrap the content inside a div.

#main {
  font-size: 48px;
  color: black;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" integrity="sha512-c42qTSw/wPZ3/5LBzD+Bw5f7bSF2oxou6wEb+I/lqeaKV5FDIfMvvRp772y4jcJLKuGUOpbJMdg/BTl50fJYAw==" crossorigin="anonymous" />
    
    <div id="main">
      <div class="animate__animated animate__backInDown">Hello There</div>
    </div>