我想要使用负边距堆叠一些图像。但是,由于没有明确定义边界的图像堆叠在视觉上可能会造成混淆,我想我应该在它们周围添加边框。奇怪的是,即使图像堆叠正确,它们的边框也会在前一个元素的下方结束。
为什么这是我得到的行为,有没有办法让边框显示在他们的直观层面?
#second {
margin-top: -50px;
margin-left: 20px;
}
img {
border: 5px ridge green;
display: block;
}
<div><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/RCA_Indian_Head_test_pattern.JPG/312px-RCA_Indian_Head_test_pattern.JPG" height="100" width="auto"><img id="second" src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/RCA_Indian_Head_test_pattern.JPG/312px-RCA_Indian_Head_test_pattern.JPG" height="100" width="auto"></div>
答案 0 :(得分:2)
此问题不会出现在Chrome中,但会出现在Firefox和IE中。简单的解决方案似乎只是将图像的位置设置为相对的:
#second {
margin-top: -50px;
margin-left: 20px;
}
img {
border: 5px ridge green;
display: block;
position: relative;
}
<div><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/RCA_Indian_Head_test_pattern.JPG/312px-RCA_Indian_Head_test_pattern.JPG" height="100" width="auto"><img id="second" src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/RCA_Indian_Head_test_pattern.JPG/312px-RCA_Indian_Head_test_pattern.JPG" height="100" width="auto"></div>
答案 1 :(得分:2)
你也可以使用变换来强制绘制边框:
#second {
margin-top: -50px;
margin-left: 20px;
}
img {
border: 5px ridge green;
display: block;
}
/* FF debug*/
div + div img {
transform:scale(1);/* transform + anyvalue that does nothing new forces the layout to be redrawn */
}
div {float:left;
margin:1em;
<div><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/RCA_Indian_Head_test_pattern.JPG/312px-RCA_Indian_Head_test_pattern.JPG" height="100" width="auto"><img id="second" src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/RCA_Indian_Head_test_pattern.JPG/312px-RCA_Indian_Head_test_pattern.JPG" height="100" width="auto"></div>
<div><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/RCA_Indian_Head_test_pattern.JPG/312px-RCA_Indian_Head_test_pattern.JPG" height="100" width="auto"><img id="second" src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/RCA_Indian_Head_test_pattern.JPG/312px-RCA_Indian_Head_test_pattern.JPG" height="100" width="auto"></div>