如何将 10x10px 图像叠加到另一图像之上?

发布于 2024-09-12 12:54:14 字数 981 浏览 5 评论 0原文

我的网站上有一个用户头像。简单的图像标签:

<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 技术交流群。

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

发布评论

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

评论(2

眼趣 2024-09-19 12:54:14

我知道我没有使用您想要的确切尺寸,但这里有一个适合您的工作示例。 (您需要父级“.main_photo”div 作为主照片的尺寸。)

.main_photo{
    position:relative;
    width:80px;
    height:80px
}
.inlay{
    position:absolute;
    top:5px;
    right:5px
}
<div class="main_photo">
    <img src="https://secure.gravatar.com/avatar/?s=200&d=mm&r=pg" />
    <img class="inlay" src="https://www.rit.edu/news/lib/views/public/images/dateline_images/facebook_icon.gif" />
</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.)

.main_photo{
    position:relative;
    width:80px;
    height:80px
}
.inlay{
    position:absolute;
    top:5px;
    right:5px
}
<div class="main_photo">
    <img src="https://secure.gravatar.com/avatar/?s=200&d=mm&r=pg" />
    <img class="inlay" src="https://www.rit.edu/news/lib/views/public/images/dateline_images/facebook_icon.gif" />
</div>

明月松间行 2024-09-19 12:54:14

首先,您必须定位它 - 通常是相对或绝对。

其次,如果有其他定位元素,您必须设置一个 z-index,尽管您可能不一定需要一个。

使用上/左下/右组合将图像叠加在另一幅图像上。

这是没有看到任何 html/css 的建议。根据具体情况,显然会有所不同。

#el-on-top { position:absolute; top:0; left:0; z-index:1; }

您可能需要设置 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 or bottom/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.

#el-on-top { position:absolute; top:0; left:0; z-index:1; }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文