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

如何解决Hero Banner下方内容重叠?修改定位后文本错乱

Hero Banner与下方内容重叠问题解决方案

问题描述

使用全屏Hero Banner后,在其下方添加content div时,内容文字与Banner发生重叠。推测问题源于Hero Banner的定位设置,但修改定位属性后,Banner上的文本布局出现混乱,切换定位方式也未解决问题,寻求可行修复方案。

原代码

CSS代码

body,
html {
  height: 100%;
  width: 100%;
  font-size: 100%;
  background-color: #333;
}

h1 {
  font-size: 5em;
}

h2 {
  font-size: 2.5em;
}

#content {
  font-family: sans-serif;
  margin: 0.5%;
  font-size: 170%;
  border: 10px outset orange;
  color: white;
  text-align: justify;
  padding: 1.5%;
  line-height: 125%;
  position: absolute;
}


/* The hero image */

.hero-image {
  background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("blackred.webp");
  height: 100%;
  width: 100%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: center;
}


/* Place text in the middle of the image */

.hero-text {
  font-family: 'Arial';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
  text-align: center;
}


/* Navbar container */
.navbar {
  overflow: hidden;
  background-color: #333;
  font-family: Arial;
}


/* Links inside the navbar */
.navbar a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}


/* The dropdown container */

.dropdown {
  float: left;
  overflow: hidden;
}


/* Dropdown button */

.dropdown .dropbtn {
  font-size: 16px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}


/* Add a red background color to navbar links on hover */

.navbar a:hover,
.dropdown:hover .dropbtn {
  background-color: white;
  color: black;
}


/* Dropdown content (hidden by default) */

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}


/* Links inside the dropdown */

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}


/* Add a grey background color to dropdown links on hover */

.dropdown-content a:hover {
  background-color: #ddd;
}


/* Show the dropdown menu on hover */

.dropdown:hover .dropdown-content {
  display: block;
}

.button {
  background-color: black;
  border: 2px solid white;
  border-color: #FFFFF;
  color: white;
  padding: 10px 30px;
  text-align: center;
  display: inline-block;
  font-size: 16px;
}

.button:hover {
  background-color: white;
  color: black;
}

HTML代码

<html>

<body>
  <div class="navbar">
    <a href="home.htm">Home</a>
    <a href="signup.htm">Sign up</a>
    <div class="dropdown">
      <button class="dropbtn">Our teams ▼
    </button>
      <div class="dropdown-content">
        <a href="LOL.htm">League of legends</a>
        <a href="valorant.htm">Valorant</a>
        <a href="RL.htm">Rocket league</a>
      </div>
    </div>
  </div>
  <div class="hero-image">
    <div class="hero-text">
      <img src="RCLOGO.png" alt="RCLOGO">
      <h2>E sports team</h2>
      <a class="button" href="signup.htm">Join now</a>
    </div>
    <div class="content">
    </div>
    <div class="footer">
    </div>
  </div>

</body>

</html>

修复方案及修改后的代码

核心修改点

  • 修正.hero-image的position属性为relative:原代码中position: center是无效值,改为relative后,内部绝对定位的.hero-text会相对于Banner容器定位,保证文本布局正常。
  • 将.content从.hero-image内部移出,作为同级元素:这样content会自然排在Banner下方,不会被Banner覆盖。
  • 移除#content的position: absolute:让content回到正常文档流,避免脱离布局导致的重叠。

修改后的CSS代码

body,
html {
  height: 100%;
  width: 100%;
  font-size: 100%;
  background-color: #333;
  margin: 0;
  padding: 0;
}

h1 {
  font-size: 5em;
}

h2 {
  font-size: 2.5em;
}

#content {
  font-family: sans-serif;
  margin: 0.5%;
  font-size: 170%;
  border: 10px outset orange;
  color: white;
  text-align: justify;
  padding: 1.5%;
  line-height: 125%;
}


/* The hero image */

.hero-image {
  background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("blackred.webp");
  height: 100vh;
  width: 100%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;
}


/* Place text in the middle of the image */

.hero-text {
  font-family: 'Arial';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
  text-align: center;
}


/* Navbar container */
.navbar {
  overflow: hidden;
  background-color: #333;
  font-family: Arial;
  position: relative;
  z-index: 2;
}


/* Links inside the navbar */
.navbar a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}


/* The dropdown container */

.dropdown {
  float: left;
  overflow: hidden;
}


/* Dropdown button */

.dropdown .dropbtn {
  font-size: 16px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}


/* Add a red background color to navbar links on hover */

.navbar a:hover,
.dropdown:hover .dropbtn {
  background-color: white;
  color: black;
}


/* Dropdown content (hidden by default) */

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}


/* Links inside the dropdown */

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}


/* Add a grey background color to dropdown links on hover */

.dropdown-content a:hover {
  background-color: #ddd;
}


/* Show the dropdown menu on hover */

.dropdown:hover .dropdown-content {
  display: block;
}

.button {
  background-color: black;
  border: 2px solid white;
  border-color: #FFFFFF;
  color: white;
  padding: 10px 30px;
  text-align: center;
  display: inline-block;
  font-size: 16px;
}

.button:hover {
  background-color: white;
  color: black;
}

修改后的HTML代码

<html>

<body>
  <div class="navbar">
    <a href="home.htm">Home</a>
    <a href="signup.htm">Sign up</a>
    <div class="dropdown">
      <button class="dropbtn">Our teams ▼</button>
      <div class="dropdown-content">
        <a href="LOL.htm">League of legends</a>
        <a href="valorant.htm">Valorant</a>
        <a href="RL.htm">Rocket league</a>
      </div>
    </div>
  </div>
  <div class="hero-image">
    <div class="hero-text">
      <img src="RCLOGO.png" alt="RCLOGO">
      <h2>E sports team</h2>
      <a class="button" href="signup.htm">Join now</a>
    </div>
  </div>
  <div id="content">
    示例内容:这是Banner下方的内容区域,现在不会和Banner重叠了。
  </div>
  <div class="footer">
  </div>

</body>

</html>

额外说明

  • 新增body, html的margin: 0; padding: 0,避免默认边距导致的不必要滚动条。
  • 将.hero-image的height: 100%改为height: 100vh,确保Banner始终占满视口高度,不受父元素高度影响。
  • 修正.button的border-color: #FFFFF为#FFFFFF,修复无效颜色值问题。
  • 给.navbar添加position: relative和z-index: 2,确保导航栏下拉菜单不会被Banner遮挡。

内容的提问来源于stack exchange,提问作者Niko Hipkins

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 03:25:16