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

Implementation of docker-compose deployment of zk+kafka+storm cluster

Cluster Deployment Overview 172.22.12.20 172.22.1...

Analyze Tomcat architecture principles to architecture design

Table of contents 1. Learning Objectives 1.1. Mas...

Complete Tutorial on Deploying Java Web Project on Linux Server

Most of this article refers to other tutorials on...

Write a React-like framework from scratch

Recently I saw the article Build your own React o...

Install three or more tomcats under Linux system (detailed steps)

If you want to install multiple tomcats, you must...

Detailed tutorial on building an ETCD cluster for Docker microservices

Table of contents Features of etcd There are thre...

Detailed explanation of the basic use of centos7 firewall in linux

1. Basic use of firewalld start up: systemctl sta...

JavaScript to show and hide images

JavaScript shows and hides pictures, for your ref...

How to use DPlayer.js video playback plug-in

DPlayer.js video player plug-in is easy to use Ma...

MySQL 8.0 user and role management principles and usage details

This article describes MySQL 8.0 user and role ma...

Example of ellipsis when CSS multi-line text overflows

Ellipses appear when multi-line text overflows Th...

How to optimize MySQL index function based on Explain keyword

EXPLAIN shows how MySQL uses indexes to process s...