


A CSS tip for getting the theme color of an image that you deserve to know (share)
This article will share with you a little trick to use CSS to obtain the theme color of images. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Background
The reason was that a classmate in the WeChat technical group asked, is there any way to get the main color of the picture? There is a picture, get its main color:
Use the obtained color value to implement a function like this - there is a picture in the container, and you want the background color It can be adapted to the main color of the picture, like this:
Everyone has some suggestions, some said to use Canvas for calculations, and some recommended special open source libraries, which are all very good.
So, can this function be achieved using CSS?
Sounds like a bit of a fantasy, can CSS still achieve this effect? emm, using CSS can indeed approximate the main color of the image in a clever way. When the requirements for the main color are not particularly precise, this is a way. Let’s find out together.
Use filter: blur() and transform: sacle() to get the theme color of the picture
Here, we use the blur filter and magnification effect to get an approximate result The theme color of the image.
Suppose we have a picture like this:
<div></div>
Use the blur filter to apply the blur filter to the picture:
div { background: url("https://i0.wp.com/airlinkalaska.com/wp-content/uploads//aurora-A CSS tip for getting the theme color of an image that you deserve to know (share)?resize=1024%2C683&ssl=1"); background-size: cover; filter: blur(50px); }
Let’s see the effect. Use a relatively large blur filter to blur(50px)
the image. The blurred image will look a bit like that, but there are some blurred edges. Try using overflow
to crop it.
Next, we need to remove the blurred edges, and use transform: scale()
to enlarge the effect, refocus the color, and transform it slightly Next code:
div { position: relative; width: 320px; height: 200px; overflow: hidden; } div::before { content: ""; position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: url("https://i0.wp.com/airlinkalaska.com/wp-content/uploads//aurora-A CSS tip for getting the theme color of an image that you deserve to know (share)?resize=1024%2C683&ssl=1"); background-size: cover; // 核心代码: filter: blur(50px); transform: scale(3); }
The result is as follows:
In this way, we use CSS to get the main color of the image, and the effect is still good!
You can click here for the complete code: CodePen Demo -- Get the main color of the image by filter and scale
Disadvantages
Of course, this solution also has some minor problems:
- can only roughly get the main color of the picture, but cannot be very precise, and
filter: blur(50px)
50px
Some debugging is required - The blur filter itself consumes performance. If there are multiple backgrounds obtained by this method on a page, it may have a certain impact on performance. , you need to make certain trade-offs when actually using it
Finally
Okay, this article ends here. It introduces a little trick to use CSS to get the theme color of the image. I hope it will be useful to you. You are helpful:)
Original address: https://segmentfault.com/a/1190000039979112
Author: chokcoco
More programming related For knowledge, please visit: programming video! !
The above is the detailed content of A CSS tip for getting the theme color of an image that you deserve to know (share). For more information, please follow other related articles on the PHP Chinese website!

There's been a run of tools, articles, and resources about color lately. Please allow me to close a few tabs by rounding them up here for your enjoyment.

Robin has covered this before, but I've heard some confusion about it in the past few weeks and saw another person take a stab at explaining it, and I wanted

I absolutely love the design of the Sandwich site. Among many beautiful features are these headlines with rainbow underlines that move as you scroll. It's not

Many popular resume designs are making the most of the available page space by laying sections out in a grid shape. Let’s use CSS Grid to create a layout that

Page reloads are a thing. Sometimes we refresh a page when we think it’s unresponsive, or believe that new content is available. Sometimes we’re just mad at

There is very little guidance on how to organize front-end applications in the world of React. (Just move files around until it “feels right,” lol). The truth

Most of the time you don’t really care about whether a user is actively engaged or temporarily inactive on your application. Inactive, meaning, perhaps they

Wufoo has always been great with integrations. They have integrations with specific apps, like Campaign Monitor, Mailchimp, and Typekit, but they also


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools