You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

跟随视频制作网站时CSS Hero区域背景覆盖不全及动画效果不符问题求助

解决Hero区域背景覆盖与动画不一致问题

看起来你卡在了Hero区域的两个关键问题上:背景没铺满整个视口,还有文字遮罩动画没达到视频里的效果。我帮你拆解下问题点,然后给出修复方案:

一、修复Hero背景未完全覆盖的问题

你的#hero元素没有设置高度,导致背景图只包裹了内部内容,没有占满整个视口。只需要给#hero添加min-height: 100vh,同时让它负责内容的居中布局:

修改后的#hero CSS:

#hero {
  background-image: url(./img/galaxy.jpeg);
  background-size: cover;
  background-position: top center;
  position: relative;
  z-index: 1;
  min-height: 100vh; /* 让Hero占满整个视口高度 */
  display: flex;
  align-items: center;
  justify-content: center;
}

同时调整通用.container的样式,避免重复设置视口高度:

.container {
  width: 100%;
  max-width: 1200px; /* 可选:限制内容最大宽度,避免大屏幕上文字过宽 */
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

二、修复文字遮罩动画的问题

现在的动画有三个核心问题:

  1. 页面加载时红色遮罩块直接显示,没有从无到有触发动画
  2. 三个<h1>的动画同时播放,没有视频里的依次出现效果
  3. 动画播放时红色块超出h1范围,显得杂乱

修复步骤:

  1. 给<h1>添加overflow: hidden,隐藏遮罩块超出的部分
  2. 设置遮罩<span>的初始状态为width: 0,避免页面加载时直接显示
  3. 给每个<h1>的遮罩添加animation-delay,实现依次动画的效果
  4. 调整动画关键帧,让过渡更流畅

修改后的相关CSS:

#hero h1 {
  display: block;
  width: fit-content;
  font-size: 4rem;
  position: relative;
  color: white;
  overflow: hidden; /* 隐藏遮罩超出h1的部分 */
  margin: 0.8rem 0; /* 可选:增加h1之间的间距,视觉更舒适 */
}

#hero span {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 0; /* 初始宽度为0,动画开始后才展开 */
  background-color: red;
  animation: text_reveal_box 1s ease forwards; /* forwards保持动画结束状态 */
}

/* 给每个h1的遮罩设置不同的动画延迟,实现依次显示 */
#hero h1:nth-child(1) span {
  animation-delay: 0.5s;
}
#hero h1:nth-child(2) span {
  animation-delay: 1.5s;
}
#hero h1:nth-child(3) span {
  animation-delay: 2.5s;
}

@keyframes text_reveal_box {
  0% {
    width: 0;
    left: 0;
  }
  50% {
    width: 100%;
    left: 0;
  }
  100% {
    width: 0;
    left: 100%;
  }
}

最终完整代码

CSS:

@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 {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  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;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
#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;
  overflow: hidden;
  margin: 0.8rem 0;
}
#hero span {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 0;
  background-color: red;
  animation: text_reveal_box 1s ease forwards;
}

#hero h1:nth-child(1) span {
  animation-delay: 0.5s;
}
#hero h1:nth-child(2) span {
  animation-delay: 1.5s;
}
#hero h1:nth-child(3) span {
  animation-delay: 2.5s;
}

#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 {
  0% {
    width: 0;
    left: 0;
  }
  50% {
    width: 100%;
    left: 0;
  }
  100% {
    width: 0;
    left: 100%;
  }
}

HTML:

<!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>

内容的提问来源于stack exchange,提问作者JP Codes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 13:14:11