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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
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)

How to remove the default style in Bootstrap list? How to remove the default style in Bootstrap list? Apr 07, 2025 am 10:18 AM

The default style of the Bootstrap list can be removed with CSS override. Use more specific CSS rules and selectors, follow the "proximity principle" and "weight principle", overriding the Bootstrap default style. To avoid style conflicts, more targeted selectors can be used. If the override is unsuccessful, adjust the weight of the custom CSS. At the same time, pay attention to performance optimization, avoid overuse of !important, and write concise and efficient CSS code.

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-

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 build a bootstrap framework How to build a bootstrap framework Apr 07, 2025 pm 12:57 PM

To create a Bootstrap framework, follow these steps: Install Bootstrap via CDN or install a local copy. Create an HTML document and link Bootstrap CSS to the &lt;head&gt; section. Add Bootstrap JavaScript file to the &lt;body&gt; section. Use the Bootstrap component and customize the stylesheet to suit your needs.

How to layout bootstrap How to layout bootstrap Apr 07, 2025 pm 02:24 PM

To use Bootstrap to layout a website, you need to use a grid system to divide the page into containers, rows, and columns. First add the container, then add the rows in it, add the columns within the row, and finally add the content in the column. Bootstrap's responsive layout function automatically adjusts the layout according to breakpoints (xs, sm, md, lg, xl). Different layouts under different screen sizes can be achieved by using responsive classes.

How to verify bootstrap date How to verify bootstrap date Apr 07, 2025 pm 03:06 PM

To verify dates in Bootstrap, follow these steps: Introduce the required scripts and styles; initialize the date selector component; set the data-bv-date attribute to enable verification; configure verification rules (such as date formats, error messages, etc.); integrate the Bootstrap verification framework and automatically verify date input when form is submitted.

How to upload files on bootstrap How to upload files on bootstrap Apr 07, 2025 pm 01:09 PM

The file upload function can be implemented through Bootstrap. The steps are as follows: introduce Bootstrap CSS and JavaScript files; create file input fields; create file upload buttons; handle file uploads (using FormData to collect data and then send to the server); custom style (optional).

See all articles