简体   繁体   English

将div与其他两个元素重叠

[英]Overlapping div over two other elements

I'm trying to lay a div's content partly over a header element and a section element, but I need some help with the structure/styling. 我试图将div的内容部分地放置在header元素和section元素上,但是我在结构/样式上需要一些帮助。

Visually, I've almost achieved what I want with absolute / relative positioning, but it feels hacky because I'm using a negative top value. 在视觉上,我几乎可以通过absolute / relative定位实现我想要的功能,但是由于我使用的是负的top价值,因此感觉很不客气。 The other issue is that as I bring up the div's content with a negative top , its parent's height remains the same, creating unwanted space below the div content. 另一个问题是,当我用负top显示div的内容时,其父级的高度保持不变,从而在div内容下方创建了多余的空间。

I also tried just giving #about .row a negative margin-top , which didn't cause the unwanted space issue, but due to the structure, the div's content appeared underneath the header. 我还尝试只给#about .row负的margin-top ,这不会引起不必要的空间问题,但是由于结构的原因,div的内容出现标题的下面

I'm sure there's a better way to approach this, so I'm hoping someone here can help me out! 我敢肯定有更好的方法可以解决这个问题,所以我希望这里的人能帮助我! Thanks in advance! 提前致谢!

Current markup: 当前标记:

<header class="bg-primary" id="header">
    <div class="container">
        <div class="col-lg-10 col-lg-offset-1 text-center">
          <h2 class="section-heading">Header</h2>
        </div>
    </div>
</header>

<section class="bg-primary" id="about">
    <div class="container">
        <div class="row">
            <div class="col-lg-10 col-lg-offset-1 text-center">
                <h2 class="section-heading">About</h2>
                <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
            </div>
        </div>
    </div>
</section>

CSS: CSS:

* {box-sizing: border-box;}
body {margin: 0 auto; text-align: center; font: 24px/34px Arial, sans-serif;}
header {background-color: #eaeaea; padding: 100px;}
section {background-color: #337ab7; margin: 0 auto; padding: 50px; width: 100%;}

section#about {position: absolute;}
#about .container {position: relative;}
#about .row {position: relative; top: -120px; background-color: #fff; color: #333; margin: 0 auto; padding: 50px; max-width: 1170px;}

I also threw together a quick Fiddle to replicate the sections in questions: https://jsfiddle.net/jcmo4hua/ . 我还拼凑了一个快速提琴,以复制有问题的部分: https : //jsfiddle.net/jcmo4hua/

Instead of using position:relative; 而不是使用position:relative; and negative top , use negative margin-top . 和负数top ,请使用负margin-top

fiddle 小提琴

You should use margin-top:-120px on the .container element, which is a parent of the element you are giving top:-120px at the moment. 您应该在.container元素上使用margin-top:-120px ,这是您目前给top:-120px的元素的父元素。

This way you will retain expected effect and clean markup. 这样,您将保留预期的效果并清除标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM