如何在另一张图像上叠加10x10px图像?

时间:2010-08-03 01:39:29

标签: html css image css-float

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

<img src="foo.jpg" class="userphoto" width="48" height="48" alt="">

现在,我希望浮动图像左上角的图像(Facebook图标 - 10x10像素)。

这表示用户已通过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;
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:5)

我知道我没有使用你想要的确切尺寸,但这里有一个适合你的例子。 (您需要父“.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>

答案 1 :(得分:3)

首先你需要position它 - 通常是相对的或绝对的。

其次,如果有其他定位元素,则必须设置z-index,但您可能不一定需要。{/ p>

使用top/leftbottom/right组合将图像叠加在另一个上。

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

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

您可能需要设置position:relative,以便AP'd元素的顶部相对于某些东西,而不是包装器或整个页面。