CSS3 filter (filter) to achieve the sample code of gray or black mode of web page

CSS3 filter (filter) to achieve the sample code of gray or black mode of web page

front end

css3,filter can not only achieve the gray effect of web pages, but also assist in achieving the night mode effect. Let’s see how to implement it!

Introduction to filter properties

filter: none | blur() | brightness() | contrast() | drop-shadow() | grayscale() | hue-rotate() | invert() | opacity() | saturate() | sepia() | url();

Tip: Use spaces to separate multiple filters.

Website gray effect

This effects blog was first created when I was summarizing the commonly used CSS effects.

It can be easily achieved with the following code!

The code is as follows:

html {
   filter: grayscale(100%); //IE browser-webkit-filter: grayscale(100%); //Google browser-moz-filter: grayscale(100%); //Firefox-ms-filter: grayscale(100%);
  -o-filter: grayscale(100%);
  filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);
  -webkit-filter: grayscale(1); // Google Chrome}

Compared with the gray effect, the night mode is a little more difficult to achieve!

Night mode judgment

If it is pure web, you can use the prefers-color-scheme query statement in modern browsers.

The syntax is as follows:

no-preference The system does not inform the user of the color scheme to use.
light indicates that the system prefers a light theme.
dark indicates that the system prefers a dark theme.

For example:

/* Dark mode */
@media (prefers-color-scheme: dark) {
    body { background: #333; color: white; }
}
/* Light mode */
@media (prefers-color-scheme: light) {
    body { background: white; color: #333; }
}

If you need to determine the system's dark or light theme in JavaScript code, you can use the native window.matchMedia() method, for example:

// Whether to support dark mode // Return true or false
window.matchMedia("(prefers-color-scheme: dark)").matches;

Night Mode Code

html {
    filter: invert(1) hue-rotate(.5turn);
}

After that, the picture will be reversed, which is very ugly.

img {
    filter: invert(1) hue-rotate(.5turn);
}

Add another layer to the picture, and it will be right side up, so the pure picture will be fine

Therefore, the combination can be set up like this:

html, img {
    filter: invert(1) hue-rotate(.5turn);
}
img {
    opacity: .75;    
}

But there is a problem with the background image and the black shadow. For the background image, you can set it as follows:

@media (prefers-color-scheme: dark) {
    html, img { 
        filter: invert(1) hue-rotate(180deg);
    }
   .bgfilter{
    filter: invert(1) hue-rotate(180deg);
  }
    .some-ele-box {
        box-shadow: none;
    }
}

question

The above settings are only for light backgrounds, and will not work for no background or transparent backgrounds.

In addition, special processing is required for each style. For example, you can use the filter as a global variable to perfectly match the night mode. For example:

@media (prefers-color-scheme: dark) {
    html {
        filter:invert(1) hue-rotate(180deg)
    }

    .dark-img,img {
        filter: invert(1) hue-rotate(180deg)
    }
   // Special processing for background images // Special processing for global colors // background-color: var(--darkColor)
}

This concludes this article about how to use CSS3 filter to create a gray or black mode on a web page. For more information about CSS3 filter, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. We hope that you will support 123WORDPRESS.COM in the future!

<<:  Summary of WEBAPP development skills (notes for mobile website development)

>>:  Detailed explanation of angular two-way binding

Recommend

In-depth understanding of mathematical expressions in CSS calc()

The mathematical expression calc() is a function ...

A brief discussion on the mysql execution process and sequence

Table of contents 1:mysql execution process 1.1: ...

How to use Dayjs to calculate common dates in Vue

When using vue to develop projects, the front end...

React's reconciliation algorithm Diffing algorithm strategy detailed explanation

Table of contents Algorithmic Strategy Single-nod...

CentOS7 firewall and port related commands introduction

Table of contents 1. Check the current status of ...

Tutorial on deploying jdk and tomcat on centos7 without interface

1. Install xshell6 2. Create a server connection ...

Web front-end development CSS related team collaboration

The front-end development department is growing, ...

Solution for mobile browsers not supporting position: fix

The specific method is as follows: CSS Code Copy ...

Analysis of Docker's method for creating local images

The so-called container actually creates a readab...

Node uses koa2 to implement a simple JWT authentication method

Introduction to JWT What is JWT The full name is ...

How to start a Vue.js project

Table of contents 1. Node.js and Vue 2. Run the f...

MYSQL subquery and nested query optimization example analysis

Check the top 100 highest scores in game history ...