如何在一个DIV中重叠两个图像?

时间:2018-01-20 22:51:31

标签: html image css3 block overlap

如何在一个DIV中重叠两个图像?我有这两个图像,我想把它们叠加在另一个上面

    <div id="personImgDiv">
<img id="scaleImg" src="http://hddfhm.com/images/clipart-110-scale-2.gif" />
<img id="personImg" src="http://vignette4.wikia.nocookie.net/simpsons/images/6/69/The_simpsons_ralph_wiggum-1-.png/revision/latest?cb=20141124210636" />

</div>

我认为这个CSS可以做到......

#scaleImg {
  position: relative;
  top: 0px;
  left: 0px;
}


#personImg {
  position: relative;
  top: 0px;

但事实并非如此。这是演示我的问题的小提琴 - https://jsfiddle.net/vksLv6ew/

2 个答案:

答案 0 :(得分:0)

#personImgDiv {
    position: relative; /* this is the most important bit*/
}
#scaleImg {
    position: absolute;
    top: 0px;
    left: 0px;
}
#personImg {
    position: relative; /* only set this if you want this image to overlap the other, otherwise don't */
}

答案 1 :(得分:0)

对于此行为,容器元素应为 relative ,且子项必须为绝对

即。孩子们将相对于容器具有绝对的定位。这是相应的风格:

&#13;
&#13;
#container {
    position: relative;
}

#container img {
    position: absolute;
    top: 0;
    left: 0;
}
&#13;
<div id="container">
    <img src="http://hddfhm.com/images/clipart-110-scale-2.gif" />
    <img src="http://vignette4.wikia.nocookie.net/simpsons/images/6/69/The_simpsons_ralph_wiggum-1-.png/revision/latest?cb=20141124210636" />
</div>
&#13;
&#13;
&#13;

小提琴玩:https://jsfiddle.net/npsquc6g/