HTML/CSS搭建WordPress首页,section无法显示在hero区域下方求助
问题解决:Info Section 显示位置错误
问题原因
你的.container设置了position: fixed,这个属性会让元素脱离正常文档流,不再占据页面空间,导致后面的.info元素自动填充到页面最顶部,而非跟随在hero区域下方。同时.container的min-height: 100vh让它占满视口高度,fixed定位会让它固定在视口,进一步挤压后续元素的位置。
解决方案
- 移除
.container的position: fixed、top: 0、left: 0属性,让它回到正常文档流中 - 保留
min-height: 100vh保证hero区域占满视口高度,同时保留width: 100%确保宽度铺满 - 可按需调整
z-index,若无需让.container在其他元素下方可直接移除该属性
修改后的代码
HTML代码(仅需修复一处标签闭合错误,结构无需调整)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>danieljmanning - WordPress Web Designer</title> <link rel="stylesheet" href="style.css"> <meta name="description" content="Daniel Manning's Web Design, SEO optimisation and blog writing services. Do you want to rank #1? Get In Touch Today!"> <style> h1,h2,h3,h4,h5,h6,p{ color: #fff; } </style> </head> <div class="container"> <nav> <img src="/images/danieljmanning_logo.png" class="logo" alt="logo"> <ul> <li><a href="#" aria-label="danieljmanning's home page">Home</a></li> <li><a href="#" aria-label="More about who Daniel Manning is and more about his experience in web design and SEO Optimisation">About</a></li> <li><a href="#" aria-label="Daniel Manning's Portfolio Page">Portfolio</a></li> <li><a href="#" aria-label="Web Design, SEO Optimisation and Monthly Support Services with pricing and detail">Services</a></li> <li><a href="#" aria-label="Blog Listing Page">Blog</a></li> </ul> <button class="btn">Apply Now</button> </nav> <div class="content"> <h1>DANIELJMANNING</h1> <h2>AFFORDABLE WORDPRESS WEB DESIGN</h2> <p>I am Daniel J Manning <span class="p-home">here to help you earn more</span> for your business with a website! <span class="p-home">Scroll Down to See My Work.</span></p> <button class="portfolio-btn">PORTFOLIO</button> <button class="apply-btn">APPLY</button> </div> </div> <section class="info"> <h1>Earn more with a website</h1> <p>Having a website gives you a forefront, a place where visitors can find you, giving them the opportunity to come to your business.</p> </section> </body> </html>
修改后的CSS代码
*{ margin: 0; padding: 0; font-family: 'Poppins', sans-serif; box-sizing: border-box; /* 让元素的内边距和边框不会额外增加元素总宽度 */ } .container { width: 100%; min-height: 100vh; background-color: black; background-size: cover; background-position: center; overflow: hidden; /* 移除fixed定位相关属性 */ } nav{ width: 100%; display: flex; align-items: center; justify-content: space-between; color: white; border-bottom: 0.1px solid #303030; } .logo{ width: 20%; cursor: pointer; padding-left: 40px; padding-top: 0px; } nav ul{ list-style: none; width: 100%; text-align: right; padding-right: 20px; } nav ul li{ display: inline-block; margin: 10px 20px; font-size: 16px; cursor: pointer; } nav ul li a{ color: #fff; text-decoration: none; } .btn{ display: flex; width: 190px; height: 55px; padding-top: 17px; padding-bottom: 17px; padding-left: 40px; padding-right: 40px; border: 0; outline: 0; border-radius: 0px; background: #fff; color: black; font-weight: 500; cursor: pointer; align-items: center; justify-content: center; font-size: 14px; } .content{ margin-top: 22%; color: #fff; max-width: 1000px; margin-left: 10%; height: calc(100vh - 80px); /* 减去导航栏高度,避免内容溢出视口 */ } .content h1{ display: block; width: 100%; border-bottom: 1px solid #fff; } .content h2{ padding-top: 20px; padding-bottom: 20px; color: rgb(47, 193, 255); font-size: 18px; } .content p{ padding-bottom: 40px; } .p-home{ font-weight: bold; } .home-btn{ display: flex; width: 180px; height: 50px; padding-top: 17px; padding-bottom: 17px; padding-left: 40px; padding-right: 40px; border: 0; outline: 0; border-radius: 0px; background: #fff; color: black; font-weight: 500; cursor: pointer; align-items: center; justify-content: center; font-size: 14px; } .portfolio-btn{ display: inline-block; width: 180px; height: 50px; padding-top: 17px; padding-bottom: 17px; padding-left: 40px; padding-right: 40px; border: 1px; outline: 1px; border-radius: 0px; background: transparent; color: #fff; font-weight: 500; cursor: pointer; align-items: center; justify-content: center; font-size: 14px; border-top: 1px solid #fff; border-bottom: 1px solid #fff; border-left: 1px solid #fff; border-right: 1px solid #fff; } .apply-btn{ display: inline-block; width: 180px; height: 50px; padding-top: 17px; padding-bottom: 17px; padding-left: 40px; padding-right: 40px; border: 0; outline: 0; border-radius: 0px; background: transparent; color: #fff; font-weight: 500; cursor: pointer; align-items: center; justify-content: center; font-size: 14px; } .info{ margin-top: 100px; padding: 0 10%; /* 增加内边距,和hero区域内容对齐 */ }
额外优化说明
- 修复了HTML中
h2标签的闭合错误(原代码用</h3>闭合h2) - 将导航栏文字移入
<a>标签内部,确保点击文字可触发链接 - 调整
.content的高度计算方式,避免内容溢出视口产生不必要的滚动条
内容的提问来源于stack exchange,提问作者Daniel Manning
相关产品推荐
相关产品推荐

