多Section布局问题:sec3与上方sec-2内容重叠求助
问题解决:页面区域重叠嵌入问题
问题现象
制作多Section页面布局时,class为sec3的区域与上方的sec-2区域重叠,甚至嵌入到sec-2内的txt类div中;调整区域顺序时显示正常,但在sec-2后添加新区域就出现重叠问题。
问题根源
- HTML标签未正确闭合:
sec-2标签内部的<div class="text">和<div class="card">均未闭合,浏览器解析时会将后续的sec3元素错误识别为sec-2的子元素,从而引发重叠嵌入问题。 - CSS属性值缺失单位:
sec-2 .text p.p::before中的bottom: -5未添加单位,导致伪元素布局异常,间接影响整体排版稳定性。
修复步骤
- 补全HTML中所有未闭合的标签,确保
<sec-2>、<div class="text">、<div class="card">都正确闭合。 - 为CSS中
bottom: -5添加单位px,修改为bottom: -5px。
修正后的代码
HTML代码
<sec-2 class="mmargin"> <div class="image"> </div> <div class="text"> <h2>Stay productive, wherever you are</h2> <p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Temporibus doloribus ipsa cum. Sapiente quisquam error magnam odit repellendus nihil dolorem quis </p> <p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Temporibus doloribus ipsa cum. Sapiente quisquam error magnam odit repellendus nihil dolorem quis </p> <p class="p"> See how Fylo works </p> <div class="card"> <div class="image1"> </div> <p> Lorem, ipsum dolor sit amet consectetur adipisicing elit. Qui omnis ducimus veniam, cupidita </p> <div class="av"> <div class="image2"> </div> <div class="txt"> <h3> Kyle Burton </h3> <p> Founder & CEO, Huddle </p> </div> </div> </div> <!-- 闭合card标签 --> </div> <!-- 闭合text标签 --> </sec-2> <!-- section 2 --> <!-- section-3 --> <div class="sec3 mmargin"> <div class="text"> <h2> Get early access today </h2> <p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Aliquid sapiente a alias libero labore rerum assumenda cupiditate illum iure adipisci. Veniam vel voluptatem deleniti officia culpa sed, asperiores eveniet fugiat. </p> </div> <form action=""> <input type="email" placeholder="email@example.com"> <button> Get Started For Free </button> </form> </div>
CSS代码
* { box-sizing: border-box; } :root { --mobile-width: 375px; --light-blue: hsl(224, 93%, 58%); } .mmargin { margin: 50px auto; } body { margin: 0; padding: 0 ; font-family: "Open Sans", sans-serif; font-weight: 400; } h1, h2, h3 { font-family: "Raleway", sans-serif; font-weight: 700; } button:hover { opacity: 0.5; cursor: pointer; } /* sec-2 */ sec-2 { width: var(--mobile-width); display: flex; flex-direction: column; } sec-2 .image { margin-bottom: 50px; } sec-2 .image img { max-width: 100%; } sec-2 .text h2 { font-size: 20px; text-align: center; margin: 30px 0; } sec-2 .text p.p { margin: 50px auto; text-align: center; } sec-2 .text p.p { color: #3da08f; position: relative; } sec-2 .text p.p:hover { opacity: 0.5; cursor: pointer; } sec-2 .text p.p::before { content: ""; width: 175px; height: 2px; background-color: #3da08f; position: absolute; left: 50%; transform: translateX(-50%); bottom: -5px; /* 添加单位px */ } sec-2 .text p.p img { width: 25px; vertical-align: middle; } sec-2 .text .card { display: flex; flex-direction: column; box-shadow: 0 0 10px rgb(197, 197, 197); padding: 20px; } sec-2 .text .card .image1 { width: 40px; } sec-2 .text .card .image1 img { width: 50%; } sec-2 .text .av { display: flex; align-items: center; gap: 15px; margin: 30px 0; } sec-2 .text .av .image2 { width: 50px; } sec-2 .text .av img { max-width: 100%; border-radius: 50%; } sec-2 .text .txt { display: flex; flex-direction: column; gap: 5px; } sec-2 .text .txt h3 { margin: 0; } sec-2 .text .txt p { margin: 0; } /* sec-2 */ /* .sec3 */ .sec3 { width: var(--mobile-width); background-color: hsl(238, 22%, 44%); display: flex; flex-direction: column; justify-content: center; color: white; padding: 50px; } .sec3 .text h2 { text-align: center; } .sec3 .text p { text-align: center; font-size: 18px; line-height: 1.5; } .sec3 form { margin: 30px auto; } .sec3 form input { width: 50%; margin-bottom: 10px; opacity: 0.3; } .sec3 form button { width: 50%; text-align: center; } /* .sec3 */
内容的提问来源于stack exchange,提问作者Mohammed Abdullah
相关产品推荐
相关产品推荐

