无法居中 CSS

Can't center CSS

我正在尝试跟随视频一起创建一个完整的网站项目。目前正在尝试将动画添加到一个 header 中,该 header 被分成三个并且都在 .无论出于何种原因,在用背景覆盖 header 并应用动画时,我无法复制他在视频中所做的事情。当我编写相同的代码时,header 没有被完全阻止,我的动画与视频中的动画不一样。

应该是什么样子proper way the header should be covered

我编码后的样子:enter image description here

@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Serif:wght@200&display=swap');
* {
  Padding: 0;
  margin: 0;
  box-sizing: border-box;
}

html {
  font-size: 10px;
  color: white;
  font-family: 'IBM Plex Serif', serif;
}

a {
  text-decoration: none;
}

body {
  background: rgb(123, 127, 153);
}

.container {
  min-height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}


/* Hero Section */

#hero {
  background-image: url(./img/galaxy.jpeg);
  background-size: cover;
  background-position: top center;
  position: relative;
  z-index: 1;
}

#hero::after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  background-color: blue;
  opacity: .2;
  z-index: -1;
}

#hero h1 {
  display: block;
  width: fit-content;
  font-size: 4rem;
  position: relative;
  color: white;
}

#hero span {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  background-color: red;
  animation: text_reveal_box 1s ease;
}

#hero .cta {
  display: inline-block;
  padding: 10px 30px;
  position: relative;
  left: 55px;
  align-items: center;
  color: red;
  background-color: transparent;
  border: 2px solid red;
  font-size: 2rem;
  text-transform: uppercase;
  letter-spacing: .1rem;
  margin-top: 30px;
  transition: .3s ease;
  transition-property: background-color, color;
}

#hero .cta:hover {
  color: white;
  background-color: red;
}


/* End Hero Section */


/*keyframes*/

@keyframes text_reveal_box {
  50% {
    width: 100%;
    left: 0;
  }
  100% {
    width: 0;
    left: 100%;
  }
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" type="text/css" href="style.css">
  <title>FullSite</title>
  <meta charset="UTF-8">
</head>

<body>
  <!-- Start Hero Section -->
  <section id="hero">
    <div class="hero container">
      <div>
        <h1><span>Hello...</span></h1>
        <h1><span>We are</span></h1>
        <h1><span>J.P.Astronomics</span></h1>
        <a href="#" type="button" class="cta">Portfolio</a>
      </div>
    </div>
  </section>
  <!-- End Hero Section -->

  <!-- Start Astronomical Goals-->
  <section id="service">
    <div class="service container">
      <div class="serivce-top">
        <h1 class="section-title">Astronomical Goals</h1>
      </div>
    </div>
  </section>
  <!--End Astronomical Goals-->
</body>

</html>

您必须删除以下 css 并添加一些样式才能实现设计

  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%; 

@import url("https://fonts.googleapis.com/css2?family=IBM+Plex+Serif:wght@200&display=swap");
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

html {
  font-size: 10px;
  color: white;
  font-family: "IBM Plex Serif", serif;
}

a {
  text-decoration: none;
}

body {
  background: rgb(123, 127, 153);
}

.container {
  min-height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}


/* Hero Section */

#hero {
  background-image: url(./img/galaxy.jpeg);
  background-size: cover;
  background-position: top center;
  position: relative;
  z-index: 1;
}

#hero::after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  background-color: blue;
  opacity: 0.2;
  z-index: -1;
}

#hero h1 {
  font-size: 4rem;
  position: relative;
  color: white;
}

.content-wrapper {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

#hero span {
  background-color: red;
  color: red;
  animation: text_reveal_box 1s ease;
}

#hero .cta {
  display: inline-block;
  padding: 10px 30px;
  position: relative;
  align-items: center;
  color: red;
  background-color: transparent;
  border: 2px solid red;
  font-size: 2rem;
  text-transform: uppercase;
  letter-spacing: 0.1rem;
  margin-top: 30px;
  transition: 0.3s ease;
  transition-property: background-color, color;
}

#hero .cta:hover {
  color: white;
  background-color: red;
}


/* End Hero Section */


/*keyframes*/

@keyframes text_reveal_box {
  50% {
    width: 100%;
    left: 0;
  }
  100% {
    width: 0;
    left: 100%;
  }
}
<section id="hero">
  <div class="hero container">
    <div class="heading-wrapper">
      <h1><span>Hello...</span></h1>
      <h1><span>J.P.Astronomics</span></h1>
      <h1><span>We are</span></h1>
      <a href="#" type="button" class="cta">Portfolio</a>
    </div>
  </div>
</section>
<!-- End Hero Section -->

<!-- Start Astronomical Goals-->
<section id="service">
  <div class="service container">
    <div class="serivce-top">
      <h1 class="section-title">Astronomical Goals</h1>
    </div>
  </div>
</section>

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

a {
  text-decoration: none;
}

body {
  background: rgb(123, 127, 153);
}

.container {
  width: min(80%, 1200px);
  margin: auto;
  min-height: 100vh;
}

.hero {
  border: 2px solid red;
  display: grid;
  place-items: center;
}

.hero-text {
  border: 2px solid yellow;

  display: inline-block;
  line-height: 1;
  margin: 5em auto;
}
span {
  display: inline-block;
  margin-right: auto;

  position: relative;
  font-size: clamp(1.5rem, 5vw + 10px, 3rem);
  animation: reveal_anim 1s forwards;
}
span::before {
  content: "";
  position: absolute;
  background-color: red;
  display: block;
  width: 100%;
  height: 100%;
  z-index: -1;
}

a {
  text-decoration: none;
}

.cta {
  display: inline-block;
  margin-right: auto;
  margin-top: 1em;
  padding: 0.2em 0.5em;
  font-size: clamp(1rem, 5vw, 2rem);
  border: 2px solid blue;
  animation: reveal_anim 1s forwards;
  animation-delay: 500ms;
  opacity: 0;

}

@keyframes reveal_anim {
  0% {
    transform: translateX(50%);
  }
  50% {
    opacity: 0.5;
  }
  100% {
    transform: translateX(0%);
    opacity: 1;
  }
}
<!-- Start Hero Section -->
<section id="hero">
  <div class="hero container">
    <div class="hero-text">
      <h1><span>Hello...</span><br>
        <span>We are</span><br>
        <span>J.P.Astronomics</span>
      </h1>
      <a href="#" type="button" class="cta">Portfolio</a>
    </div>
  </div>
</section>
<!-- End Hero Section -->

<!-- Start Astronomical Goals-->
<section id="service">
  <div class="service container">
    <div class="serivce-top">
      <h1 class="section-title">Astronomical Goals</h1>
    </div>
  </div>
</section>
<!--End Astronomical Goals-->

抱歉对 HTML 做了一点改动。