简体   繁体   English

在一个背景图像上添加一个<img>本身?

[英]Adding a background-image on an <img> itself?

Is it possible to add a background-image on an <img> tag?是否可以在<img>标签上添加background-image I cannot create a <div> around it for now (due to other restraints).我现在不能在它周围创建一个<div> (由于其他限制)。 I'm wondering if it's possible on just the <img> tag.我想知道是否可以仅在<img>标签上使用。 Thanks.谢谢。

 img { width: 500px; background-image: url('https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.png'); background-repeat: no-repeat; z-index:100; }
 <img src="https://www.cieau.com/wp-content/uploads/2019/11/eau_nature.jpg">

Use padding and add the image there:使用填充并在那里添加图像:

 img { width: 300px; padding:0 0 50px 0; background: url('https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.png') bottom right/auto 50px; background-repeat: no-repeat; }
 <img src="https://www.cieau.com/wp-content/uploads/2019/11/eau_nature.jpg">

Like below by considering multiple backgrounds and by keeping only the padding area:通过考虑多个背景并仅保留填充区域,如下所示:

 img { width: 0; height:0; padding:300px 500px 0 0; background: url('https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.png') bottom right/auto 50px, url(https://www.cieau.com/wp-content/uploads/2019/11/eau_nature.jpg) center/cover; background-repeat: no-repeat; }
 <img src="https://www.cieau.com/wp-content/uploads/2019/11/eau_nature.jpg">

Yes, this is possible - but the img would need some transparency for you to see the background image of course.是的,这是可能的 - 但 img 需要一些透明度才能让您看到背景图像。

This snippet is the same as yours but with the actual img src removed and a width and height put on the img element so it has some dimensions, since it isn't in this case picking them up from the img itself.此片段与您的相同,但删除了实际的 img src 并在 img 元素上放置了宽度和高度,因此它具有一些尺寸,因为在这种情况下它不是从 img 本身中提取它们的。 You can see the StackOverflow logo emerging from underneath.您可以看到 StackOverflow 徽标从下方出现。

 img { width: 500px; background-image: url('https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.png'); background-repeat: no-repeat; z-index:100; }
 <img src="" width=200 height=200>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM