html - 叠加图片不影响透明

标签 html css

我想使用 css 为图像着色,但不影响其透明部分。

例如:如果我在具有透明背景的图像中有一个棕色的小方 block ,我只想将这个方 block 变成另一种颜色。

我找到了这个指南 (http://css-tricks.com/forums/discussion/10828/color-overlay-img/p1),但它也会影响透明部分。

提前致谢

最佳答案

你可以做的是放置一个 <div>在您要着色的尺寸的图像下方。这就是我要做的:

<div class="image">

    <div class="colour"></div>
    <img src="[..].png" alt="Some image" />

</div>

然后是 CSS:

.image{
    width: 100px; /* The image width */
    height: 100px; /* The image height */

    position: relative;
}

.image .colour{
    width: 30px; /* Width of the part you want to colour */
    height: 30px; /* Height of the part you want to colour */

    position: absolute;
    top: 35px; /* Y coordinate */
    left: 35px; /* X coordinate */
}

.image img{
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1; /* Place above the coloured part */
}

关于html - 叠加图片不影响透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11916818/

相关文章:

html - 使用 innerHTML 将 <details> 元素附加到 div 时,div 也会变为 "down"

html - CSS 定位问题 [img|span]

jquery - 使用 jQuery 添加时,Magic CSS3 动画无法正常工作

javascript - 如何让一个div顶部成为其他div的底部

html - 没有选择和选项标签的 Angular react 形式下拉列表

html - Bootstrap 导航的移动菜单

javascript - td 背景着色应用于整个列而不是单个单元格

javascript - 字段变为空白

javascript - 主页中的隐藏内部链接(可以通过 onclick 事件显示)用于 SEO?

html - 导航栏图像问题