使 div 内的图像出现在其 div 背景后面

发布于 2025-01-02 10:44:13 字数 426 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不念旧人 2025-01-09 10:44:13

通过为 .artist-container 提供更高的 z-index,您可以将其放置在比子图像更高的堆叠顺序中,尽管子图像的 z-index 始终高于其父图像。

如果想要实现水印的效果,可以:

  1. 将图片作为div的背景,并在其内部放置图片水印。

  2. 将另一个 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:

  1. Make the image the background of the div and place an image watermark inside it.

  2. 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.

画离情绘悲伤 2025-01-09 10:44:13

我使用一些跨度制作了一个小样本,它不会向您的文档添加任何语义内容,并且仍然会保留图像的语义含义。

HTML:

<span class="cover_contain">
   <img src="http://i.imgur.com/hla4q.jpg" alt="[image]" width="128" height="128" />
   <span class="cover_image"></span>
</span>

CSS:

span.cover_contain {
    display: inline-block;
    position: relative;
}
span.cover_image {
    display: block;
    background: url('http://i.imgur.com/5BtFV.png') center center no-repeat;
    width: 128px;
    height: 128px;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

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:

<span class="cover_contain">
   <img src="http://i.imgur.com/hla4q.jpg" alt="[image]" width="128" height="128" />
   <span class="cover_image"></span>
</span>

CSS:

span.cover_contain {
    display: inline-block;
    position: relative;
}
span.cover_image {
    display: block;
    background: url('http://i.imgur.com/5BtFV.png') center center no-repeat;
    width: 128px;
    height: 128px;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

jsFiddle Live Preview

不打扰别人 2025-01-09 10:44:13

将图像作为div的背景图像,将水印作为img

make the image as the background-image of the div and the watermark as the img

兔小萌 2025-01-09 10:44:13

不可能在该元素中的图像前面放置背景。您可以简单地使用主图像作为背景,或者:

您可以做什么

<div class="holder">
    <img src=".." class="main_image">
    <img src=".." class="watermark">
</div>

.holder {
    width:100px;
    height:100px;
    position:relative;
    display:block;
 }

 .main_image {
     position:absolute;
     top:0;
     left:0;
     z-index:0;
 }
.watermark {
     position:absolute;
     top:0;
     left:0;
     z-index:9;
 }

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

<div class="holder">
    <img src=".." class="main_image">
    <img src=".." class="watermark">
</div>

.holder {
    width:100px;
    height:100px;
    position:relative;
    display:block;
 }

 .main_image {
     position:absolute;
     top:0;
     left:0;
     z-index:0;
 }
.watermark {
     position:absolute;
     top:0;
     left:0;
     z-index:9;
 }
七月上 2025-01-09 10:44:13

您可以使用负的 z-index,但在这种情况下,您必须让包装器不具有任何 z-index。这是堆叠上下文的特征之一。

这是一个演示小提琴: http://dabblet.com/gist/1731538

您的代码将是这样的:

.artist-container {
    background:url(images/artist-back.png);
    width:310px;
    height:376px;
    margin-left:-9px;
    position:relative;
}
.artist-container img {
    width:300px;
    height:300px;
    margin-left:5px;
    z-index:-1;
    position:relative;
}

You can use the negative z-index, but in that case you must have the wrapper not to have any z-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:

.artist-container {
    background:url(images/artist-back.png);
    width:310px;
    height:376px;
    margin-left:-9px;
    position:relative;
}
.artist-container img {
    width:300px;
    height:300px;
    margin-left:5px;
    z-index:-1;
    position:relative;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文