search
HomeWeb Front-endCSS TutorialA 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.

A CSS tip for getting the theme color of an image that you deserve to know (share)

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:

A CSS tip for getting the theme color of an image that you deserve to know (share)

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:

A CSS tip for getting the theme color of an image that you deserve to know (share)

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:

A CSS tip for getting the theme color of an image that you deserve to know (share)

<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.

A CSS tip for getting the theme color of an image that you deserve to know (share)

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:

A CSS tip for getting the theme color of an image that you deserve to know (share)

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:

  1. can only roughly get the main color of the picture, but cannot be very precise, and filter: blur(50px) 50px Some debugging is required
  2. 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!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
So Many Color LinksSo Many Color LinksApr 13, 2025 am 11:36 AM

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.

How Auto Margins Work in FlexboxHow Auto Margins Work in FlexboxApr 13, 2025 am 11:35 AM

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

Moving Rainbow UnderlinesMoving Rainbow UnderlinesApr 13, 2025 am 11:27 AM

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

New Year, New Job? Let's Make a Grid-Powered Resume!New Year, New Job? Let's Make a Grid-Powered Resume!Apr 13, 2025 am 11:26 AM

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

One Way to Break Users Out of the Habit of Reloading Too MuchOne Way to Break Users Out of the Habit of Reloading Too MuchApr 13, 2025 am 11:25 AM

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

Domain-Driven Design With ReactDomain-Driven Design With ReactApr 13, 2025 am 11:22 AM

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

Detecting Inactive UsersDetecting Inactive UsersApr 13, 2025 am 11:08 AM

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   ZapierWufoo ZapierApr 13, 2025 am 11:02 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

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

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

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

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools