简体   繁体   English

CSS堆栈div彼此叠置

[英]css stack divs on top of each other

I cant seem to figure it out. 我似乎无法弄清楚。 I have seen all examples and questions, but i cant get those divs ontop of each other. 我已经看过所有示例和问题,但是我无法使这些div相互重叠。

They have and border of 1px and for some reason the border shifts the divs to the right and bottom. 它们的边框为1px,由于某种原因,边框将div移至右侧和底部。

I know if i disable the borders then all the code online works just fine, but i need those dotted lines and with them, the divs dont align anymore. 我知道如果我禁用边框,那么所有在线代码都可以正常工作,但是我需要那些虚线,并且与它们一起,div不再对齐。 Z-index doesnt work, the blue div doesnt show ontop. Z-index不起作用,蓝色div不显示在顶部。

https://jsfiddle.net/x1L2jxnx/14/ https://jsfiddle.net/x1L2jxnx/14/

<style>
    .content {
      width: 64px;
      height: 64px;
      margin: 32px;
      background-color: #FFD800;
      position: relative;
    }

    .content div {
      width: inherit;
      height: inherit;
      position: absolute;
      border-style: dotted;
    }

    .margin {
      border-color: #03A9F4;
      z-index: 3;
    }

    .border {
      border-color: #black;
      z-index: 2;
    }

    .padding {
      border-color: #808080;
      z-index: 1;
    }
</style>

<div class="content">
  <div class="margin">
    <div class="border">
      <div class="padding">

      </div>
    </div>
  </div>
</div>

I assume this is what you want. 我想这就是你想要的。 The width describes the inner width your <div> has. width描述您的<div>具有的内部宽度。 The border width comes on top of it. 边框宽度位于其顶部。 So every <div> has additional twice the border width to its inherited width. 因此,每个<div>的边框宽度都是其继承宽度的两倍。

 .content { width: 64px; height: 64px; margin: 32px; background-color: #FFD800; position: relative; } .content div { top:0; bottom:0; right:0; left:0; position: absolute; border-style: dotted; } .margin { border-color: #03A9F4; z-index: 3; } .border { border-color: #black; z-index: 2; } .padding { border-color: #808080; z-index: 1; } 
 <div class="content"> <div class="margin"> <div class="border"> <div class="padding"> </div> </div> </div> </div> 

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

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