重叠的div元素

10

我需要两个重叠的 div,看起来像下面这个例子。

    ------------------------------------
   |                      |             |
   |   ------------------ |             |
   |  '                  '|             |
   |  '                  '|             |
   |  '                  '|             |
   |  '                  '|             |
   |  '                  '|             |
   |  '                  '|             |
   |  '                  '|             |
   |  --------------------|             |
   |  '                  '|             |
   |  '                  '|             |
   |   -------------------|             |
   |                      |             |
   |                      |             |
    ------------------------------------

但是不知道怎么做。这里是我的实验场,请问有谁知道我错在哪里了吗?

编辑1:我有一个左侧div和一个右侧div。左侧div有重叠的div,右侧div是普通的div。我猜你们中大多数人会感到困惑,认为右侧div是重叠的,实际上左侧div中有两个div需要重叠。

抱歉让大家感到困惑。


对于编辑:只需交换我给你的示例的z-index。 - Ben Rowe
4个回答

21
我认为你想要这样的东西:

html

<div class="parent">
  <div class="a"></div>
  <div class="b"></div>
</div>

CSS

.parent {
  position: relative;
}

.a {
  position: absolute;
  width: 100px;
  height: 100px;
  z-index: 100;
  background: red;
}

.b {
  position: absolute;
  width: 80px;
  height: 180px;
  z-index: 110;
  left: 10px;
  background: blue;
  top: 10px;
}

编辑:在您的情况下,parent = 内容容器,a/b = 左侧容器/右侧容器


0

我已经改成这样:

#rightContainer {
    /*float:right ;*/
    width:20%;
    /*margin:0px;*/
    margin-top: 30px;
    margin-left: 30px;
    padding:0px;
    position:relative;
    background-color:Lime;
}

您可以根据需要更改“30px”。我只在FireFox中进行了测试。


0
你需要使用 topleftz-index 属性来实现。

-1

我不太确定如何使用 z-index 属性

但我认为在样式标签中可能会有所帮助

margin:-50px -50px 0px 0px;

减号属性有效,它的效果如何应该完全取决于测试。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接