简体   繁体   English

在不使用 Position:absolute 的情况下将两个 div 放在一起

[英]Placing two divs on top of each other without using Position:absolute

I have been trying to add two divs on top of each other without using Position:absolute, and it is not working.我一直在尝试在不使用 Position:absolute 的情况下添加两个 div,但它不起作用。

Does anyone know how to do that??有谁知道这是怎么做到的吗?? Thank you.谢谢你。

<div class="row" style="background-color:aqua;">
  <div class="col-xs-12 col-sm-12 col-md-12" >   

        <div style="left:0px;">
               <asp:Label ID="lblEmail" runat="server" Text="Label" />
        </div>

        <div style="left:0px;">
             <asp:Button ID="btnJoin" runat="server" Text="a button" 
                  CssClass="btn btn-lg btn-primary"/>
        </div>

  </div>
</div>

you can do this using grid. 您可以使用网格来做到这一点。

 .parent { display: grid; grid-template-columns: 1fr; } .parent div { padding: 50px; background-color: rgba(0,0,0,0.5); grid-row-start: 1; grid-column-start: 1; } 
 <div class="parent"> <div class="child1"> 1 </div> <div class="child2"> 2 </div> </div> 

If both elements are in normal flow, you could use a negative margin-top on the element which you want to layer on top of it's predecessor: 如果两个元素都正常流动,则可以在要层叠在其前身之上的元素上使用负的margin-top:

 body { margin: 0; padding: 0; } .box { padding: 20px; background: coral; } .box--overlay { background: rgba(0, 255, 255, 0.4); text-align: right; margin-top: -40px; /*Change this to a value of your choosing*/ } 
 <div class="box">I am box 1</div> <div class="box box--overlay">I am box 2</div> 

Try giving position relative to both the elements and align the second element on top of the first by using z-index 2 to the second element and top:(); 尝试给出相对于两个元素的位置,并通过将z-index 2与第二个元素和top :();对齐,将第二个元素放在第一个元素的顶部。 left:(); 剩下:(); that would not effect your navbar. 那不会影响您的导航栏。

This is one way to do it though a cleaner way might be to get the width of the element you're covering using javascript so that you can dynamically cover elements without statically putting in exact pixel value for margin-left, as is the case for css for blue div这是一种方法,尽管一种更简洁的方法可能是使用 javascript 获取要覆盖的元素的宽度,这样您就可以动态覆盖元素,而无需静态地为 margin-left 输入精确的像素值,例如css 用于蓝色 div

 .red { background: rgba(255,0,0,0.5); display: inline-block; }.blue { background: rgba(0,0,255,0.5); display: inline-block; margin-left: -24px; }
 <div class="red">red</div> <div class="blue">blue</div>

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

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