为什么 ::after 元素在动画过程中会改变它的位置?

问题描述 投票:0回答:0

我正在试验伪元素,我想知道为什么

.container::after
元素在动画期间改变它的位置。这是 HTML 和 CSS 代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>
<body>
  <header>
    <h1>stuff</h1>
  </header>
  <main>
    <div class="container">I am a container</div>
  </main>
</body>
</html>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  height: 100vh;
}

header {
  position: sticky;
  top: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0.3rem;
  background-color: steelblue;
  z-index: 1;
}

main {
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  align-items: center;
  font-size: 2rem;
  background-color: grey;
  height: 100%;
  position: relative;
}

div {
  border: 10px solid red;
  background-color: white;
}

main:hover .container {
  transform: rotate(360deg);
  color: purple;
  transition: all 2s;
}

main .container::after {
  position: absolute;
  top: 200px;
  left: 100px;
  background-color: white;
  color: gold;
  content: "after div";
}

我试着把

position: relative 

在 div 元素上。但这不是我想要的。显然 ::after 元素在动画期间与其父元素 (div) 相关,但这是为什么呢?我可以避免吗?

css css-animations styling pseudo-element
© www.soinside.com 2019 - 2024. All rights reserved.