HTML网页导航下方元素与banner互相遮盖 嵌套布局问题求助
问题说明
我需要了解HTML/CSS嵌套相关规则,同时在开发HTML网站时无法实现预期显示效果:已完成banner与导航栏开发,但尝试在导航栏下方添加内容时,新增内容要么覆盖banner,要么被banner遮盖。
相关代码
HTML代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> Home: Old Barber </title> </head> <body id="body"><link rel="stylesheet" href="test.css"> <div id="wrapper"> <header id="header"> Welcome </header> <div id="bannerHolder"> <div class="banner"> OLD BARBER </div> <nav id="nav"> <a href="test_template.html"> Home </a> <a href="test_template.html"> Products </a> <a href="test_template.html"> About </a> </nav> </div> </div> <div class="container"> <img src="https://www.open.edu/openlearn/ocw/pluginfile.php/1654608/mod_oucontent/oucontent/93155/8a822f73/b6b08556/mse_s6_figure_3.jpg" alt="Barber" class="img"></img> <div class="overlay"> <div class="text">Shop</div> </div> </body> </html>
CSS代码
/* Imported fonts */ @import url('https://fonts.googleapis.com/css2?family=Merienda:wght@700&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Italianno&display=swap'); #wrapper { background: orange; display: block; margin: auto; left: 0; position: absolute; right: 0; text-align: center; top: 0; width: 100%; } #bannerHolder { background: #aaa; display: block; height: 100px; } #bannerHolder .banner { background-image: url(https://p0.zoon.ru/d/d/5ce4efe774cfee5d9265c8ee_5d6e753f1d26b.jpg); background-position: center; background-size: 100%; color: white; font-family: 'Merienda', cursive; font-size: 800%; height: 200%; left: 0; margin: auto; position: relative; right: 0; text-align: center; text-shadow: 2px 2px 4px black; } #body{ background-color: gray; } #nav{ background-color: orange; color: black; display: flex; justify-content: space-around; padding: 0.5%; position: relative; text-align: center; } #h1{ font-family: 'Italianno', cursive; } .container{ position: relative; width: 50%; } .img{ display: block; height: auto; width: auto; } .overlay{ position: absolute; top: 0; bottom: 0; left: 0; right: 0; height: auto; width: auto; opacity: 0; transition: .5s ease; background-color: orange; } .container:hover .overlay{ opacity: 1; } .text{ color: white; font-size: larger; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); }
补充说明
将#wrapper的position: absolute;属性移除后的效果如下图,但不符合预期显示效果,需要banner、导航栏等元素铺满页面的顶部、左侧和右侧。
问题原因与修复方案
问题根源
#wrapper设置了position: absolute,会让元素脱离普通文档流,后续的.container元素无法识别它的高度,会默认从页面顶部开始排布,因此出现重叠问题。- 浏览器会给body默认添加8px左右的外边距,所以移除
position: absolute后顶部元素无法铺满左右两侧。 #bannerHolder固定设置了height: 100px,但内部的.banner设置了height: 200%,内容溢出父容器,高度计算异常。
具体修改步骤
- 消除body默认边距,实现元素左右铺满
#body{ background-color: gray; margin: 0; /* 新增属性 */ }
- 调整
#wrapper样式,移除脱离文档流的属性,保留100%宽度自然铺满顶部
#wrapper { background: orange; display: block; text-align: center; width: 100%; /* 移除left: 0; position: absolute; right: 0; top: 0; margin: auto; 这些不需要的属性 */ }
- 调整banner容器高度设置,避免内容溢出
#bannerHolder { background: #aaa; display: block; /* 移除固定高度height: 100px,让容器高度随内容自动撑开 */ } #bannerHolder .banner { /* 原有属性保留 */ height: 200px; /* 替换原有的height: 200%,固定高度避免百分比计算异常 */ }
修改完成后,#wrapper会正常占据页面顶部空间,后续的.container会自动排在导航栏下方,不会出现重叠,同时banner和导航栏会铺满页面顶部和左右两侧,符合需求。
内容的提问来源于stack exchange,提问作者user13592496
相关产品推荐
相关产品推荐

