制作div"重叠"容器div

时间:2016-08-08 20:34:15

标签: html css

我正在尝试制作类似于此的页面: enter image description here

我正在尝试将个人资料图片转到"下面"壁纸div。这是我的相关代码:

CSS代码:

<div class="col-md-6 roundedCorners" style="background-color: white">
    <div class="wallpaper">
        <div class="row">
            <div class="col-md-6" style="text-align: left">
                <div class="profile-picture">
                    &nbsp;
                </div>
            </div>
            <div class="col-md-6">

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

HTML代码:

character

现在,除了将圆形轮廓图片垂直居中放置在图像中之外,一切正常。我希望它漂浮在壁纸之外,如上图所示。我错过了什么?

3 个答案:

答案 0 :(得分:2)

目前,您的代码不包含任何positioning个样式。

您最有可能希望position: absolute用于个人资料图片(使用bottomleft属性来设置位置)。执行此操作时,您还需要将position: relative添加到壁纸中,以便绝对定位与壁纸相关。

这是经过修改的代码,可以帮助您朝着正确的方向前进:

.profile-picture {
    /* set the position to absolute */
    position: absolute;
    /* set the bottom to be slightly outside the wallpaper boundary.  Adjust this number as desired */
    bottom: -50px;
    /* set the left where desired.  Note I removed your margin-left, since you don't need both left and margin-left */
    left: 10px;
    display: inline-block;
    width: 150px;
    height: 150px;
    -ms-border-radius: 50%;
    border-radius: 50%;
    background-repeat: no-repeat;
    background-position: center center;
    -ms-background-size: cover;
    background-size: cover;
    background-image: url("../Images/profiles/default.png")
}

.wallpaper {
    /* set the position to relative, so the profile picture position is in relationship to the wallpaper container */
    position: relative;
    /* you may (or may not) need this.  The profile is outside the wallpaper, so would be considered "overflow". */
    overflow: visible;
    width: 100%;
    -ms-background-size: cover;
    background-size: cover;
    background-image: url("../images/Wallpapers/default.png")
}

要详细了解您希望将壁纸设置为position: relative的原因及其工作原理,请查看此文章:https://css-tricks.com/absolute-positioning-inside-relative-positioning/

答案 1 :(得分:0)

可能的解决方案是定位你的元素。

在您的容器中position:relative;.wallpaper),您可以将您的个人资料图片相对于容器置于绝对位置,因此bottom: -70px;可以将其下移到您需要的确切位置

.profile-picture {
  position: absolute;
  bottom: -70px;
  display: inline-block;
  margin-left: 10px;
  width: 150px;
  height: 150px;
  -ms-border-radius: 50%;
  border-radius: 50%;
  border: 5px solid white;
  background-repeat: no-repeat;
  background-position: center center;
  -ms-background-size: cover;
  background-size: cover;
  background-image: url(https://unsplash.it/150/150)
}
.wallpaper {
  position: relative;
  width: 100%;
  height: 200px;
  -ms-background-size: cover;
  background-size: cover;
  background-image: url(https://unsplash.it/2000/200)
}
<div class="col-md-6 roundedCorners" style="background-color: white">
  <div class="wallpaper">
    <div class="row">
      <div class="col-md-6" style="text-align: left">
        <div class="profile-picture">
          &nbsp;
        </div>
      </div>
      <div class="col-md-6">

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

答案 2 :(得分:0)

除了其他答案之外,您还可以将个人资料图片放在&#34;下面&#34;使用z-index样式的壁纸。将以下内容添加到每个类样式:

.profile-picture {
    z-index:4;
}
.wallpaper {
    z-index:5;
}