如何将 10x10px 图像叠加到另一图像之上?
我的网站上有一个用户头像。简单的图像标签:
<img src="foo.jpg" class="userphoto" width="48" height="48" alt="">
现在,我想在图像的左上角浮动一个图像(Facebook 图标 - 10x10px)。
这表示用户已通过 Facebook 的身份验证。
我该怎么做?图像标签上的 CSS 样式,或者我必须有一个具有绝对定位的单独 div 吗?
不需要是透明的或任何东西,只需要精确定位在左上角。
当然,我不能只是物理地修改图像,因为我需要根据 Facebook 状态确定是否动态覆盖图像。但我希望动态添加一个 css 类。
有什么想法吗?
答案:
结合两个答案就可以了。使用 div 而不是另一个图像。
HTML:
<div class="foo">
<div class="fboverlay"></div>
<a>
<img src="foo.jpg" class="userphoto" width="48" height="48" alt="">
</a>
</div>
CSS:
.foo
{
position: absolute;
top: 0;
right: 0;
}
.fboverlay
{
background-image: url('/image/facebook/logo.gif');
position: absolute;
z-index: 1;
top: 10px;
left: 10px;
width: 10px;
height: 10px;
}
感谢您的帮助。
I have a user avatar on my website. Simple image tag:
<img src="foo.jpg" class="userphoto" width="48" height="48" alt="">
Now, i want to float an image (Facebook Icon - 10x10px) over this image on the top left corner of the image.
This is to signify that the user is authenticated to Facebook.
How can i do this? CSS styling on the image tag, or will i have to have a seperate div with absolute positioning?
Doesn't need to be transparent or anything, just needs to be positioned exactly in the top-left corner.
Of course i cant just physically modify the image, as i need to determine whether to overlay the image dynamically based on the Facebook status. But i was hoping to dynamically add a css class.
Any ideas?
ANSWER:
Got it working with a combination of both answers. Used a div instead of another image.
HTML:
<div class="foo">
<div class="fboverlay"></div>
<a>
<img src="foo.jpg" class="userphoto" width="48" height="48" alt="">
</a>
</div>
CSS:
.foo
{
position: absolute;
top: 0;
right: 0;
}
.fboverlay
{
background-image: url('/image/facebook/logo.gif');
position: absolute;
z-index: 1;
top: 10px;
left: 10px;
width: 10px;
height: 10px;
}
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道我没有使用您想要的确切尺寸,但这里有一个适合您的工作示例。 (您需要父级“.main_photo”div 作为主照片的尺寸。)
I know I'm not using the exact dimensions you wanted, but here's a working example for you. (You would need the parent ".main_photo" div to be the dimensions of the main photo.)
首先,您必须
定位
它 - 通常是相对或绝对。其次,如果有其他定位元素,您必须设置一个
z-index
,尽管您可能不一定需要一个。使用
上/左
或下/右
组合将图像叠加在另一幅图像上。这是没有看到任何 html/css 的建议。根据具体情况,显然会有所不同。
您可能需要设置
position:relative
,以便 AP 元素的顶部相对于某些内容,而不是相对于包装器或整个页面。First you have to
position
it - either relative or absolute usually.Secondly, you have to set a
z-index
if there are other positioned elements, though you may not necessarily need one.Using
top/left
orbottom/right
combinations layer the image over the other one.This is advice without seeing any html/css. It will obviously differ depending on the situation.
You may need to set
position:relative
so the AP'd element's top is relative to something, instead of say, the wrapper or the entire page.