Table of Contents
mix-blend-mode: difference
Home Web Front-end CSS Tutorial Cleverly use CSS blending modes to intelligently adapt text to background color

Cleverly use CSS blending modes to intelligently adapt text to background color

Sep 08, 2022 am 11:02 AM
css blend mode

Cleverly use CSS blending modes to intelligently adapt text to background color

There is a piece of text on the page. Can this text be displayed in different colors under different background colors? It is also commonly known as intelligent color change. Like the following:

The text appears as white on a black background and as black on a white background. It seems to be a very complicated effect, but it is actually very easy to implement in CSS. Today I will introduce such a little trick. In CSS, use the mixing mode mix-blend-mode: difference to make the text intelligently adapt. Match the background color. [Recommended learning: css video tutorial]

mix-blend-mode: difference

CSS3 has added a very interesting attribute -- mix-blend-mode, where the Chinese free translations of mix and blend are both mix, then the literal translation of this attribute is mix-blend mode. Of course, we usually call it mix mode. There are some mixing modes as shown in the figure below:

Among them, the protagonist of this article is mix-blend-mode: difference, which means difference. model. This blending mode looks at the color information in each channel, compares the base color to the drawing color, and subtracts the pixel value of the darker pixel from the pixel value of the lighter pixel.

Mixing with white will invert the background color; mixing with black will not change it.

To put it simply, the bright area of ​​the upper layer inverts the color of the lower layer, and the dark area displays the color normally. The effect is the completely opposite color to the original image.

The most common application scenario of this mixing mode is the scenario described at the beginning of the article, which enables text to display different colors on different background colors.

Most suitable for black and white scenes, a very simple DEMO:

<div></div>
Copy after login
div {
    height: 100vh;
    background: linear-gradient(45deg, #000 0, #000 50%, #fff 50%);

    &::before {
        content: "LOREM IPSUM";
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        color: #fff;
        mix-blend-mode: difference;
        animation: move 3s infinite linear alternate;
    }
}
@keyframes move {
    0% {
        transform: translate(-30%, -50%);
    }
    100% {
        transform: translate(-70%, -50%);
    }
}
Copy after login

The effect is as follows:

##CodePen Demo -- linear-gradient Mix-blend-mode

Of course, it is not necessarily black or white. Take a look at the example below. There is such a scenario. Sometimes we don’t quite Determine the final performance value of the background color (may be the background configuration and pass it to the front end), but you need to allow the text to be displayed normally under any background color. At this time, you can also try to use

mix-blend-mode: difference.

<ul class="flex-box">
  <div class="box">
    <p>开通会员查看我的VIP等级</p>
  </div>
   // ..... 
</ul>
Copy after login
div {
    // 不确定的背景色
}
p {
    color: #fff;
    mix-blend-mode: difference;
}
Copy after login
No matter what the background color is, the

element with mix-blend-mode: difference set can display text normally:

CodePen Demo -- mix-blend-mode:difference implements text color adaptive background

# Disadvantages of ##mix-blend-mode:difference

Of course, this method is not perfect, because the color after

mix-blend-mode:difference

is superimposed with the background color , although it can be displayed normally, it is not necessarily the most suitable color or the best color for display effect. When actually used here, in non-black and white scenes, more experiments are needed to make choices.

Finally

To summarize, this article introduces a little trick to use CSS mixed mode to realize text adaptation background display. If you are interested in mixed mode, I recommend you Take another look at my following articles:

    Incredible blending mode mix-blend-mode
  • Incredible blending mode background-blend-mode
  • Two lines of CSS code to achieve arbitrary coloring technology for images
  • Skillfully use CSS to construct gradient color QR codes
  • CSS tricks | Use blending modes to achieve text hollow wave effect
  • ##Explore the CSS 3D failure problem caused by CSS blending modes\filters
  • CSS Art--Use background to create various wonderful backgrounds
  • Okay, this is the end of this article, I hope it will be helpful to you

Original address: https://www.cnblogs.com/coco1s/p/16012545.html

Author: ChokCoco

(Learning video sharing: web front-end)

The above is the detailed content of Cleverly use CSS blending modes to intelligently adapt text to background color. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles