Home Web Front-end CSS Tutorial Introduction to how to use CSS3 to write grayscale filters to create black and white photo effects

Introduction to how to use CSS3 to write grayscale filters to create black and white photo effects

Mar 13, 2017 pm 05:55 PM

This article mainly introduces the method of using CSS3 to write grayscale filters to create black and white photo effects. The filter in CSS3 is very powerful. The article also introduces the method that is compatible with IE. Friends who need it can refer to it

The filter function in

CSS3 allows us to process images easily and conveniently without using PhotoShop or a lot of JavaScript and PHP codes. This property is already supported by the newer Firefox, Safari, and Chrome browsers, and we can simulate this effect through comprehensive alternative technologies-even IE browsers.

In this article, we will use the standard test image Lena Söderberg (Translator’s Note: Lena’s beautiful photo is used as a standard test image for image compression) as a demonstration, and use CSS to convert it into a black and white image. Below I will explain how to use this feature of CSS to adjust hue, blur, brightness, contrast, and some other effects. Rendering:
Introduction to how to use CSS3 to write grayscale filters to create black and white photo effects

CSS3 grayscale filter

Using CSS3 to dilute the color of an image couldn’t be easier. We can write this CSS statement as a class, so that when we encounter a picture with the desired effect, we can directly add a class.

img.desaturate { filter: grayscale(100%); }
Copy after login


Of course, when current browsers use CSS3, they need to add their own experimental prefixes for browser functions, so the first thing we have to do is Write the prefix of the browser:

img.desaturate { filter: grayscale(100%);   
-webkit-filter: grayscale(100%);   
-moz-filter: grayscale(100%);   
-ms-filter: grayscale(100%);   
-o-filter: grayscale(100%);   
}
Copy after login


If you want to use it on a certain picture, it is very simple, add a class:

<img src=lena-söderberg.png alt="Lena Söderberg" style=width:512px;height:512px class=desaturate>
Copy after login


That’s it. ## Add an SVG filter effect. This feature is currently only available in Chrome 18+, and other browsers will soon add support. To get the same effect in Firefox 4+, we may need to use SVG filters. I created a new separate file saturate.svg with the following code:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg">   
<filter id="greyscale">   
<feColorMatrix type="matrix" values="0.3333 0.3333 0.3333 0 0
0.3333 0.3333 0.3333 0 0
0.3333 0.3333 0.3333 0 0
0  0  0  1 0"/>   
</filter>   
</svg>
Copy after login




Don't be intimidated by this SVG code - although the matrix sequence above is a bit complicated. I recommend you copy and paste this code directly into a common "small file". I will write another article to introduce the above matrix changes in detail, so I won’t go into details here. Coupled with the above SVG file reference, the CSS code we want to insert into the HTML page is as follows:

img.desaturate{   
filter: grayscale(100%);   
-webkit-filter: grayscale(100%); -moz-filter: grayscale(100%);   
-ms-filter: grayscale(100%); -o-filter: grayscale(100%);   
filter: url(desaturate.svg#greyscale);   
}
Copy after login


Compatibility for IE:

Now our code is compatible with future browsers and the latest versions of Chrome and Firefox 4+. In order to add IE 6-9 to the compatibility list, we need to use Microsoft's clumsy but effective filter filter:

img.desaturate{   
filter: grayscale(100%);   
-webkit-filter: grayscale(100%); -moz-filter: grayscale(100%);   
-ms-filter: grayscale(100%); -o-filter: grayscale(100%);   
filter: url(desaturate.svg#greyscale);   
filter: gray;   
}
Copy after login


If you still want to browse the old version of Webkit kernel Browser compatibility:

img.desaturate{   
filter: grayscale(100%);   
-webkit-filter: grayscale(100%); -moz-filter: grayscale(100%);   
-ms-filter: grayscale(100%); -o-filter: grayscale(100%);   
filter: url(desaturate.svg#greyscale);   
filter: gray;   
-webkit-filter: grayscale(1);   
}
Copy after login



If you want to achieve this visual effect in all browsers (assuming your visitors support JavaScript) you can use jQuery or Greyscale.js to modify your image to remove color.

The CSS code we wrote above allows us to turn the image into black and white without using PhotoShop. Using CSS to implement this feature can be very easy to modify: for example, you can see that when we change the decolorization parameter from 100% to 50%, the image will have the effect of a blend of primary colors and black and white.

Other effects:

In addition, some other filter effects can be added to black and white photos:

-webkit-filter:blur(5px);  //模糊,此处为5像素   

-webkit-filter:sepia(0.5);  //叠加褐色,取值范围0-1,此处表示50%的褐色   

-webkit-filter:brightness(0.5);  //亮度,取值范围0-1,5倍亮度(数字为0时为正常样式,为1时表示的是100%亮度,无法看到图片)   

-webkit-filter:hue-rotate(30deg); //色相(按照色相环进行旋转,顺时针方向,红-橙-黄-黄绿-绿-蓝绿-蓝-蓝紫-紫-紫红-红)此处为叠加黄色滤镜   

-webkit-filter:invert(1);  //反色,取值范围0-1,0为原图,1为彻底反色之后,0.5为灰色   

-webkit-filter:saturate(4);  //饱和度,取值范围0~*,0为无饱和度,1为原图,值越高饱和度越大   

-webkit-filter:contrast(2);  //对比度,取值范围0~*,0为无对比度(灰色),1为原图,值越高对比度越大   

-webkit-filter:opacity(0.8);  //透明度,取值范围0~1,0为全透明,1为原图   

-webkit-filter:drop-shadow(17px 17px 20px black); //阴影
Copy after login



The above is the detailed content of Introduction to how to use CSS3 to write grayscale filters to create black and white photo effects. 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 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
1663
14
PHP Tutorial
1266
29
C# Tutorial
1237
24
Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How We Created a Static Site That Generates Tartan Patterns in SVG How We Created a Static Site That Generates Tartan Patterns in SVG Apr 09, 2025 am 11:29 AM

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

PHP is A-OK for Templating PHP is A-OK for Templating Apr 11, 2025 am 11:04 AM

PHP templating often gets a bad rap for facilitating subpar code — but that doesn&#039;t have to be the case. Let’s look at how PHP projects can enforce a basic

Programming Sass to Create Accessible Color Combinations Programming Sass to Create Accessible Color Combinations Apr 09, 2025 am 11:30 AM

We are always looking to make the web more accessible. Color contrast is just math, so Sass can help cover edge cases that designers might have missed.

See all articles