[英]How to put background-img in front of img inside a div
I have a div which has a background-img and and img element that is inside this div. 我有一个div,该div中有background-img和img元素。 I want the background-img to be shown on top of the img.
我希望将background-img显示在img顶部。 Both images are positioned relative.
两个图像都相对放置。 I know this sounds a little weird, I mean this is a "background" image, but if there is any way for doing this that would really help me This is my code in a simple way:
我知道这听起来有些怪异,我的意思是这是一个“背景”图像,但是如果有任何方法可以真正帮助我,这就是简单的代码:
<div id="sample_div">
<img src="path of the image">
</div>
<style>
#sample_div{
position: relative;
background-img= url(another path);
}
#sample_div img{
position:relative;
}
</style>
More info about these images: The background image is a floor with the 1800*150 dimension and I want to show 2 characters standing on top of it. 关于这些图像的更多信息:背景图像是尺寸为1800 * 150的地板,我想在其上面显示2个字符。 I want to align these 2 images in a way that they stand on top of this floor image and their feet go a little under the top surface of the floor.
我要对齐这两个图像,使它们站立在此地板图像的顶部,并且其脚稍稍低于地板的顶部表面。 Thought this might help to figure out what I reallt want to do
认为这可能有助于弄清我想做的事情
z-index: 100;
Z-Index, will always reposition of things, whether you want things in front or the back. Z-Index,将始终重新定位事物,无论您想要事物在前面还是后面。 And of course, to make things go back, make it a negative number.
当然,要使结果回溯,请使其为负数。 It dont have to be 100, I always just use larger numbers.
它不必是100,我总是只使用较大的数字。 Hope this helps
希望这可以帮助
You can use pseudo elements for this case: 在这种情况下,可以使用伪元素:
Demo: http://jsfiddle.net/GCu2D/364/ 演示: http : //jsfiddle.net/GCu2D/364/
HMTL: HMTL:
<div id="sample_div">
<img src="http://www.lorempixel.com/400/200/sports/2" />
</div>
CSS: CSS:
#sample_div {
position: relative;
}
#sample_div:after {
position:absolute;
content:url(http://www.lorempixel.com/400/200/sports/1);
top:0;
bottom:0;
right:0;
left:0;
z-index:1;
}
#sample_div img {
position:relative;
}
Don't need to move it anywhere, just hide the child image when you need to: 无需将其移动到任何地方,只需在需要时隐藏子图像即可:
.hide-inner-contents {
background-image:url(src);
}
.hide-inner-contents > img{
display:none;
}
<div class="hide-inner-contents"><img/></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.