CSS:如何制作div块*而不是*在其父容器中占用空间

Dav*_*rks 12 html css positioning css-position

如果我有2个div标签:

<div id="parentDiv">
   <div id="childDiv"><!-- other html --></div>
   <!-- parent div html -->
</div>
Run Code Online (Sandbox Code Playgroud)

我希望内容<div id="childDiv">与内容重叠<!-- parent div html -->.

原因(如果这看起来像糟糕的代码设计人):我需要在谷歌网站黑客攻击的解决办法,我不能在侧栏导航添加自定义代码,只在主HTML的空间,我想浮动一个div不带任何空间并将其相对放置在侧杆上以绕过强制限制.

我可以做任何事情,除了停止childDiv占用它的混蛋主页容器中的空间.

S.V*_*ser 15

你可以给它一个绝对的位置,并用边距导航它.


    #childDiv {
      position: absolute;
      margin-top: -100px;
      margin-left: -100px;
    }

  • 您还需要`position:relative`父 (3认同)

Mat*_*ell 8

#parentDiv {
    position: relative;
}
#childDiv {
    position:absolute;
    top:0;
    left:0;
    width:100%;
}
Run Code Online (Sandbox Code Playgroud)

如果您需要在侧边栏“外部”增加内容或重叠内容,您可以使用负边距来增加 childDiv 的宽度或将其向上移动(以覆盖无法覆盖的填充或边距)。

如果你需要让#childDiv 覆盖整个#parentDiv,你可以使用一些JavaScript 来设置childDiv 的最小高度,然后添加一个彩色背景或其他东西来覆盖任何内容。

var parentHeight = jQuery('#parentDiv').height();
jQuery('#childDiv').css('min-height',parentHeight + 'px');
Run Code Online (Sandbox Code Playgroud)


Mr *_*ter 7

怎么样简单

#childDiv {height:0; overflow:visible;}

但你可能希望它有背景颜色,嗯?嗯.