使 div 内的图像出现在其 div 背景后面
我有一个 div,其背景部分透明并带有水印。在 div 内部,我正在调用一个图像,但我希望该图像出现在 div 背景后面,这样我就可以让带水印的透明 div 背景出现在图像上。这可能吗?这是我的 css 不起作用......
.artist-container {
background:url(images/artist-back.png);
width:310px;
height:376px;
margin-left:-9px;
z-index:331;
position:relative;
}
.artist-container img {
width:300px;
height:300px;
margin-left:5px;
z-index:330;
position:relative;
}
I have a div that has background that is partly transparent with a watermark. Inside of the div I'm calling an image but I want that image to appear behind the background of the div so I can have the watermarked transparent div background appear over the image. Is that possible? Here's the css that I have that isn't working...
.artist-container {
background:url(images/artist-back.png);
width:310px;
height:376px;
margin-left:-9px;
z-index:331;
position:relative;
}
.artist-container img {
width:300px;
height:300px;
margin-left:5px;
z-index:330;
position:relative;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通过为
.artist-container
提供更高的 z-index,您可以将其放置在比子图像更高的堆叠顺序中,尽管子图像的 z-index 始终高于其父图像。如果想要实现水印的效果,可以:
将图片作为div的背景,并在其内部放置图片水印。
将另一个 div 绝对放置在
.artist-container
中,其尺寸与图像相同,并且图像的 z-index 较高,以水印作为背景。By giving
.artist-container
a higher z-index, you are placing it higher in the stacking order than the child image, though children always have a higher z-index than their parents.If you want to give the effect of a watermark, you can:
Make the image the background of the div and place an image watermark inside it.
Position another div within
.artist-container
absolutely, with the same dimensions as that of the image and with a higher z-index of the image, with the watermark as the background.我使用一些跨度制作了一个小样本,它不会向您的文档添加任何语义内容,并且仍然会保留图像的语义含义。
HTML:
CSS:
jsFiddle 实时预览
I whipped up a small sample using some spans, which won't add any semantic content to your document and will still maintain the semantic meaning of your image.
HTML:
CSS:
jsFiddle Live Preview
将图像作为div的背景图像,将水印作为img
make the image as the background-image of the div and the watermark as the img
不可能在该元素中的图像前面放置背景。您可以简单地使用主图像作为背景,或者:
您可以做什么
it's not possible to put a background in front of an image of the image is in that element. You can simply use the main image as background, or:
what you could do
您可以使用负的
z-index
,但在这种情况下,您必须让包装器不具有任何z-index
。这是堆叠上下文的特征之一。这是一个演示小提琴: http://dabblet.com/gist/1731538
您的代码将是这样的:
You can use the negative
z-index
, but in that case you must have the wrapper not to have anyz-index
. It's one of the features of stacking context.Here is a demo fiddle: http://dabblet.com/gist/1731538
And the code for you would be something like this: