Sample code for changing the color of a png image through a CSS3 filter

Sample code for changing the color of a png image through a CSS3 filter

This method uses the drop-shadow filter in CSS3 to generate a projection of any color on the non-transparent part of the png image, and then hides the original image, thereby achieving the purpose of changing the color of the image. See the effect first

Example images

Next is the code

<p><strong>Original Icon</strong></p>
<i class="icon icon-del"></i>
<p><strong>Icons that can change color</strong></p>
<i class="icon"><i class="icon icon-del"></i></i>

.icon {
    display: inline-block;
    width: 20px; height: 20px;
    overflow: hidden;
}
.icon-del {
    background: url(delete.png) no-repeat center;
}
.icon > .icon {
    position: relative;
    left: -20px;
    border-right: 20px solid transparent;
    -webkit-filter: drop-shadow(#0033FF 20px 0);
    filter: drop-shadow(#0033FF 20px 0);
}

Note that one of the key points

border-right: 20px solid transparent;

In Chrome, if the main body of an element is not visible on the page in any way, its drop-shadow is invisible; if the main body is visible even by 1 pixel, the drop-shadow is completely visible.

Since we used overflow:hidden to hide the original icon, its projection cannot be displayed normally on the Chrome browser. We solve this problem by adding a transparent border.

Summarize

This is the end of this article about changing the color of PNG images through CSS3 filter. For more relevant content about changing the color of PNG images with CSS, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  HTML background color gradient achieved through CSS

>>:  Vue implements bottom query function

Recommend

Introduction to ApplicationHost.config (IIS storage configuration area file)

For a newly created website, take ASP.NET MVC5 as...

How to implement mobile web page size adaptation

I finally finished the project at hand, and the m...

HTML Tutorial: Collection of commonly used HTML tags (4)

Related articles: Beginners learn some HTML tags ...

MySQL Failover Notes: Application-Aware Design Detailed Explanation

1. Introduction As we all know, in the applicatio...

Vue realizes the logistics timeline effect

This article example shares the specific code of ...

Linux kernel device driver kernel time management notes

/****************** * Linux kernel time managemen...

Docker binding fixed IP/cross-host container mutual access operation

Preface Previously, static IPs assigned using pip...

Centos7 installation of MySQL8 tutorial

MySQL 8 new features: My personal opinion on MySQ...

JS implements request dispatcher

Table of contents Abstraction and reuse Serial Se...

Detailed explanation of top command output in Linux

Preface I believe everyone has used the top comma...

CSS example code for implementing sliding doors

The so-called sliding door technology means that ...

Simple steps to create a MySQL container with Docker

Preface We have already installed Docker and have...

Tomcat uses Log4j to output catalina.out log

Tomcat's default log uses java.util.logging, ...