Significantly optimize PNG image size with CSS mask (recommended)


This article is welcome to share and aggregate, full text reprint is not necessary, respect copyright, the circle is so big, if urgent need can contact authorization.

1、 Png images that cannot be further compressed

For example, there is a PNG image as shown below (size 1 / 2 is shown), and there is still 122k after compression with top-level PNG tools.

The original address is here:

The size of PNG is as follows:

If the project has only one picture like this, and if the page has many PNGS of this size all at once, the impact on the performance of the first loading will be very obvious. For example, the four card pictures in the figure below have 500K.

As the background of the card is also complex graphics, the corners must be transparent, and it is certainly not possible to change it to JPG format (the corners will become solid color). Is there no way to further optimize it?

Yes, that is to use CSS mask to significantly optimize the size of PNG images.

2、 Mask image and PNG size optimization

mask-imageThe mask has such a function that only the area where the mask image exists can be displayed. If it is an arbitrary mask image with transparent edges and corners, it can make the edges and corners of JPG image white and transparent.

The method is feasible, demo here.

The specific methods are as follows.

1. PNG to jpg

First, keep the PNG image as JPG, and 50% of the image quality is enough, as shown in the figure below:

At this point, the size of the picture can be reduced by more than 50%!

2. Make solid PNG according to the outline of PNG

The reason why PNG pictures are large in size is that they have too many colors. If we turn to solid colors, the size can be reduced by more than 100 times.

The size of PNG picture filled with pure black color is less than 1K

3. Use mask to make JPG corners white and transparent

Suppose that the JPG card chart uses the<img>The HTML code is as follows:

<img src="card.jpg">

Use the following CSS code:

img {
    -webkit-mask-image: url(card-mask.png);
    mask-image: url(card-mask.png);

You can have the same effect as the original 122k PNG image, with transparent edges and small size.

Because the four corners of card-mask.png are transparent, the corners of card.jpg become transparent after card-mask.png is used as a mask image.

4. Cross domain restriction of mask image

With the security upgrade of chrome and other browsers, the mask image currently has cross domain access restrictions, and there will be an error similar to the following:

Access to image at ‘…/card-mask.png’ from origin ‘’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

We can change the solid color mask graph into Base64 format

img {

Seeing is believing, you can click here: PNG image size optimization demo using CSS mask

The final effect of JPG format card is shown in the figure below (four corners are transparent)

3、 Effect comparison after optimization

The size comparison of the four images before and after optimization is as follows:

The original 4 pictures support 458k, the optimized picture size is 197k + 1K, the size is reduced56.8%


mask-imageThis kind of mobile terminal has been supported for a long time, and the traditional syntax is used here. There is no compatibility problem at all, so you can rest assured to use it.


This article about using CSS mask to significantly optimize the size of PNG image is introduced here. For more relevant CSS mask content, please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!