如何使用CSS重叠div?

时间:2016-10-27 14:19:28

标签: html css

我所拥有的用户个人资料容器包含一个头部,一个正文(有两个内部主体部分)和一个包含照片的页脚。

enter image description here

我正在寻找一种让div重叠的方法,这样左边的第一个div就会被修复,而下一个div就像前一个重叠一样:

enter image description here

这里有我的

HTML

<div class="userContainer">
    <div class="userHead">
        Head
    </div>
    <div class="userBody">
        <div class="userBody1">
            Body 1
        </div>
        <div class="userBody2">
            Body 2
        </div>
        <div class="userImage">
            <img src="https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg">
        </div>
    </div>
</div>

<div class="userContainer">
    <div class="userHead">
        Head
    </div>
    <div class="userBody">
        <div class="userBody1">
            Body 1
        </div>
        <div class="userBody2">
            Body 2
        </div>
        <div class="userImage">
            <img src="https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg">
        </div>
    </div>
</div>

CSS

.userContainer {
    position: relative;
    float: left;
    margin-right: 20px;
    color: white;
    height:100px !important;
}

.userHead {
    background-color: #f05624;
    width: 40px;
    height: 40px;
  line-height: 40px;
    text-align: center;
    font-size: 14px;
    color: white;
    border-radius: 200px;
    margin-left: 20px;
    font-weight: 400;
}

.userBody {
    width: 75px;
    position: absolute;
    top: 30px;
    left: 3px;
    border-top-right-radius: 150px;
    border-top-left-radius: 150px;
    border-bottom-right-radius: 20px;
    border-bottom-left-radius: 20px;
    font-weight: 400;
    text-align: center;
}

.userBody1 {
    height: 25%;
    border-top-right-radius: 150px;
    border-top-left-radius: 150px;
    background-color: #f05624;
    padding-top: 10px;
}

.userBody2 {
    height: 15%;
    background-color: #f05624;
    font-size: 10px !important;
}

.userImage img{
    height: 100%;
  width: 100%;
}

检查jsfiddle

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

添加另一行css,explicilty将第一个容器左边距设置为0 px。

.userContainer:nth-child(1){
    margin-left:0px;
}

将您的其他容器边距设置为margin-left -20px

.userContainer {
    position: relative;
    float: left;
    margin-right: 20px;
    color: white;
    height:100px !important;
    margin-left:-30px;
}

以下是一个例子:

https://jsfiddle.net/edqpyvs1/2/

更新的解决方案:

添加此JS代码以确保容器具有比下一个容器更大的z-index ...

var containers = document.getElementsByClassName("userContainer");

var zindexvalue = 1000;

for(index = containers.length - 1 ; index >= 0; index--){
  var container = containers[index];
  container.style.zIndex = zindexvalue;
  zindexvalue = zindexvalue + 1;
}

<强>

<强> Check it out.

答案 1 :(得分:0)

你可以在div1:

中嵌套div2
<div id="div1">
    <img src="\img1.png" />

    <div id="div2">
        <img src="\img1.png" />
    </div>

</div>

答案 2 :(得分:0)

以下是使用z-index的解决方案:https://jsfiddle.net/edqpyvs1/1/

.userContainer {
    position: relative;
    float: left;
    margin-right: 20px;
    color: white;
    height:100px !important;
    z-index: 100;
}
.userContainer + .userContainer{
    z-index: 99;
    left: -50px;
}