HTML/CSS文本与搜索栏堆叠布局问题求助
实现文本堆叠在搜索栏上方的布局修复
问题根源
- 页面
body使用了display: flex且默认flex-direction: row,导致<h1>和搜索栏容器.overlay-content被当作横向排列的flex子元素,出现并排效果。 align-items: center让所有flex子元素垂直居中,加上h1的大字号,导致搜索栏顶部被遮挡。- 搜索栏样式里的
overflow: scroll和无效的overflow-y: center属性多余,影响正常显示。 - 原代码重复定义了
.container样式,造成样式冲突。
修复方案
- 将
<h1>和.overlay-content包裹到统一的.main-content容器中,设置该容器为垂直flex布局,让内部元素从上到下堆叠。 - 调整
body的flex设置,确保整个页面内容垂直居中,同时内部容器保持垂直流。 - 清理重复CSS样式,修正搜索栏的样式属性,保证显示正常。
- 调整h1的margin值,确保和搜索栏之间有合适间距。
修改后的完整代码
<!DOCTYPE html> <html> <head> <title>My Page</title> <style> html, body { height: 100%; margin: 0; padding: 0; } body { display: flex; justify-content: center; align-items: center; background-color: #f9f9f9; } .main-content { display: flex; flex-direction: column; align-items: center; width: 100%; } .background { position: absolute; top: 0; bottom: 0; left: 0; right: 0; background-color: #000000; z-index: -1; } h1 { font-family: 'Space Mono', monospace; font-size: clamp(3rem, 10vw, 10rem); color: white; padding: 0rem clamp(1rem, 2vw, 3rem); border-radius: clamp(0.4rem, 0.75vw, 1rem); text-align: center; margin: 0 0 30px 0; } #mySearch { display: block; width: 50%; min-width: 300px; font-size: 18px; padding: 11px; border: 1px solid #ddd; margin-bottom: 20px; box-sizing: border-box; } #myMenu { list-style: none; padding: 0; display: flex; flex-wrap: wrap; justify-content: center; gap: 20px; } .game { width: 200px; height: 310px; border-radius: 8px; cursor: pointer; border: 1px solid white; display: flex; flex-direction: column; } .game:hover { border: 1px solid #674ea7; } .gameimg { width: 100%; height: 265px; border-bottom: 1px solid white; border-radius: 8px 8px 0px 0px; object-fit: cover; } .gameinfo { width: 100%; text-align: center; overflow: hidden; text-overflow: ellipsis; color: white; font-size: 16px; line-height: 35px; margin: 0; } </style> </head> <body> <div class="main-content"> <h1 data-value="GAMES">GAMES</h1> <div class="overlay-content"> <form action="/action_page.php"> <input type="text" placeholder="Search.." name="search" id="mySearch" onkeyup="myFunction()" title="Type in a category"> <ul id="myMenu"> <li> <div class="game" onclick="goto(),acilspace()"> <img src="" class="gameimg"> <p class="gameinfo">Acilspace</p> </div> </li> <li> <div class="game" onclick="goto(),astriods()"> <img src="" class="gameimg"> <p class="gameinfo">Astriods</p> </div> </li> <li> <div class="game" onclick="goto(),vex5()"> <img src="" class="gameimg"> <p class="gameinfo">Vex 5</p> </div> </li> <li> <div class="game" onclick=""> <img src="" class="gameimg"> <p class="gameinfo">c</p> </div> </li> </ul> </form> </div> </div> <canvas class="background"></canvas> <script src="https://cdnjs.cloudflare.com/ajax/libs/particlesjs/2.2.2/particles.min.js"></script> <script> var particles = Particles.init({ selector: '.background', sizeVariations: 3, color: ['#949398', '#674ea7'], connectParticles: true }); function myFunction() { var input, filter, ul, li, p, i; input = document.getElementById("mySearch"); filter = input.value.toUpperCase(); ul = document.getElementById("myMenu"); li = ul.getElementsByTagName("li"); for (i = 0; i < li.length; i++) { p = li[i].getElementsByClassName("gameinfo")[0]; if (p.innerText.toUpperCase().indexOf(filter) > -1) { li[i].style.display = ""; } else { li[i].style.display = "none"; } } } const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; let interval = null; document.addEventListener("DOMContentLoaded", () => { const h1 = document.querySelector("h1"); const targetValue = h1.dataset.value; let iteration = 0; clearInterval(interval); interval = setInterval(() => { h1.innerText = h1.innerText .split("") .map((letter, index) => { if(index < iteration) { return targetValue[index]; } return letters[Math.floor(Math.random() * 26)] }) .join(""); if(iteration >= targetValue.length){ clearInterval(interval); } iteration += 1 / 3; }, 75); }); </script> </body> </html>
额外优化说明
- 新增
.main-content容器,通过flex-direction: column实现h1和搜索栏的垂直堆叠。 - 修正搜索栏宽度设置,添加
min-width保证小屏幕可用性,移除无效的overflow属性。 - 游戏列表改用
flex-wrap实现响应式排列,替换原float布局,符合现代CSS规范。 - 将
.gameinfo从<a>标签改为<p>标签,语义更合理,同步调整搜索过滤逻辑适配该修改。
内容的提问来源于stack exchange,提问作者Oz Abramovich
相关产品推荐
相关产品推荐

