如何将两张图片重叠在一张 DIV 中?

How do I overlap two images in one DIV?

如何将两张图片重叠在一起 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;

但事实并非如此。这是证明我的问题的 Fiddle -- https://jsfiddle.net/vksLv6ew/

#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 */
}

对于此行为,container 元素应该是 relative 并且 children 必须是 absolute .

即children 将具有相对于容器的绝对定位。下面是对应的样式:

#container {
    position: relative;
}

#container img {
    position: absolute;
    top: 0;
    left: 0;
}
<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>

Fiddle 一起玩:https://jsfiddle.net/npsquc6g/